Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2022-03-11
19:15
[9c7557160f] Alternative fix for UB handling of int overflow. check-in: 601a2b1dfe user: dgp tags: core-8-6-branch
17:21 • Ticket [9c7557160f] signed integer overflow in UpdateStringOfByteArray() sta... artifact: 8d8dca38f4 user: jan.nijtmans
16:49 • Ticket [9c7557160f]: 5 changes artifact: 7fd0982c5d user: dgp
2022-03-08
15:33 • Closed ticket [9c7557160f]. artifact: 95f8077137 user: jan.nijtmans
15:23
Fix [9c7557160]: signed integer overflow in UpdateStringOfByteArray() check-in: 5e793d379a user: jan.nijtmans tags: core-8-6-branch
2022-02-18
20:07 • Add attachment usoba.diff to ticket [9c7557160f] artifact: 7044e8d703 user: chrstphrchvz
20:07 • New ticket [9c7557160f] signed integer overflow in UpdateStringOfByteArray()... artifact: d29e03a746 user: chrstphrchvz

signed integer overflow in UpdateStringOfByteArray()

Closed / Fixed9c7557160fd1fb402aa1eb3ec4b358cfa79d596fCreated 5 years agoUpdated 5 years ago
Descriptionwiki2022-02-18 20:07:21
int size overflowing when incremented is undefined behavior. Example script and output showing UBSan (-fsanitize=signed-integer-overflow) error:
set f [open /dev/zero [list RDONLY BINARY]]
fconfigure $f -buffersize [expr {2**20}]
set z [read $f [expr {2**30}]]
puts [::tcl::mathop::ni $z [list]]
tcl/generic/tclBinary.c:568:10: runtime error: signed integer overflow: 2147483647 + 1 cannot be represented in type 'int'
max size for a Tcl value (2147483647 bytes) exceeded
(tclsh aborted)

If size does not overflow, it could instead be INT_MAX, causing overflow in ckalloc(size + 1). Example script and output:

set f [open /dev/zero [list RDONLY BINARY]]
fconfigure $f -buffersize [expr {2**20}]
set a [encoding convertto utf-8 a]
set z [read $f [expr {2**30 - 1}]]
set az [string cat $a $z]
puts [::tcl::mathop::ni $az [list]]
tcl/generic/tclBinary.c:575:19: runtime error: signed integer overflow: 2147483647 + 1 cannot be represented in type 'int'
1

Casting size to unsigned int avoids this; see attached patch. (There could be alternatives such as declaring size as unsigned int.)

Referenced by1
jan.nijtmanswiki2022-03-08 15:33:47

Fixed [here]. Thanks!

dgpplain2022-03-11 16:49:11
Alternative patch to consider found on bug-9c7557160f branch.

Leaves behind fewer typecasts for future code readers to puzzle over.
jan.nijtmanswiki2022-03-11 17:21:28

@dgp, I like your alternative! Just go ahead.

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