Tcl Source Code

View Ticket
Login
Ticket UUID: 816913a65eabbd69b07be7221b68b10064103a0e
Title: GrowStringBuffer(): signed integer overflow
Type: Bug Created on: 2021-12-24 19:11:02
Submitter: chrstphrchvz Assigned to: jan.nijtmans
Subsystem: 10. Objects Severity: Minor
Priority: 5 Medium Last modified: 2022-01-14 22:49:30
Status: Closed Closed by: jan.nijtmans
Resolution: Fixed Closed on: 2022-01-14 22:49:30
Version: 8.6.12
Description:

In GrowStringBuffer() (generic/tclStringObj.c), it is possible for int attempt to be assigned INT_MAX, such as when the function is called with int needed = INT_MAX. When this happens, int attempt will also be assigned INT_MAX, and attempt + 1 will overflow (although I do not observe this causing unexpected behavior). UBSan (-fsanitize=signed-integer-overflow) error example:

% set dz [open /dev/zero [list RDONLY]]
% set nulls [read $dz [expr {(1 << 30) - 1}]]
tcl/generic/tclStringObj.c:167:20: runtime error: signed integer overflow: 2147483647 + 1 cannot be represented in type 'int'

If it is indeed okay to allocate (unsigned int)INT_MAX + 1U, then some possible ways to avoid overflow include declaring attempt as unsigned int, or changing instances of attempt + 1 to promote attempt to unsigned int (such as by using attempt + 1U).

User Comments:
jan.nijtmans added on 2022-01-13 17:32:06:

Possible fix committed [a03e9793d4f70960|here]