From c0bb93b1e559b7aba34d413c9a54cc35d8e67794 Mon Sep 17 00:00:00 2001 From: Slobodan Ilic Date: Sun, 22 Sep 2024 12:40:01 +0200 Subject: [PATCH] Fix mem leak --- src/spss/readstat_sav_parse_mr_name.c | 47 ++++++++++++++------------ src/spss/readstat_sav_parse_mr_name.rl | 5 ++- 2 files changed, 29 insertions(+), 23 deletions(-) diff --git a/src/spss/readstat_sav_parse_mr_name.c b/src/spss/readstat_sav_parse_mr_name.c index 3fb8318..5c01fa0 100644 --- a/src/spss/readstat_sav_parse_mr_name.c +++ b/src/spss/readstat_sav_parse_mr_name.c @@ -1,5 +1,5 @@ -#line 1 "./src/spss/readstat_sav_parse_mr_name.rl" +#line 1 "src/spss/readstat_sav_parse_mr_name.rl" #include #include #include @@ -7,7 +7,7 @@ #include "../readstat_malloc.h" -#line 11 "./src/spss/readstat_sav_parse_mr_name.c" +#line 11 "src/spss/readstat_sav_parse_mr_name.c" static const char _mr_extractor_actions[] = { 0, 1, 0, 1, 1, 1, 2, 1, 3, 1, 4 @@ -67,7 +67,7 @@ static const int mr_extractor_start = 1; static const int mr_extractor_en_main = 1; -#line 75 "./src/spss/readstat_sav_parse_mr_name.rl" +#line 75 "src/spss/readstat_sav_parse_mr_name.rl" readstat_error_t extract_mr_data(const char *line, mr_set_t *result) { @@ -89,14 +89,14 @@ readstat_error_t extract_mr_data(const char *line, mr_set_t *result) { // Execute Ragel finite state machine (FSM) -#line 93 "./src/spss/readstat_sav_parse_mr_name.c" +#line 93 "src/spss/readstat_sav_parse_mr_name.c" { cs = mr_extractor_start; } -#line 96 "./src/spss/readstat_sav_parse_mr_name.rl" +#line 96 "src/spss/readstat_sav_parse_mr_name.rl" -#line 100 "./src/spss/readstat_sav_parse_mr_name.c" +#line 100 "src/spss/readstat_sav_parse_mr_name.c" { int _klen; unsigned int _trans; @@ -171,7 +171,7 @@ readstat_error_t extract_mr_data(const char *line, mr_set_t *result) { switch ( *_acts++ ) { case 0: -#line 10 "./src/spss/readstat_sav_parse_mr_name.rl" +#line 10 "src/spss/readstat_sav_parse_mr_name.rl" { mr_name = (char *)malloc(p - start + 1); memcpy(mr_name, start, p - start); @@ -179,14 +179,14 @@ readstat_error_t extract_mr_data(const char *line, mr_set_t *result) { } break; case 1: -#line 16 "./src/spss/readstat_sav_parse_mr_name.rl" +#line 16 "src/spss/readstat_sav_parse_mr_name.rl" { mr_type = *p; start = p + 1; } break; case 2: -#line 21 "./src/spss/readstat_sav_parse_mr_name.rl" +#line 21 "src/spss/readstat_sav_parse_mr_name.rl" { int n_cv_digs = p - start; char *n_dig_str = (char *)malloc(n_cv_digs + 1); @@ -207,7 +207,7 @@ readstat_error_t extract_mr_data(const char *line, mr_set_t *result) { } break; case 3: -#line 40 "./src/spss/readstat_sav_parse_mr_name.rl" +#line 40 "src/spss/readstat_sav_parse_mr_name.rl" { char *lbl_len_str = (char *)malloc(p - start + 1); memcpy(lbl_len_str, start, p - start); @@ -221,7 +221,7 @@ readstat_error_t extract_mr_data(const char *line, mr_set_t *result) { } break; case 4: -#line 52 "./src/spss/readstat_sav_parse_mr_name.rl" +#line 52 "src/spss/readstat_sav_parse_mr_name.rl" { int len = p - start; char *subvar = (char *)malloc(len + 1); @@ -233,7 +233,7 @@ readstat_error_t extract_mr_data(const char *line, mr_set_t *result) { mr_subvariables[mr_subvar_count++] = subvar; } break; -#line 237 "./src/spss/readstat_sav_parse_mr_name.c" +#line 237 "src/spss/readstat_sav_parse_mr_name.c" } } @@ -246,7 +246,7 @@ readstat_error_t extract_mr_data(const char *line, mr_set_t *result) { _out: {} } -#line 97 "./src/spss/readstat_sav_parse_mr_name.rl" +#line 97 "src/spss/readstat_sav_parse_mr_name.rl" // Check if FSM finished successfully if (cs < 9 || p != pe) { @@ -288,7 +288,7 @@ readstat_error_t parse_mr_line(const char *line, mr_set_t *result) { } -#line 292 "./src/spss/readstat_sav_parse_mr_name.c" +#line 292 "src/spss/readstat_sav_parse_mr_name.c" static const char _mr_parser_actions[] = { 0, 1, 0 }; @@ -331,7 +331,7 @@ static const int mr_parser_start = 1; static const int mr_parser_en_main = 1; -#line 157 "./src/spss/readstat_sav_parse_mr_name.rl" +#line 160 "src/spss/readstat_sav_parse_mr_name.rl" readstat_error_t parse_mr_string(const char *line, mr_set_t **mr_sets, size_t *n_mr_lines) { @@ -344,14 +344,14 @@ readstat_error_t parse_mr_string(const char *line, mr_set_t **mr_sets, size_t *n *n_mr_lines = 0; -#line 348 "./src/spss/readstat_sav_parse_mr_name.c" +#line 348 "src/spss/readstat_sav_parse_mr_name.c" { cs = mr_parser_start; } -#line 169 "./src/spss/readstat_sav_parse_mr_name.rl" +#line 172 "src/spss/readstat_sav_parse_mr_name.rl" -#line 355 "./src/spss/readstat_sav_parse_mr_name.c" +#line 355 "src/spss/readstat_sav_parse_mr_name.c" { int _klen; unsigned int _trans; @@ -426,19 +426,22 @@ readstat_error_t parse_mr_string(const char *line, mr_set_t **mr_sets, size_t *n switch ( *_acts++ ) { case 0: -#line 140 "./src/spss/readstat_sav_parse_mr_name.rl" +#line 140 "src/spss/readstat_sav_parse_mr_name.rl" { char *mln = (char *)malloc(p - start); memcpy(mln, start + 1, p - start); mln[p - start - 1] = '\0'; *mr_sets = realloc(*mr_sets, ((*n_mr_lines) + 1) * sizeof(mr_set_t)); retval = parse_mr_line(mln, &(*mr_sets)[*n_mr_lines]); - if (retval != READSTAT_OK) goto cleanup; + if (retval != READSTAT_OK) { + if (mln == NULL) free(mln); + goto cleanup; + } (*n_mr_lines)++; start = p + 1; } break; -#line 442 "./src/spss/readstat_sav_parse_mr_name.c" +#line 445 "src/spss/readstat_sav_parse_mr_name.c" } } @@ -451,7 +454,7 @@ readstat_error_t parse_mr_string(const char *line, mr_set_t **mr_sets, size_t *n _out: {} } -#line 170 "./src/spss/readstat_sav_parse_mr_name.rl" +#line 173 "src/spss/readstat_sav_parse_mr_name.rl" if (cs < 4 || p != pe) { retval = READSTAT_ERROR_BAD_MR_STRING; diff --git a/src/spss/readstat_sav_parse_mr_name.rl b/src/spss/readstat_sav_parse_mr_name.rl index c9afae8..62e0558 100644 --- a/src/spss/readstat_sav_parse_mr_name.rl +++ b/src/spss/readstat_sav_parse_mr_name.rl @@ -143,7 +143,10 @@ readstat_error_t parse_mr_line(const char *line, mr_set_t *result) { mln[p - start - 1] = '\0'; *mr_sets = realloc(*mr_sets, ((*n_mr_lines) + 1) * sizeof(mr_set_t)); retval = parse_mr_line(mln, &(*mr_sets)[*n_mr_lines]); - if (retval != READSTAT_OK) goto cleanup; + if (retval != READSTAT_OK) { + if (mln == NULL) free(mln); + goto cleanup; + } (*n_mr_lines)++; start = p + 1; }