diff --git a/src/stata/readstat_dta.c b/src/stata/readstat_dta.c index 0f35aaa..d21804d 100644 --- a/src/stata/readstat_dta.c +++ b/src/stata/readstat_dta.c @@ -56,6 +56,15 @@ readstat_error_t dta_ctx_init(dta_ctx_t *ctx, uint32_t nvar, uint64_t nobs, ctx->nvar = nvar; ctx->nobs = nobs; + /* Each variable takes bytes in the file's descriptor lists (checked + * exactly further on): a count past the file's size is damage, and + * the table below would set aside 4 bytes for each first (a 93-byte + * file asked for 1 GB). johnmcm.com, Nov 2026. */ + if (ctx->file_size > 0 && ctx->nvar > ctx->file_size) { + retval = READSTAT_ERROR_PARSE; + goto cleanup; + } + if (ctx->nvar) { if ((ctx->variables = calloc(ctx->nvar, sizeof(readstat_variable_t *))) == NULL) { retval = READSTAT_ERROR_MALLOC;