|
2021-12-16
| ||
| 08:03 | Undo prevous commit. See [41b1406f51] for the reason check-in: 5286286df9 user: jan.nijtmans tags: core-8-6-branch | |
| 07:58 | • Ticket [41b1406f51] AppendUtfToUtfRep(): undefined behavior involving pointers</... artifact: 15bf986b1d user: jan.nijtmans | |
|
2021-12-15
| ||
| 22:12 | • Ticket [41b1406f51]: 5 changes artifact: 3cf8450693 user: chrstphrchvz | |
| 16:59 | • Ticket [41b1406f51]: 4 changes artifact: 3cc49d8745 user: jan.nijtmans | |
| 16:58 | Addendum to [41b1406f51]: change '<=' in range-check to '<' check-in: 8f432c79cf user: jan.nijtmans tags: core-8-6-branch | |
|
2021-12-13
| ||
| 13:43 | • Closed ticket [41b1406f51]: AppendUtfToUtfRep(): undefined behavior involving po... artifact: f820b3c0cd user: jan.nijtmans | |
| 11:39 | Fix [41b1406f51]: AppendUtfToUtfRep(): undefined behavior involving pointers check-in: 3f70533e6d user: jan.nijtmans tags: core-8-6-branch | |
|
2021-12-12
| ||
| 10:35 | • New ticket [41b1406f51] AppendUtfToUtfRep(): undefined behavior involving pointe... artifact: 595ba0ab03 user: chrstphrchvz | |
AppendUtfToUtfRep(): undefined behavior involving pointers
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.
5286286df9Undo prevous commit. See [41b1406f51] for the reason check-in2021-12-16 08:03:398f432c79cfAddendum to [41b1406f51]: change '<=' in range-check to '<' check-in2021-12-15 16:58:083f70533e6dFix [41b1406f51]: AppendUtfToUtfRep(): undefined behavior involving pointers check-in2021-12-13 11:39:15
I think the "runtime error" is fixed [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!
Little addendum [here]: the '<=' in the range-check should be '<'
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).
Indeed, better keep the '<=' as it was. Thanks for the explanation!