Artifacts Associated With Ticket cea2c63928
Ticket change [e8d6db452b] (rid 196730) by chrstphrchvz on 2021-12-06 08:17:56:
- assignee initialized to: "nobody"
- closer initialized to: "nobody"
- cmimetype initialized to: "text/x-fossil-wiki"
- comment initialized 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 <<= 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. - foundin initialized to: "8.6.12"
- is_private initialized to: "0"
- login: "chrstphrchvz"
- priority initialized to: "5 Medium"
- resolution initialized to: "None"
- severity initialized to: "Minor"
- status initialized to: "Open"
- submitter initialized to: "chrstphrchvz"
- subsystem initialized to: "11. Conversions from String"
- title initialized to:
Address -fsanitize=shift-exponent complaints in TclParseNumber()
- type initialized to: "Patch"
Add attachment "fix-ubsan-shift-exponent.diff" [cd46257305] (rid 196731) by chrstphrchvz on 2021-12-06 08:18:43.
Ticket change [e447eb0f68] (rid 196738) by jan.nijtmans on 2021-12-07 21:19:48:
- assignee changed to: "jan.nijtmans"
- closedate changed to: "2459556.38876082"
- closer changed to: "jan.nijtmans"
- icomment:
Thanks for the report! Should be fixed now in core-8-6-branch, core-8-branch and trunk
- login: "jan.nijtmans"
- mimetype: "text/x-fossil-wiki"
- resolution changed to: "Fixed"
- status changed to: "Closed"