| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
readstat_copy_label() allocates strlen(label) bytes and memcpy's strlen(label) bytes — no NUL terminator. Any consumer calling strlen() on the stored label reads past the end of the allocation. readstat_label_string_value() has the same bug for string_key: it stores the key string without a NUL terminator. Both allocations now use strlen + 1 and copy strlen + 1 bytes. readstat_copy_label() changes return type to readstat_error_t to propagate allocation failure.
| Back | FazBrowse Home | New Git URL |
PR 3: Fix off-by-one in label/key malloc — missing NUL terminator
Summary
Two malloc calls in readstat_writer.c allocate strlen(s) bytes and then
memcpy exactly strlen(s) bytes — omitting the NUL terminator. This
produces a heap allocation with no null terminator, so any code that later
calls strlen on the stored string reads past the end of the allocation.
Both sites are in the public writer API and are exercised by every caller that
registers a value label or a string-keyed label.
Site 1: readstat_copy_label — label string for value labels
Vulnerable code
What goes wrong
strlen("Male") returns 4. malloc(4) returns a 4-byte buffer.
memcpy(..., 4) fills all 4 bytes with M, a, l, e.
There is no NUL at byte 4.
If any code later does strlen(value_label->label), it reads into the next
heap chunk until it finds a zero byte — undefined behaviour and a potential
information leak.
The label_len field is used to avoid strlen in most internal paths, so
this is silent in the common case but breaks if the label is ever treated as a
C string (e.g. in error messages, debug output, or downstream consumers of the
readstat_value_label_t struct).
Reproduction
Fix
The function signature changes from void to readstat_error_t to propagate
the allocation failure.
Site 2: readstat_label_string_value — string key for string-keyed value labels
Vulnerable code
What goes wrong
Identical issue: string_key is allocated without room for a NUL terminator.
String-keyed value labels are used in SPSS SAV files for string variables.
Fix