Skip to content
Tcl Source Code View Ticket
Login

View Ticket

signed integer overflow in Tcl_ScanObjCmd()

Closed / Fixed7deeddb36f217dac87bc68cb69718a89e3c7d5d7Created 5 years agoUpdated 5 years ago
Descriptionwiki2022-02-16 08:57:16
Setting Tcl_WideInt wideValue to the highest value representable by Tcl_WideInt and then doing wideValue++ is signed integer overflow (undefined behavior).

UBSan (-fsanitize=signed-integer-overflow) error when Tcl_WideInt is long long:

% scan -0x18000000000000000 {%li}
tcl/generic/tclScan.c:926:13: runtime error: signed integer overflow: 9223372036854775807 + 1 cannot be represented in type 'long long int'
-9223372036854775808

Likewise when Tcl_WideInt is long:

% scan -0x18000000000000000 {%li}
tcl/generic/tclScan.c:926:13: runtime error: signed integer overflow: 9223372036854775807 + 1 cannot be represented in type 'Tcl_WideInt' (aka 'long')
-9223372036854775808

Increment only after casting to Tcl_WideUInt if wraparound behavior is desired (without e.g. -fwrapv); see attached patch.

jan.nijtmanswiki2022-02-16 14:34:58

Fixed [here]

Thanks for the report!

chrstphrchvzwiki2022-02-17 02:26:48

I now notice the WIDE_MIN and WIDE_MAX macros in tclPort.h; should those be used instead for consistency? See use-wide-macros.diff.

Log in (anonymous is fine) to add a comment.