Repository navigation
Conversation
Initial allocation uses HANDSEED, which is often not a multiple of REFS_PER_REALLOC. The old grow path only realloc'd when num_object_refs % REFS_PER_REALLOC == 0, so later inserts wrote past the buffer (seen as WASM/heap crashes on large drawings). * include/dwg.h (Dwg_Data): Add num_alloced_object_refs. * src/decode.c (dwg_decode, dwg_decode_add_object_ref): Track capacity and grow when num_object_refs reaches it. * src/free.c (dwg_free): Clear num_alloced_object_refs.
The previous patch accidentally split the format string across a raw newline instead of embedding \n, which is invalid C and tripped CodeFactor.
CodeFactor flags "Redundant blank line at the end of a code block" on include/dwg.h whenever the file is touched. Remove the 18 pre-existing blank lines immediately before struct/enum closers so the object_ref capacity change can pass CI.
rurban
left a comment
There was a problem hiding this comment.
Do we really have track that extra? The mod calc can easily be done dynamically, just needs to be fixed
|
I can confirm that the reallocs strategy is broken for the initial chunks between the HANDSEED and chunk size, but I'll fix it better |
|
Thanks for the report and the patch (#1526). I closed this PR and fixed it differently, in branch
I could not reproduce the overflow with ASAN: when the count reaches the next multiple of 16384 the vector is realloc'ed before it can reach an initial HANDSEED-sized capacity, also for a HANDSEED of 20480 or 40000. If you have a file which crashes in |
The object_ref vector grows by REFS_PER_REALLOC when the count reaches a multiple of it, but the first reservation was HANDSEED, no multiple. Reserve a multiple, and cap it: HANDSEED comes from the file, and the vector grows in chunks anyway. GH #1526, PR #1532. * src/decode.c (dwg_decode_add_object_ref): round up and cap max_refs. * add_test: test_object_ref_chunks. Reported-by: mlightcad
Summary
Fixes #1526.
Initial
object_refallocation usesHANDSEED, which is often not a multiple ofREFS_PER_REALLOC. The grow path only realloc'd whennum_object_refs % REFS_PER_REALLOC == 0, so later inserts wrote past the buffer.Change
Dwg_Data.num_alloced_object_refsnum_object_refs >= num_alloced_object_refsdirty_refsTest plan
dwg_decode_add_object_refmake check(or at least decode smoke tests)