Skip to content

Fix undefined behavior reported by UBSan - #513

Merged
jow- merged 2 commits into
masterfrom
fix/ubsan-findings
Oct 28, 2026
Merged

jow- merged 2 commits into
masterfrom
fix/ubsan-findings

Conversation

@jow-

@jow- jow- commented Oct 28, 2026

Copy link
Copy Markdown
Collaborator

Fix two undefined behavior (UB) instances that UBSan reports at runtime, each found while executing
the respective code path:

1. debug_proto: skip the line search of an empty buffer (lib/debug_proto.c)

A debug protocol buffer starts out empty with no data allocated, and the first read may deliver nothing. Searching
such an empty buffer for the newline of a complete line passed a null pointer to memchr(), which is declared nonnull
— undefined even though the search cannot match anything:

runtime error: null pointer passed as argument 1, which is declared to never be null

The fix only performs the line search when something has actually been buffered.

2. compiler: look up scanned brackets with a bracket key (compiler.c)

Entries of the scanned bracket list are compared with uc_bracket_cmp(), which treats both arguments as uc_bracket_t
pointers. The search key of uc_compiler_literal_follow() was, however, a plain size_t position, so the comparator
read the follow member past the end of the key — reported by UBSan while compiling a destructuring assignment of an
array pattern:

compiler.c:2639:40: runtime error: member access within address ...
with insufficient space for an object of type 'const uc_bracket_t'

The fix passes a uc_bracket_t holding the position as the search key, which is what the comparator expects and what
the searched array holds.

jow- added 2 commits October 28, 2026 20:51
The entries of the scanned bracket list are compared with
uc_bracket_cmp(), which treats both of its arguments as uc_bracket_t
pointers.  The search key of uc_compiler_literal_follow() was the plain
size_t position however, so the comparison read the follow member past
the end of the key, which the undefined behaviour sanitizer reports while compiling a
destructuring assignment of an array pattern:

  compiler.c:2639:40: runtime error: member access within address
  0x7f8660f0bf40 with insufficient space for an object of type
  'const uc_bracket_t'

Pass a bracket holding the position as key instead, which is what the
comparator expects and what the searched array holds.

Signed-off-by: Jo-Philipp Wich <jo@mein.io>
A debug protocol buffer starts out empty, with no data allocated for it
yet, and the first read may deliver nothing.  Searching an empty buffer
for the newline of a complete line passed a null pointer to memchr(),
which is declared nonnull, which is undefined even though such a search
cannot match anything:

  runtime error: null pointer passed as argument 1, which is declared to
  never be null

Only look for a line when something has been buffered.

Signed-off-by: Jo-Philipp Wich <jo@mein.io>
@jow-
jow- merged commit 05c0668 into master Oct 28, 2026
3 checks passed
@jow-
jow- deleted the fix/ubsan-findings branch October 28, 2026 21:55
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.

1 participant