Skip to content
Tcl Source Code Ticket Change Details
Login

Ticket Change Details

Overview

Artifact ID: e8d6db452b6fe35db8fbfe0da1b09d75e53834b15c5abd029a5dead690f51555
Ticket: cea2c63928b19c7ade84cbb5304cc2a33514912a
Address -fsanitize=shift-exponent complaints in TclParseNumber()
User & Date: chrstphrchvz on 2021-12-06 08:17:56
Changes

  1. assignee changed to: "nobody"
  2. closer changed to: "nobody"
  3. cmimetype changed to: "text/x-fossil-wiki"
  4. comment changed to:
    (Note that I do not observe Tcl behaving unexpectedly despite this complaint.)
    
    The C standard (at least since C89) says that shifting by the width of the left operand (after promotion) or more is undefined behavior. No exception is made for the left operand being 0. So I believe [https://clang.llvm.org/docs/UndefinedBehaviorSanitizer.html#available-checks|UBSan (<tt>-fsanitize=shift-exponent</tt>)] is correct to complain about certain shifts in <tt>TclParseNumber()</tt> (generic/tclStrToD.c) when parsing numbers containing enough leading zeros. Example complaints for 64-bit <tt>Tcl_WideUInt</tt> (note that they may only appear once over the lifetime of the interpreter):
    
    <pre>
    # tcl/generic/tclStrToD.c:714:27: runtime error: shift exponent 66 is too large for 64-bit type 'Tcl_WideUInt' (aka 'unsigned long')
    scan "[join [lrepeat 22 0] {}]1" {%i}
    
    # tcl/generic/tclStrToD.c:714:27: runtime error: shift exponent 66 is too large for 64-bit type 'Tcl_WideUInt' (aka 'unsigned long')
    scan "[join [lrepeat 22 0] {}]1" {%o}
    
    # tcl/generic/tclStrToD.c:830:42: runtime error: shift exponent 64 is too large for 64-bit type 'Tcl_WideUInt' (aka 'unsigned long')
    scan "[join [lrepeat 16 0] {}]1" {%x}
    
    # tcl/generic/tclStrToD.c:872:42: runtime error: shift exponent 64 is too large for 64-bit type 'Tcl_WideUInt' (aka 'unsigned long')
    scan "[join [lrepeat 64 0] {}]1" {%b}
    
    # tcl/generic/tclStrToD.c:1205:23: runtime error: shift exponent 64 is too large for 64-bit type 'Tcl_WideUInt' (aka 'unsigned long')
    expr "0b[join [lrepeat 64 0] {}]"
    
    # tcl/generic/tclStrToD.c:1226:23: runtime error: shift exponent 64 is too large for 64-bit type 'Tcl_WideUInt' (aka 'unsigned long')
    expr "0x[join [lrepeat 16 0] {}]"
    
    # tcl/generic/tclStrToD.c:1248:28: runtime error: shift exponent 66 is too large for 64-bit type 'Tcl_WideUInt' (aka 'unsigned long')
    expr "[join [lrepeat 23 0] {}]"
    
    # tcl/generic/tclStrToD.c:1248:28: runtime error: shift exponent 66 is too large for 64-bit type 'Tcl_WideUInt' (aka 'unsigned long')
    expr "0o[join [lrepeat 22 0] {}]"
    </pre>
    
    <hr />
    To address these, I would replace e.g.:
    <pre>
    octalSignificandWide = (octalSignificandWide << shift) + (c - '0')
    </pre>
    
    with:
    <pre>
    /*
     * When the significand is 0, it is possible for the
     * amount to be shifted to equal or exceed the width
     * of the significand. Do not shift when the
     * significand is 0 to avoid undefined behavior. 
     */
    
    if (octalSignificandWide != 0) {
        octalSignificandWide &lt;&lt;= shift;
    }
    octalSignificandWide += c - '0';
    </pre>
    
    I also suggest rewording the current comment for the too-large shift check (which appeared in [57a68b7c98]), because it is undefined behavior (not merely “de facto nonportable”) to shift, not just by more than width of a value, but also by the exact width of a value.
    
    See attached patch.
    
  5. foundin changed to: "8.6.12"
  6. is_private changed to: "0"
  7. login: "chrstphrchvz"
  8. priority changed to: "5 Medium"
  9. resolution changed to: "None"
  10. severity changed to: "Minor"
  11. status changed to: "Open"
  12. submitter changed to: "chrstphrchvz"
  13. subsystem changed to: "11. Conversions from String"
  14. title changed to:
    Address -fsanitize=shift-exponent complaints in TclParseNumber()
    
  15. type changed to: "Patch"