-
Notifications
You must be signed in to change notification settings - Fork 365
Add NUL character at the end of copied error message #1179
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
Conversation
C++ error message returned by `what` must be NUL-terminated. However, the current copy function only copied the characters, but didn't add the NUL. Allocate one more byte and set it to NUL.
BTW, here is the issue fixed by this (from Valgrind):
@dtolnay Maybe Valgrind tests should be added in the CI? No idea how hard that is. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is correct as currently implemented.
The valgrind output you pasted appears to be from code that is not in this repository. If you could share code that reproduces an out of bounds read using what is currently in the repository, I can have another look.
Hm, well, OK, let's assume you get the message "test" from Rust. That is, pointer to 4 characters "test" + len = 4. You allocate 4 bytes and copy the 4 characters "test" into those 4 bytes. Now, the exception is thrown and caught in the C++ code (via Care to reconsider? 🙂 |
For "test", the len passed into this function is 5, not 4, so the memcpy already includes the original \0 byte. |
Sorry for making a fool of myself. You are right, the NUL byte is pushed in The valgrind output was from the change required for handling C++ exceptions via The original code allocated at least once extra for the Anyway, the changes in the other PR are self-contained and valgrind-verified, so this change is indeed not needed. |
C++ error message returned by
what
must be NUL-terminated. However, the current copy function only copied the characters, but didn't add the NUL. Allocate one more byte and set it to NUL.