Skip to content

lib: use checked size arithmetic for internal container growth - #305

Merged
hyperrealm merged 1 commit into
hyperrealm:masterfrom
vera-rubin:fix/checked-alloc-sizes
Oct 19, 2026
Merged

hyperrealm merged 1 commit into
hyperrealm:masterfrom
vera-rubin:fix/checked-alloc-sizes

Conversation

@vera-rubin

Copy link
Copy Markdown
Contributor

The string buffer, string vector and setting list code compute allocation sizes inline (n * size, a + b + 1). This routes them through two checked util.c helpers, libconfig_reallocarray() and libconfig_size_add(). A size that doesn't fit in size_t goes to the fatal error handler, as allocation failures already do, so the container code is harder to misuse as it evolves.

Also: strcpy/strcat become memcpy of known lengths in strbuf.c and config_default_include_func(), and __config_list_remove() takes an unsigned index with typed pointer arithmetic.

Parsed values and written output are unchanged. No API or ABI changes. Tested on Ubuntu 24.04 with gcc 13 and clang 18 (CMake and autotools test suites, fuzzer corpus replay), with three new tests.

If it lands, we may submit it to Google's Patch Rewards program, and we'd be glad to share any reward with the maintainers.

— ChipLab

Route every allocation-size computation in the string buffer, string
vector and setting list code through two small helpers in util.c:

  libconfig_reallocarray()  checks nmemb * size before reallocating
  libconfig_size_add()      checks a + b

If a size cannot be represented in a size_t, the helpers call the
fatal error handler, exactly as libconfig_malloc() and friends already
do for an allocation failure, so the existing
config_set_fatal_error_func() hook covers it.

Also:
- libconfig_strbuf_append_string() and config_default_include_func()
  copy with memcpy() using lengths they have already computed, instead
  of strcpy()/strcat().
- __config_list_remove() takes an unsigned index (matching its callers
  and config_list_t.length) and moves elements with typed pointer
  arithmetic instead of an int byte offset.
- The include file array is allocated with libconfig_calloc(2,
  sizeof(const char *)) instead of sizeof(char **) * 2.

Parsed values and written output are unchanged, and there are no public
API or ABI changes. New tests cover list growth and removal across
allocation chunks, long concatenated strings, include-directory path
joining, and more include files than the file name vector chunk size.
@hyperrealm
hyperrealm merged commit 139903c into hyperrealm:master Oct 19, 2026
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