Skip to content

add malloc failure test for cgif_raw API#93

Merged
dloebl merged 4 commits into
mainfrom
ealloc_raw_test
Feb 15, 2026
Merged

add malloc failure test for cgif_raw API#93
dloebl merged 4 commits into
mainfrom
ealloc_raw_test

Conversation

@dloebl

@dloebl dloebl commented Feb 10, 2026

Copy link
Copy Markdown
Owner

Iteratively fail each malloc call in the cgif_raw code path (newgif + addframe + close) to verify all allocation failures are handled correctly without crashes.

@dloebl dloebl self-assigned this Feb 10, 2026
Comment thread src/cgif_raw.c
if(pContext == NULL) {
return CGIF_EALLOC;
}
memset(pContext, 0, sizeof(LZWGenState));

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Found by the new ealloc_raw test with ASAN:
When an early malloc fails, the goto cleanup path frees all pContext fields, including ones never assigned. Zero-initialize pContext so unassigned pointers are NULL and safe to free.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds a targeted regression test that injects malloc failures across the cgif_raw encode path to ensure allocation failures are handled cleanly (no crashes / invalid frees), and tightens internal initialization to make failure cleanup safe.

Changes:

  • Add a new Meson test target (ealloc_raw) that compiles cgif_raw.c directly so malloc can be intercepted.
  • Introduce tests/ealloc_raw.c, which iteratively fails each malloc call during cgif_raw_newgif + cgif_raw_addframe + cgif_raw_close.
  • Zero-initialize LZWGenState after allocation to ensure cleanup paths can safely free() partially-initialized members.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
tests/meson.build Registers the new ealloc_raw malloc-failure injection test executable.
tests/ealloc_raw.c Implements iterative malloc failure injection by compiling cgif_raw.c into the test TU with a malloc macro override.
src/cgif_raw.c Initializes LZWGenState to zero to make allocation-failure cleanup safe.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

MCLoebl
MCLoebl previously approved these changes Feb 14, 2026

@MCLoebl MCLoebl left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. Minor stuff: is the initialization in l.11,12 of ealloc_raw.c needed? I suggest replacing n by the more intuitive name fail_after in l.69, 78, 87 as well.

@dloebl
dloebl merged commit dcd45c7 into main Feb 15, 2026
15 checks passed
@dloebl
dloebl deleted the ealloc_raw_test branch February 15, 2026 12:18
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.

3 participants