Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2022-04-07
18:58 • New ticket [05ff16e799] signed integer overflow in ExtendStringRepWithUnicode()<... artifact: c22669188a user: chrstphrchvz
2022-03-24
01:01 • Ticket [0e8fd6c6d5] (unsigned)STRING_SIZE(STRING_MAXCHARS) is 0 status still... artifact: 58c547008c user: chrstphrchvz
2022-03-22
20:09 • Closed ticket [0e8fd6c6d5]. artifact: f588184979 user: jan.nijtmans
19:48
Fix [0e8fd6c6d5]: (unsigned)STRING_SIZE(STRING_MAXCHARS) is 0 (and optimize STRING_SIZE a little bit... check-in: 44ba6e4ce3 user: jan.nijtmans tags: core-8-6-branch
18:41 • Ticket [0e8fd6c6d5] (unsigned)STRING_SIZE(STRING_MAXCHARS) is 0 status still... artifact: 5bb96271ea user: chrstphrchvz
02:10 • Ticket [0e8fd6c6d5]: 3 changes artifact: bf5f854885 user: chrstphrchvz
02:02 • Add attachment string-maxchars-oboe.diff to ticket [0e8fd6c6d5fd2f29|0e... artifact: 86c3335bd7 user: chrstphrchvz
02:02 • New ticket [0e8fd6c6d5] (unsigned)STRING_SIZE(STRING_MAXCHARS) is 0. artifact: 58e788fd80 user: chrstphrchvz

(unsigned)STRING_SIZE(STRING_MAXCHARS) is 0

Closed / Fixed0e8fd6c6d5fd2f29f90c6e101197796951e399d6Created 5 years agoUpdated 5 years ago
Descriptionwiki2022-03-22 02:02:17
…causing stringAlloc(STRING_MAXCHARS) or similar to eventually lead to a crash or noticeable memory corruption. Example:
set a [string repeat a 1073741820]
# crash when attempting to create string with longest possible unicode rep and no string rep
set aa [string replace $a 0 0 $a ]

After TCLFLEXARRAY ([29540d10c4]) was introduced, STRING_MAXCHARS was updated ([3a6a0399ad]), but the …- 1… seems to be misplaced (it currently makes no difference in the numerator of the expression): see attached patch. STRING_MAXCHARS apparently should return 1 less than what it currently does (i.e. 2147483638 rather than 2147483639, for 32-bit int and 16-bit Tcl_UniChar).

To my knowledge there are no similar issues for other structures using TCLFLEXARRAY.

Referenced by2
  • c22669188aNew ticket [05ff16e799] signed integer overflow in ExtendStringRepWithUnicode(). ticket2022-04-07 18:58:01
  • 44ba6e4ce3Fix [0e8fd6c6d5]: (unsigned)STRING_SIZE(STRING_MAXCHARS) is 0 (and optimize STRING_SIZE a little bit) check-in2022-03-22 19:48:53
chrstphrchvzwiki2022-03-22 02:10:14

Just an observation: when built using Clang 13.0.1 AddressSanitizer (-fsanitize=address) on macOS, running this example triggers an invalid write in realloc(ptr, 0); the address is just before ptr, i.e. likely in the ASAN chunk header, so I wonder if this is an ASAN bug.

realloc(ptr, 0) (where ptr != NULL) is expected to become undefined behavior in C23, however. Should Tcl try preventing this from happening in e.g. Tcl_Realloc(), ckrealloc(), etc.?

chrstphrchvzwiki2022-03-22 18:41:41

The ASan flag allocator_frees_and_returns_null_on_realloc_zero is enabled by default, so this Tcl issue effectively triggers a double free. The problem seems to be that ASan is not detecting/reporting double frees for at least some large allocations.

jan.nijtmanswiki2022-03-22 20:09:07

Good catch! Should be fixed now.

Thanks for the report and the patch!

chrstphrchvzwiki2022-03-24 01:01:51
Log in (anonymous is fine) to add a comment.