@@ -374,24 +374,26 @@ static int32_t get_nvars(const char *fn)
374374{
375375 FILE * fh = fopen (fn , "r" );
376376 char * line = NULL ;
377+ char * line2 = NULL ;
377378 size_t len ;
378379 nvars_t nvars = -1 ;
379380
380381 /* number of variables is read from first line, it is 1 + (number of commata) */
381382 if (getline (& line , & len , fh ) != -1 )
382383 {
383384 nvars = 1 ;
384- line = strchr (line , ',' );
385- while (line != NULL )
385+ line2 = strchr (line , ',' );
386+ while (line2 != NULL )
386387 {
387388 nvars ++ ;
388389 // line points to a comma, which must be followed by one or more characters
389390 // --> line+1 is valid
390- line = strchr (line + 1 , ',' );
391+ line2 = strchr (line2 + 1 , ',' );
391392 }
392393 }
393394
394395 free (line );
396+ free (line2 );
395397 fclose (fh );
396398
397399 return nvars ;
@@ -606,11 +608,11 @@ static void remove_trailing_delim(char *line, size_t *len, char delim) {
606608 }
607609}
608610
609- static void get_nterms_and_all_nterms (FILE * fh , char * * linep ,
611+ static void get_nterms_and_all_nterms (FILE * fh ,
610612 int max_line_size , data_gens_ff_t * gens ,
611613 int32_t * nr_gens , nelts_t * nterms , nelts_t * all_nterms ){
612614
613- char * line = * linep ;
615+ char * line = NULL ;
614616 size_t len = 0 ;
615617 ssize_t nread ;
616618 for (int32_t i = 0 ; i < * nr_gens ; i ++ ) {
@@ -626,7 +628,7 @@ static void get_nterms_and_all_nterms(FILE *fh, char **linep,
626628 gens -> lens [i ] = * nterms ;
627629 * all_nterms += * nterms ;
628630 }
629- * linep = line ;
631+ free ( line ) ;
630632 gens -> nterms = * all_nterms ;
631633}
632634
@@ -861,13 +863,13 @@ static int get_coefficient_mpz_and_term_from_line(char *line, int32_t nterms,
861863 return 1 ;
862864}
863865
864- static void get_coeffs_and_exponents_ff32 (FILE * fh , char * * linep , nelts_t all_nterms ,
866+ static void get_coeffs_and_exponents_ff32 (FILE * fh , nelts_t all_nterms ,
865867 int32_t * nr_gens , data_gens_ff_t * gens ){
866868 int32_t pos = 0 ;
867869 size_t len = 0 ;
868870 ssize_t nread ;
869871
870- char * line = * linep ;
872+ char * line = NULL ;
871873 if (getline (& line , & len , fh ) != -1 ){
872874 }
873875 if (getline (& line , & len , fh ) != -1 ){
@@ -893,17 +895,17 @@ static void get_coeffs_and_exponents_ff32(FILE *fh, char **linep, nelts_t all_nt
893895 }
894896 pos += gens -> lens [i ];
895897 }
896- * linep = line ;
898+ free ( line ) ;
897899}
898900
899901
900- static void get_coeffs_and_exponents_mpz (FILE * fh , char * * linep , nelts_t all_nterms ,
902+ static void get_coeffs_and_exponents_mpz (FILE * fh , nelts_t all_nterms ,
901903 int32_t * nr_gens , data_gens_ff_t * gens ){
902904 int32_t pos = 0 ;
903905 size_t len = 0 ;
904906 ssize_t nread ;
905907
906- char * line = * linep ;
908+ char * line = NULL ;
907909 if (getline (& line , & len , fh ) != -1 ){
908910 }
909911 if (getline (& line , & len , fh ) != -1 ){
@@ -936,7 +938,7 @@ static void get_coeffs_and_exponents_mpz(FILE *fh, char **linep, nelts_t all_nte
936938 }
937939 pos += 2 * gens -> lens [i ];
938940 }
939- * linep = line ;
941+ free ( line ) ;
940942}
941943
942944
@@ -987,6 +989,9 @@ static inline void get_data_from_file(char *fn, int32_t *nr_vars,
987989 char * * vnames = (char * * )malloc ((* nr_vars ) * sizeof (char * ));
988990 get_variables (fh , line , max_line_size , nr_vars , gens , vnames );
989991 if (duplicate_vnames (vnames , * nr_vars ) == 1 ) {
992+ for (int32_t i = 0 ; i < * nr_vars ; i ++ ){
993+ free (vnames [i ]);
994+ }
990995 free (vnames );
991996 free (line );
992997 exit (1 );
@@ -996,17 +1001,17 @@ static inline void get_data_from_file(char *fn, int32_t *nr_vars,
9961001 initialize_data_gens (* nr_vars , * nr_gens , * field_char , gens );
9971002
9981003 nelts_t nterms , all_nterms = 0 ;
999- get_nterms_and_all_nterms (fh , & line , max_line_size , gens , nr_gens ,
1004+ get_nterms_and_all_nterms (fh , max_line_size , gens , nr_gens ,
10001005 & nterms , & all_nterms );
10011006
10021007 fclose (fh );
10031008 fh = fopen (fn , "r" );
10041009
10051010 if (gens -> field_char ){
1006- get_coeffs_and_exponents_ff32 (fh , & line , all_nterms , nr_gens , gens );
1011+ get_coeffs_and_exponents_ff32 (fh , all_nterms , nr_gens , gens );
10071012 }
10081013 else {
1009- get_coeffs_and_exponents_mpz (fh , & line , all_nterms , nr_gens , gens );
1014+ get_coeffs_and_exponents_mpz (fh , all_nterms , nr_gens , gens );
10101015 }
10111016
10121017 free (line );
0 commit comments