fix(examples): guard shape_buf overflow in npy_writer#167
Closed
LeoBuron wants to merge 1 commit into
Closed
Conversation
writeNpy's shape_buf[256] is filled by snprintf calls whose remaining-size argument `sizeof(shape_buf) - shape_len` unsigned-underflowed when shape_len exceeded 256 (e.g., ndim around 120+, or smaller with multi- digit dims). The underflowed huge size_t bypassed snprintf's bounds check and allowed OOB writes — caught by UBSan at npy_writer.c:22 as "index 257 out of bounds for type 'char[256]'". Fix: extract appendBounded helper that vsnprintf's into a fixed buffer with explicit bounds checking (returns -1 on truncation, error, or already-past-end offset). All four shape_buf append sites route through it; a single rc=6 is returned if any indicates truncation. Regression test builds an ASan+UBSan harness with ndim=200, asserts no sanitizer trip and rc != 0. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fix the latent buffer-bounds bug in
examples/_shared/npy_writer.cflagged in #165.writeNpyfilledshape_buf[256]via a snprintf loop whose remaining-size argumentsizeof(shape_buf) - shape_lenunsigned-underflowed whenshape_lenexceeded 256. The underflowed hugesize_tbypassed snprintf's bounds check and allowed OOB writes — caught by UBSan atnpy_writer.c:22asindex 257 out of bounds for type 'char[256]'. Triggered by high-rank shapes (ndim ~120+, or smaller with multi-digit dims).What changed
appendBoundedhelper innpy_writer.cthat wrapsvsnprintfwith explicit bounds checking (offset >= buf_size→ -1; truncation → -1).snprintfsites forshape_bufwithappendBoundedcalls; singlerc=6returned at end if any indicates truncation.#include <stdarg.h>forva_list.Test plan
test_npy_writer_rejects_high_rank_shape_that_overflows_buf(TDD: written before the fix). Compiles an ASan+UBSan harness with ndim=200, asserts no sanitizer trip and rc != 0.UndefinedBehaviorSanitizer: runtime error: index 257 out of bounds for type 'char[256]'atnpy_writer.c:22.unit_test(gcc) 42/42,unit_test_asan42/42,pytest22 passed (was 21, +1 new test).Closes #165
🤖 Generated with Claude Code