Skip to content

decode: track object_ref allocation capacity separately - #1532

Closed
mlightcad wants to merge 3 commits into
LibreDWG:masterfrom
mlightcad:fix/object-ref-capacity
Closed

mlightcad wants to merge 3 commits into
LibreDWG:masterfrom
mlightcad:fix/object-ref-capacity

Conversation

@mlightcad

Copy link
Copy Markdown
Contributor

Summary

Fixes #1526.

Initial object_ref allocation uses HANDSEED, which is often not a multiple of REFS_PER_REALLOC. The grow path only realloc'd when num_object_refs % REFS_PER_REALLOC == 0, so later inserts wrote past the buffer.

Change

  • Add Dwg_Data.num_alloced_object_refs
  • Grow when num_object_refs >= num_alloced_object_refs
  • Keep upstream's note that pointer-array realloc does not need dirty_refs

Test plan

  • Decode a large DWG that previously crashed under ASan/WASM around dwg_decode_add_object_ref
  • make check (or at least decode smoke tests)

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 rurban 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.

Do we really have track that extra? The mod calc can easily be done dynamically, just needs to be fixed

@rurban rurban self-assigned this Nov 15, 2026
@rurban rurban added this to the 0.14.1 milestone Nov 15, 2026
@rurban rurban added bug Something isn't working blocking questionable labels Nov 15, 2026
@rurban

rurban commented Nov 15, 2026

Copy link
Copy Markdown
Contributor

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

@rurban rurban closed this Nov 15, 2026
@rurban

rurban commented Nov 15, 2026

Copy link
Copy Markdown
Contributor

Thanks for the report and the patch (#1526). I closed this PR and fixed it differently, in branch smoke/pr1532 (commit b573b35), for these reasons:

  • The vector already grows in whole REFS_PER_REALLOC chunks, when the count reaches a multiple of it. We want to keep that, and not realloc for every new ref. Only the first reservation, taken from HANDSEED, was not a multiple of the chunk size, so the first reservation is now rounded up to a multiple.
  • A HANDSEED comes from the file, so the first calloc is capped at 64 chunks (about 1M refs). More refs still grow in chunks. A crafted HANDSEED cannot force a huge allocation anymore.
  • No new num_alloced_object_refs field in the public Dwg_Data. It would change the struct layout of every user of the library for a value we can derive from the count.
  • The patch also removed many unrelated blank lines in include/dwg.h, which should not be mixed into a fix.

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 dwg_decode_add_object_ref, please attach it, so we can add it as a test. Added test_object_ref_chunks meanwhile, which exercises the growth.

@mlightcad
mlightcad deleted the fix/object-ref-capacity branch November 15, 2026 07:57
rurban added a commit that referenced this pull request Nov 15, 2026
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

blocking bug Something isn't working questionable

Projects

None yet

Development

Successfully merging this pull request may close these issues.

decode: object_ref realloc overflows when HANDSEED is not a multiple of REFS_PER_REALLOC

2 participants