| Ticket UUID: | 41b1406f515e092067103439e63caad5843039e3 | ||
| Title: | AppendUtfToUtfRep(): undefined behavior involving pointers | ||
| Type: | Bug | Created on: | 2021-12-12 10:35:02 |
| Submitter: | chrstphrchvz | Assigned to: | jan.nijtmans |
| Subsystem: | 44. UTF-8 Strings | Severity: | Minor |
| Priority: | 5 Medium | Last modified: | 2021-12-16 07:58:52 |
| Status: | Closed | Closed by: | jan.nijtmans |
| Resolution: | Fixed | Closed on: | 2021-12-16 07:58:52 |
| Version: | 8.6.12 | ||
| Description: | ||||
|
AppendUtfToUtfRep() (generic/tclStringObj.c) tries to check if the source string bytes is within the destination string buffer objPtr->bytes, since extending the destination string’s buffer may move the source string and require updating bytes:
if (bytes && bytes >= objPtr->bytes
&& bytes <= objPtr->bytes + objPtr->length) {
offset = bytes - objPtr->bytes;
}
The pointer arithmetic in the range check is performed even when objPtr->bytes is NULL, which is undefined behavior. UBSan (-fsanitize=pointer-arithmetic) complains:
$ ./tcltest ../tests/stringObj.test
tcl/generic/tclStringObj.c:1741:29: runtime error: applying zero offset to null pointer
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior tcl/generic/tclStringObj.c:1741:29 in
stringObj.test: Total 68 Passed 68 Skipped 0 Failed 0
Also, I believe the range check resembles the problem described in https://devblogs.microsoft.com/oldnewthing/20170927-00/?p=97095: either the pointers are for the same buffer and the check is always true, or the pointers are to different buffers and the check is undefined behavior. If this is the case, I’m not aware of a good workaround: the suggestion of casting to uintptr_t requires C99; and at least one caller of AppendUtfToUtfRep(), Tcl_AppendLimitedToObj(), is a public API which has no knowledge of whether its bytes argument points to the same buffer as objPtr->bytes. See https://core.tcl-lang.org/tk/tktview?name=156e58bc6e for a similar issue. | ||||
| User Comments: | ||||
jan.nijtmans added on 2021-12-13 13:43:26:
I think the "runtime error" is fixed [3f70533e6d35fa9f|here]. About the pointer arithmetic, I don't think very much can be done. I you have an idea, feel free to re-open this issue. For now, it's best to just close it as "fixed". Explanation: if objPtr->bytes is NULL, it means that there is no byte representation yet, so GrowBuffer() will create a new buffer. This also means that "bytes" can never point to somewhere in the new buffer, so the whole extra processing can just be skipped. Thanks for the report! jan.nijtmans added on 2021-12-15 16:59:48:
Little addendum [8f432c79cf52b9af|here]: the '<=' in the range-check should be '<' chrstphrchvz added on 2021-12-15 22:12:10:
Re addendum: I too had wondered if < should be used instead of <=, but ultimately thought <= is correct once I noticed that objPtr->length doesn’t include the terminating NUL, which bytes could be pointing to; even though numBytes (the length to append) should be 0 in such a case, causing the function to return early and not perform any arithmetic on a pointer to unowned memory (due to bytes not being recomputed after the source/destination buffer is moved). jan.nijtmans added on 2021-12-16 07:58:52:
Indeed, better keep the '<=' as it was. Thanks for the explanation! | ||||
