Skip to content
Tcl Source Code History Of Ticket 41b1406f515e0920
Login

History Of Ticket 41b1406f515e0920

Artifacts Associated With Ticket 41b1406f515e0920

  1. Ticket change [595ba0ab03] (rid 196787) by chrstphrchvz on 2021-12-12 10:35:02:

    1. assignee initialized to: "nobody"
    2. closer initialized to: "nobody"
    3. cmimetype initialized to: "text/x-fossil-wiki"
    4. comment initialized to:
      <tt>AppendUtfToUtfRep()</tt> (generic/tclStringObj.c) tries to check if the source string <tt>bytes</tt> is within the destination string buffer <tt>objPtr-&gt;bytes</tt>, since extending the destination string’s buffer may move the source string and require updating <tt>bytes</tt>:
      
      <pre>
      	if (bytes && bytes >= objPtr->bytes
      		&& bytes <= <span style="font-weight:bold">objPtr->bytes + objPtr->length</span>) {
      	    offset = bytes - objPtr->bytes;
      	}
      </pre>
      
      The pointer arithmetic in the range check is performed even when <tt>objPtr-&gt;bytes</tt> is <tt>NULL</tt>, which is undefined behavior. [https://clang.llvm.org/docs/UndefinedBehaviorSanitizer.html#available-checks|UBSan (<tt>-fsanitize=pointer-arithmetic</tt>)] complains:
      
      <pre>
      $ ./tcltest ../tests/stringObj.test 
      <span style="font-weight:bold">tcl/generic/tclStringObj.c:1741:29: <span style="color:red">runtime error:</span> applying zero offset to null pointer</span>
      SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior tcl/generic/tclStringObj.c:1741:29 in 
      stringObj.test:	Total	68	Passed	68	Skipped	0	Failed	0
      </pre>
      
      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 <tt>uintptr_t</tt> requires C99; and at least one caller of <tt>AppendUtfToUtfRep()</tt>, <tt>Tcl_AppendLimitedToObj()</tt>, is a public API which has no knowledge of whether its <tt>bytes</tt> argument points to the same buffer as <tt>objPtr-&gt;bytes</tt>.
      
      See [https://core.tcl-lang.org/tk/tktview?name=156e58bc6e] for a similar issue.
      
    5. foundin initialized to: "8.6.12"
    6. is_private initialized to: "0"
    7. login: "chrstphrchvz"
    8. priority initialized to: "5 Medium"
    9. resolution initialized to: "None"
    10. severity initialized to: "Minor"
    11. status initialized to: "Open"
    12. submitter initialized to: "chrstphrchvz"
    13. subsystem initialized to: "44. UTF-8 Strings"
    14. title initialized to:
      AppendUtfToUtfRep(): undefined behavior involving pointers
      
    15. type initialized to: "Bug"
  2. Ticket change [f820b3c0cd] (rid 196808) by jan.nijtmans on 2021-12-13 13:43:26:

    1. assignee changed to: "jan.nijtmans"
    2. closedate changed to: "2459562.07183899"
    3. closer changed to: "jan.nijtmans"
    4. icomment:
      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!
      
    5. login: "jan.nijtmans"
    6. mimetype: "text/x-fossil-wiki"
    7. resolution changed to: "Fixed"
    8. status changed to: "Closed"
  3. Ticket change [3cc49d8745] (rid 196814) by jan.nijtmans on 2021-12-15 16:59:48:

    1. closedate changed to: "2459564.20820399"
    2. icomment:
      Little addendum [8f432c79cf52b9af|here]: the '<=' in the range-check should be '<'
      
    3. login: "jan.nijtmans"
    4. mimetype: "text/x-fossil-wiki"
  4. Ticket change [3cf8450693] (rid 196819) by chrstphrchvz on 2021-12-15 22:12:10:

    1. closedate changed to: "2459564.42511973"
    2. closer changed to: "chrstphrchvz"
    3. icomment:
      Re addendum: I too had wondered if <tt>&lt;</tt> should be used instead of <tt>&lt;=</tt>, but ultimately thought <tt>&lt;=</tt> is correct once I noticed that <tt>objPtr-&gt;length</tt> doesn’t include the terminating NUL, which <tt>bytes</tt> could be pointing to; even though <tt>numBytes</tt> (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 <tt>bytes</tt> not being recomputed after the source/destination buffer is moved).
      
    4. login: "chrstphrchvz"
    5. mimetype: "text/x-fossil-wiki"
  5. Ticket change [15bf986b1d] (rid 196823) by jan.nijtmans on 2021-12-16 07:58:52:

    1. closedate changed to: "2459564.83255172"
    2. closer changed to: "jan.nijtmans"
    3. icomment:
      Indeed, better keep the '<=' as it was. Thanks for the explanation!
      
    4. login: "jan.nijtmans"
    5. mimetype: "text/x-fossil-wiki"