-
Notifications
You must be signed in to change notification settings - Fork 136
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Empty slice manipulation triggers UBSAN by offsetting from a null pointer. #135
Comments
Thanks for the bug report. |
A Wuffs slice is a pointer-length pair. These two commits from last October (2023) changed Wuffs' representation of "an empty slice" from (null, 0) to (nonNull, 0), as suggested by the original bug report.
The The
In C (but not in C++), it's Undefined Behavior to compute
Coming back to today, Philippe Antoine [email protected] notes that I'm going to revert those top two commits, changing it back so that the idiomatic Wuffs representation of an empty slice is (null, 0) and that I'll also patch the C code generation for Wuffs' iterate loops so that the original bug report doesn't re-appear. |
Line numbers and snippets are from commit
ad7fda33dd5fa6243cf26df20bed1fbaccfa8601
, which is the current head of main.In C (as opposed to C++), It's undefined behavior to offset a null pointer by any offset at all, at least according to llvm's UBSAN. In at least one part of
wuffs-v0.4.c
, the end pointer of an empty slice is computed, triggering UB.Here's the top of a stack trace from apple clang 14.0.3's UBSAN, on arm, when running a simple program that decodes a png and exits:
My understanding of what's going on is that on line 57676, we do something with an empty slice:
which ultimately leads to UB at line 33620 (4 stack frames deeper):
in which v_p.ptr and the offset are both zero.
I'm not at all familiar with wuffs-the-language or its compiler, so I'm not sure what a proper fix would look like. However, I was able to eliminate the error in my own program by changing
to
in wuffs-v0.4.c.
The text was updated successfully, but these errors were encountered: