Skip to content

sas_rle: null-check the output buffer before pointer arithmetic - #438

Merged
evanmiller merged 1 commit into
WizardMac:devfrom
ziyziy89:sas-rle-null-output-pointer
Nov 18, 2026
Merged

evanmiller merged 1 commit into
WizardMac:devfrom
ziyziy89:sas-rle-null-output-pointer

Conversation

@ziyziy89

Copy link
Copy Markdown
Contributor

sas_rle_compress runs a measuring pass with a NULL output buffer (through sas_rle_compressed_len, and the sas7bdat writer's ctx->compress(NULL, 0, ...)) to size a compressed row before allocating. Its two emit helpers, sas_rle_copy_run and sas_rle_insert_run, evaluate output_buf + offset before the if (output_buf == NULL) early return. Once an earlier run has advanced the offset, the measuring pass forms NULL + offset with a non-zero offset, which is undefined behavior under C11 6.5.6p8. Nothing dereferences the pointer, but UBSan's pointer-overflow check flags it, and it fires in the project's own make check (the sas7bdat row-compressed round-trips) once the suite is built with -fsanitize=undefined.

Move the NULL check above the pointer arithmetic in both helpers so the offset is only applied to a real buffer. The writing pass is unaffected and the measuring pass returns the same length as before.

Built with -fsanitize=undefined, sas_rle_compressed_len("abcdef\0\0\0\0\0\0\0\0\0\0", 16) reports runtime error: applying non-zero offset 7 to null pointer at readstat_sas_rle.c:194 before the change and returns 8 cleanly after.

@evanmiller
evanmiller merged commit d8ed8fa into WizardMac:dev Nov 18, 2026
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants