Skip to content

Commit 230e1ef

Browse files
committed
fix: multiple mr_sets
1 parent aee63b2 commit 230e1ef

1 file changed

Lines changed: 26 additions & 4 deletions

File tree

src/spss/readstat_sav_read.c

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1365,13 +1365,35 @@ static readstat_error_t sav_parse_records_pass1(sav_ctx_t *ctx) {
13651365
if (retval != READSTAT_OK)
13661366
goto cleanup;
13671367
} else if (subtype == SAV_RECORD_SUBTYPE_MULTIPLE_RESPONSE_SETS || subtype == SAV_RECORD_SUBTYPE_MULTIPLE_RESPONSE_SETS_V14) {
1368-
if (ctx->mr_sets != NULL) {
1369-
retval = READSTAT_ERROR_BAD_MR_STRING;
1370-
goto cleanup;
1371-
}
1368+
// Files may contain multiple MR set records (subtype 7 and/or 19)
1369+
// Save existing MR sets to merge with new ones
1370+
mr_set_t *old_mr_sets = ctx->mr_sets;
1371+
size_t old_count = ctx->multiple_response_sets_length;
1372+
1373+
// Reset context to load new MR sets
1374+
ctx->mr_sets = NULL;
1375+
ctx->multiple_response_sets_length = 0;
1376+
13721377
retval = sav_read_multiple_response_sets(data_len, ctx);
13731378
if (retval != READSTAT_OK)
13741379
goto cleanup;
1380+
1381+
// Merge with existing MR sets if any
1382+
if (old_mr_sets != NULL && old_count > 0) {
1383+
size_t total_count = old_count + ctx->multiple_response_sets_length;
1384+
mr_set_t *merged = readstat_realloc(old_mr_sets, total_count * sizeof(mr_set_t));
1385+
if (merged == NULL) {
1386+
retval = READSTAT_ERROR_MALLOC;
1387+
goto cleanup;
1388+
}
1389+
1390+
// Append new MR sets after existing ones
1391+
memcpy(merged + old_count, ctx->mr_sets, ctx->multiple_response_sets_length * sizeof(mr_set_t));
1392+
free(ctx->mr_sets);
1393+
1394+
ctx->mr_sets = merged;
1395+
ctx->multiple_response_sets_length = total_count;
1396+
}
13751397
} else {
13761398
if (io->seek(data_len, READSTAT_SEEK_CUR, io->io_ctx) == -1) {
13771399
retval = READSTAT_ERROR_SEEK;

0 commit comments

Comments
 (0)