Tcl Source Code

View Ticket
Login
Ticket UUID: bca10e3790f6c2962c0302d6f62ef9c6fadf5752
Title: Undefined behavior in ResultAdd()
Type: Bug Created on: 2021-12-28 23:11:04
Submitter: chrstphrchvz Assigned to: jan.nijtmans
Subsystem: 52. Portability Support Severity: Minor
Priority: 5 Medium Last modified: 2022-01-14 22:49:53
Status: Closed Closed by: jan.nijtmans
Resolution: Fixed Closed on: 2022-01-14 22:49:53
Version: 8.6.12
Description:

ResultAdd() (generic/tclIOGT.c) is sometimes called with size_t toWrite = 0 and a ResultBuffer *r such that r->buf = NULL. In this case, r->used = 0 and r->allocated = 0, so ResultAdd() will not allocate a new buffer. This leads to NULL + 0 pointer arithmetic when doing r->buf + r->used, and calling memcpy() with NULL as the destination (even though the copy length toWrite is 0), both of which are undefined behavior. UBSan -fsanitize=nonnull-attribute (GCC) and -fsanitize=pointer-overflow errors during test iogt-1.1:

tcl/generic/tclIOGT.c:1445:5: runtime error: null pointer passed as argument 1, which is declared to never be null
tcl/generic/tclIOGT.c:1445:12: runtime error: applying zero offset to null pointer

User Comments:
jan.nijtmans added on 2022-01-13 11:41:58:

Suggested fix [acb2cec2bd31723a|here]. This also makes various functions in tclIOGT.c and tclIOTrans.c equal (since the ResultAdd() function in tclIOTrans.c didn't have this problem)