Skip to content

Commit

Permalink
Another malloc fix
Browse files Browse the repository at this point in the history
  • Loading branch information
slobodan-ilic committed Jun 20, 2024
1 parent a435d6a commit c21f15c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
22 changes: 21 additions & 1 deletion src/spss/readstat_sav.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ sav_ctx_t *sav_ctx_init(sav_file_header_record_t *header, readstat_io_t *io) {
return NULL;
}

ctx->mr_sets = NULL;

ctx->io = io;

return ctx;
Expand Down Expand Up @@ -89,6 +91,24 @@ void sav_ctx_free(sav_ctx_t *ctx) {
if (ctx->variable_display_values) {
free(ctx->variable_display_values);
}
if (ctx->mr_sets) {
for (size_t i = 0; i < ctx->multiple_response_sets_length; i++) {
if (ctx->mr_sets[i].name) {
free(ctx->mr_sets[i].name);
}
if (ctx->mr_sets[i].label) {
free(ctx->mr_sets[i].label);
}
if (ctx->mr_sets[i].subvariables) {
for (size_t j = 0; j < ctx->mr_sets[i].num_subvars; j++) {
if (ctx->mr_sets[i].subvariables[j]) {
free(ctx->mr_sets[i].subvariables[j]);
}
}
free(ctx->mr_sets[i].subvariables);
}
}
free(ctx->mr_sets);
}
free(ctx);
}

5 changes: 4 additions & 1 deletion src/spss/readstat_sav_read.c
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,10 @@ static readstat_error_t sav_read_multiple_response_sets(size_t data_len, sav_ctx
char *token = strtok(mr_string, "$\n");
int num_lines = 0;
while (token != NULL) {
ctx->mr_sets = realloc(ctx->mr_sets, (num_lines + 1) * sizeof(mr_set_t));
if ((ctx->mr_sets = realloc(ctx->mr_sets, (num_lines + 1) * sizeof(mr_set_t))) == NULL) {
retval = READSTAT_ERROR_MALLOC;
goto cleanup;
}
retval = parse_mr_line(token, &ctx->mr_sets[num_lines]);
if (retval != READSTAT_OK) goto cleanup;
num_lines++;
Expand Down

0 comments on commit c21f15c

Please sign in to comment.