Tk Source Code

View Ticket
Login
Ticket UUID: dacd18294b598975f4a557eb4028e16183973908
Title: Undefined behavior in tkTextBTree.c (out of bounds access in array)
Type: Bug Version: core-8-6-branch, trunk
Submitter: fvogel Created on: 2024-06-12 21:08:42
Subsystem: 18. [text] Assigned To: jan.nijtmans
Priority: 5 Medium Severity: Minor
Status: Closed Last Modified: 2024-06-14 06:55:51
Resolution: Fixed Closed By: jan.nijtmans
    Closed on: 2024-06-14 06:55:51
Description:

On macOS, with CFLAGS='-DPURIFY -fsanitize=address -fsanitize=undefined', when configuring with --enable-symbols=mem --enable-aqua and building with clang, ASan detects undefined behaviors in tkTextBTree.c.

To reproduce run for instance make test TESTFLAGS="-file bell.test -verbose bepst"

[...]
Only running test files that match:  bell.test
Tests began at Wed Jun 12 22:55:25 CEST 2024
bell.test
/Volumes/Users/fvogel/Documents/tcltk/fossil/tk/unix/../generic/tkTextBTree.c:1082:2: runtime error: index 8 out of bounds for type 'char[3]'
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior /Volumes/Users/fvogel/Documents/tcltk/fossil/tk/unix/../generic/tkTextBTree.c:1082:2 in
/Volumes/Users/fvogel/Documents/tcltk/fossil/tk/unix/../generic/tkTextBTree.c:4632:31: runtime error: index 6 out of bounds for type 'char[3]'
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior /Volumes/Users/fvogel/Documents/tcltk/fossil/tk/unix/../generic/tkTextBTree.c:4632:31 in
/Volumes/Users/fvogel/Documents/tcltk/fossil/tk/unix/../generic/tkTextBTree.c:4633:5: runtime error: index 7 out of bounds for type 'char[3]'
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior /Volumes/Users/fvogel/Documents/tcltk/fossil/tk/unix/../generic/tkTextBTree.c:4633:5 in
---- bell-1.1 start
++++ bell-1.1 PASSED
---- bell-1.2 start
++++ bell-1.2 PASSED
[...]

The index in array segPtr->body.chars[] is found to be larger than the array size (which is TCL_UTF_MAX, that is 3, in core-8-6-branch).

This problem itself is not specific to macOS, only the tool showing it is specific to macOS.

User Comments: jan.nijtmans added on 2024-06-14 06:55:51:

Merged to all brances now.

Also added the comment that TKFLEXARRAY doesn't work on c99 compilers insize a union, that's why it isn't used here.


fvogel added on 2024-06-13 20:23:27:

Yes, indeed [5628e780] removes this false positive (I agree it is a false positive).

Why don't we use TKFLEXARRAY BTW? Should a comment be added in the TkTextSegment struct to explain this?


jan.nijtmans added on 2024-06-13 08:06:18:

Does [5628e7807f4773b5|this] help?

Actually, this is an USBSan false positive: since we always allocate enough memory depending on how much we need, any solution which shuts up USBSan is OK. ;-)

Adding a new macro for only one instance is overkill. Better point to this ticket for the reason why we don't use TKFLEXARRAY here, unfortunately.

Thanks!


fvogel added on 2024-06-13 06:01:44:

Researching this lead me to finding this is a duplicate of [3bc0f44ef3].

Declaring char chars[TCL_UTF_MAX] in the TkTextSegment struct makes no sense IMO: This array has nothing to do with TCL_UTF_MAX at all. I see there were several successive changes here: first [bed8018384], then [8847c81c47] and finally [6067cc55df] which is the current state.

Jan, would you be so kind to have another look please? This clutters the output of UBSan. Thanks!