Skip to content

Fix Undefined Behavior in GridColumnLineNumber#531

Merged
arch1t3cht merged 1 commit intoTypesettingTools:masterfrom
filip-hejsek:ub_fix
Feb 4, 2026
Merged

Fix Undefined Behavior in GridColumnLineNumber#531
arch1t3cht merged 1 commit intoTypesettingTools:masterfrom
filip-hejsek:ub_fix

Conversation

@filip-hejsek
Copy link
Contributor

At one point during initialization, GridColumnLineNumber::Width() gets called when the event list is empty, and the function calls back() on the list, which is UB. The bug was found with UBSan:

/usr/include/boost/intrusive/pointer_traits.hpp:306:14: runtime error: downcast of address 0x7d4fddb58490 which does not point to an object of type 'AssDialogue'
0x7d4fddb58490: note: object has a possibly invalid vptr: abs(offset to top) too big
 6f 7c 00 00  b0 fa c8 dd 6f 7c 00 00  b0 fa c8 dd 6f 7c 00 00  d8 e4 71 60 55 55 00 00  10 51 d5 dd
              ^~~~~~~~~~~~~~~~~~~~~~~
              possibly invalid vptr

Fix this by instead returning the width of the string "1" when the list is empty.

Calling back() on an empty list is UB. Most of the time, we ensure that
there is at least one event, but at one point during initialization, the
function gets called when the list is still empty.

In practice, back() would return a garbage pointer from which we would
then read garbage row number and use it to calculate nonsense width.
This didn't cause any visible problem, because the width will get
recalculated after an event is inserted, but it's still incorrect.

Fix this by instead returning the width of the string "1" when the list
is empty.
@arch1t3cht
Copy link
Member

Thanks!

@arch1t3cht arch1t3cht merged commit 250eb85 into TypesettingTools:master Feb 4, 2026
@filip-hejsek filip-hejsek deleted the ub_fix branch February 5, 2026 01:32
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.

2 participants