Fix ASAN errors due adding offset to nullptr#240
Fix ASAN errors due adding offset to nullptr#240stevengj merged 3 commits intoJuliaStrings:masterfrom
Conversation
| if (!dst){ | ||
| written += utf8proc_decompose_char(entry_cp, dst, | ||
| (bufsize > written) ? (bufsize - written) : 0, options, | ||
| last_boundclass); | ||
| }else{ | ||
| written += utf8proc_decompose_char(entry_cp, dst+written, | ||
| (bufsize > written) ? (bufsize - written) : 0, options, | ||
| last_boundclass); | ||
| } | ||
|
|
There was a problem hiding this comment.
Maybe cleaner to combine into a single call with a dst ? ... argument
| if (!dst){ | |
| written += utf8proc_decompose_char(entry_cp, dst, | |
| (bufsize > written) ? (bufsize - written) : 0, options, | |
| last_boundclass); | |
| }else{ | |
| written += utf8proc_decompose_char(entry_cp, dst+written, | |
| (bufsize > written) ? (bufsize - written) : 0, options, | |
| last_boundclass); | |
| } | |
| written += utf8proc_decompose_char(entry_cp, | |
| dst ? dst+written : dst, | |
| (bufsize > written) ? (bufsize - written) : 0, options, | |
| last_boundclass); |
|
(Note that the ASAN warning is harmless in this case, since |
Actually it's not just about the warning. I have noticed runtime error caused due to this that makes the program crash. |
This reverts commit b933f42.
Could you elaborate? I don't see any |
|
@stevengj This is my rough understanding what's going on: When utf8proc_decompose_custom is called here Line 736 in 63f31c9 applying non-zero offset 4 to null pointer. By incorporating changes in this PR, I could get rid of these run-time errors.
|
|
That sounds like a runtime error caused by ASAN. Applying a nonzero offset to a null pointer is perfectly safe if you never dereference the pointer. |
Answers to this question suggest that it's a UB. Although I'm not sure how come NULL pointer is "a pointer into, or just beyond, an array object", but clang generates an undefined behaviour error for this which suggest it might be a valid interpretation. |
|
Running into similar issues while writing a Zig compilation for utf8proc. Having to compile with There are other places in the library that use null pointer offsets. If this PR is approved, I can work on a follow up. |
|
Memory access violations in C string processing libraries are a major source of bugs. Thus tools like ASAN and MSAN are very important. I think we should merge this PR and avoid all other similar C standard violations. |
|
Fair enough, it's good to allow users to use these tools, and we're going to get bombarded with complaints if we violate their requirements (even if our violations are harmless in practice). |
This PR fixes the error when Address Sanitization is enabled. The offset is added to nullptr which gives following runtime error:
The PR checks whether the pointer is nullptr, and if so do not add offset to it.