Tk Source Code

View Ticket
Login
Ticket UUID: 499f8e3313ebf8a5dbe437c012e1ce96a2d494e0
Title: AdjustStartEndRefs(): avoid undefined realloc() call
Type: Patch Version: 8.6.13
Submitter: chrstphrchvz Created on: 2023-11-04 08:15:47
Subsystem: 18. [text] Assigned To: fvogel
Priority: 5 Medium Severity: Minor
Status: Closed Last Modified: 2023-11-06 07:00:09
Resolution: Fixed Closed By: fvogel
    Closed on: 2023-11-06 07:00:09
Description:

Calling realloc(ptr, 0) with ptr != NULL is undefined behavior in C23, and is already non-portable. Unless Tcl is built without USE_TCLALLOC (such as for PURIFY builds), then to my knowledge ckrealloc() will never cause realloc() to be called with size 0 (due to space allocated for chunk headers).

The attached patch avoids an instance of this issue, which can triggered by various text widget tests for a PURIFY build, and is so far the only instance I am aware of in Tk 8.6.

User Comments: fvogel added on 2023-11-06 07:00:09:
OK, very good. Thanks for the feedback!

The patch is now merged in core-8-6-branch and forward.

chrstphrchvz added on 2023-11-06 06:27:19:

I didn't look at each call to realloc() in the Tk code to verify whether this is the only place to check for size 0 reallocs. I understand you already did this.

I have only glanced at other ckrealloc() usage in Tk 8.6, but it seemed to always use some positive count or additional constant amount of space, or be followed by code which assumes positive count.

In case count is zero we are now freeing the memory. I'm not sure where this memory is allocated again, but I didn't research this…

AdjustStartEndRefs() would allocate memory again under the TEXT_ADD_REFS case.

Do you know if there are such issues with the revised text widget…?
I have not carefully looked through revised_text for any instances, but the test suite does not trigger any.
I'm not sure how the problem can be reproduced, I couldn't trigger any issue with a PURIFY build…

This issue is currently not reported by e.g. UBSan, so I check for instances of this issue by printing a warning from, or setting a breakpoint in, the non-USE_TCLALLOC implementation of TclpRealloc():

--- generic/tclAlloc.c.orig
+++ generic/tclAlloc.c
@@ -744,6 +744,8 @@ TclpRealloc(
     char *oldPtr,              /* Pointer to alloced block. */
     unsigned int numBytes)     /* New size of memory. */
 {
+    if (oldPtr && !numBytes)
+       fprintf(stderr, "realloc(ptr, 0) called\n");
     return (char *) realloc(oldPtr, numBytes);
 }
 


fvogel added on 2023-11-05 17:06:27:

Thank you for the report and for the patch! I have committed it in a bugfix branch as [8ef8e22351]. I'm not sure how the problem can be reproduced, I couldn't trigger any issue with a PURIFY build (with or without TCL_MEM_DEBUG set).

Questions:

  • I didn't look at each call to realloc() in the Tk code to verify whether this is the only place to check for size 0 reallocs. I understand you already did this.
  • In case count is zero we are now freeing the memory. I'm not sure where this memory is allocated again, but I didn't research this, hoping you already did. Anyway with the patch the test suite doesn't show any issue.
  • Do you know if there are such issues with the revised text widget (in branch tip-466, to be built with the Tcl core-8-branch)?

Thanks again!


Attachments: