-
Notifications
You must be signed in to change notification settings - Fork 351
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
C impl: Prevent memcpy undefined behavior #4
Conversation
Thanks for catching this. I probably won't have time to merge it today, but I should get to it tomorrow. For now, I'll note that this code is currently only used to test the other C code; the Rust crate does not build this file. |
I'm not sure that's undefined behavior (but I'm also not confident it isn't). From C18, § 7.24.1 "String handling" > "String function conventions", (2):
A few paragraphs later, § 7.24.2.1, "The memcpy function", (2):
I don't really understand how any valid implementation of memcpy could produce UB regardless of |
@cemeyer take a look at https://www.imperialviolet.org/2016/06/26/nonnull.html |
@oconnor663 It's a great write-up. I think perhaps Langley and definitely the glibc authors have interpreted §7.1.4 differently than I have. The language only says,
The sentence includes other examples clearly irrelevant to string functions, such as mathematical values out of domain. So I believe the "such as" language is intended to provide only examples of possible invalid argument values, rather than a list of values which are always invalid. Indeed, some of the So the question I think is, is Similarly, it is valid for In response to Langley's example of glibc, As Langley goes on to describe, there is negligible to zero program code benefit from making this invocation UB, and potentially large downside. I think I'm largely on the same page with his Conclusions section.
💯 In summary: I am still not persuaded this is actually UB per the standard. However, please go ahead and make this change to defend against the glibc/GCC implementation-specific foot-gun. |
Merged and followed on with d7d71b2. |
This prevents a (potential, depending on the caller) case of undefined behavior in the call to
memcpy
inchunk_state_fill_buf
. This can happen whenblake3_hasher_update
is called withinput=NULL
andinput_len=0
. Passing a null pointer to memcpy is undefined behavior even if then
argument is 0.Empy C++ vectors (can) behave this way, eg.: