AVRO-4305: [c] Make st_insert() OOM-safe - #3969
Open
iemejia wants to merge 1 commit into
Open
Conversation
The vendored st hash table allocated entries via avro_new() inside the ADD_DIRECT macro (used by st_insert and st_add_direct) without checking for a NULL return. On allocation failure this dereferenced NULL and crashed rather than reporting an error, so map decoding and every other st user was not OOM-safe on that path. The rehash() helper had the same problem with its Calloc() of the new bin array. - ADD_DIRECT now receives a pre-allocated entry; st_insert allocates it, returns -1 on failure and leaves the table unchanged (0 = inserted, 1 = updated, as before). st_add_direct (void) becomes a no-op on failure. - rehash() skips the rehash and keeps the existing bins when the new bin array cannot be allocated (the table stays correct, only denser). - The only caller that inspects the return value (save_named_schemas in schema.c) already treats any non-zero as an error, so -1 propagates correctly. Adds test_avro_4305, which installs a failing allocator and verifies st_insert returns -1 without crashing and leaves the table unchanged, and that a subsequent insert still works. The test segfaults without the fix.
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.
What changes were proposed in this pull request?
The vendored
sthash table used by the C SDK (lang/c/src/st.c) allocated table entries viaavro_new()inside theADD_DIRECTmacro (used byst_insert()andst_add_direct()) without checking for a NULL return:On allocation failure this dereferenced NULL and crashed rather than reporting an error, so map decoding — and every other
stuser — was not OOM-safe on that path.rehash(), which runs insideADD_DIRECT, had the same problem with theCalloc()of its new bin array.Noted during review of AVRO-4293 (bounded allocation when decoding length-prefixed values/collections); that PR fixed the
map.cside but deliberately left this shared-code change out of scope.How was this patch fixed?
ADD_DIRECTnow receives a pre-allocated entry.st_insert()allocates it, returns-1on failure and leaves the table unchanged (0= inserted,1= updated, as before).st_add_direct()(which returnsvoid) becomes a no-op on allocation failure instead of crashing.rehash()skips the rehash and keeps the existing bins when the new bin array cannot be allocated — the table stays correct, only denser.save_named_schemasinschema.c) already treats any non-zero as an error, so the new-1propagates correctly. All other callers ignore the return value and simply observe an unchanged table.st_insert's contract is documented inst.h.How was this patch tested?
New
test_avro_4305installs a failing allocator and verifies that:st_insert()returns-1on OOM without crashing and leaves the table unchanged;Verified the test segfaults without the fix (SIGSEGV) and passes with it. Full C suite: 54/54 tests pass, including the valgrind
memcheck_test_avro_4305variant (no leaks).