Add NUL character at the end of copied error message#1179
Add NUL character at the end of copied error message#1179schreter wants to merge 1 commit intodtolnay:masterfrom
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. |
dtolnay
left a comment
There was a problem hiding this comment.
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
whatmust 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.