Tk Source Code

View Ticket
Login
Ticket UUID: b2de5886c686a6ea82373efa7f3cc687eb3015c0
Title: Misleading comment in tkImgPhInstance.c
Type: Bug Version: all
Submitter: chrstphrchvz Created on: 2026-10-14 04:49:50
Subsystem: 41. Photo Images Assigned To: jan.nijtmans
Priority: 5 Medium Severity: Cosmetic
Status: Closed Last Modified: 2026-10-18 08:45:53
Resolution: Fixed Closed By: jan.nijtmans
    Closed on: 2026-10-18 08:45:53
Description:

In GetColorTable():

	/*
	 * The following line of code should not normally be needed due to the
	 * assignment in the following line. However, it compensates for bugs
	 * in some compilers (HP, for example) where sizeof(ColorTable) is 24
	 * but the assignment only copies 20 bytes, leaving 4 bytes
	 * uninitialized; these cause problems when using the id for lookups
	 * in imgPhotoColorHash, and can result in core dumps.
	 */

	memset(&colorPtr->id, 0, sizeof(ColorTableId));
	colorPtr->id = id;

I suggest removing or at least rewording this comment. The C standard does not require compilers to copy padding during struct assignment. Unless HP, etc. documented that their compilers would copy such padding (which I doubt they did), there were never any such compiler bugs.

User Comments: jan.nijtmans added on 2026-10-18 08:45:53:

Now fixed in [01c4ab26997c6d61|trunk], core-9-0-branch and core-8-6-branch.

> Replacing the second `memset()` call and assignment with `memcpy()` as Jan proposed does seem better

Thanks!


chrstphrchvz added on 2026-10-17 07:07:24:

The comment also has a typo: it meant to say sizeof(ColorTableId) rather than sizeof(ColorTable).

I am trying to understand exactly what prompted Marc's comment. Here is the relevant excerpt from tk4.0p1.patch (the first post-release bugfix patch to Tk 4.0), if it helps provide context:

*** 2399,2404 ****
--- 2402,2419 ----
  	 */
  
  	colorPtr = (ColorTable *) ckalloc(sizeof(ColorTable));
+ 
+ 	/*
+ 	 * The following line of code should not normally be needed due
+ 	 * to the assignment in the following line.  However, it compensates
+ 	 * for bugs in some compilers (HP, for example) where
+ 	 * sizeof(ColorTable) is 24 but the assignment only copies 20 bytes,
+ 	 * leaving 4 bytes uninitialized;  these cause problems when using
+ 	 * the id for lookups in imgPhotoColorHash, and can result in
+ 	 * core dumps.
+ 	 */
+ 
+ 	memset((VOID *) &colorPtr->id, 0, sizeof(ColorTableId));
  	colorPtr->id = id;
  	colorPtr->flags = 0;
  	colorPtr->refCount = 0;

Both the comment (whose author expected colorPtr->id = id to always copy padding) and the memset() call were added simultaneously; "leaving 4 bytes uninitialized" was describing previous behavior. memset() was not what was failing to initialize memory, nor was memset() foreseeing how memory would be used, nor was sizeof() failing to account for padding.

ยง6.2.6.1 of the C standard has said:

When a value is stored in an object of structure or union type, including in a member object, the bytes of the object representation that correspond to any padding bytes take unspecified values (e.g. structure and union assignment can fail to copy any padding bits).

The part in parentheses is what prompted this ticket. But now I also think this paragraph means that after assigning to all members of id, its padding does not have to remain zeroed (in which case a compiler can optimize away the existing memset() calls in GetColorTable()). Replacing the second memset() call and assignment with memcpy() as Jan proposed does seem better. I would say Tk is still relying on nonportable behavior after the first memset() call, but that is arguing against it working perfectly fine for the past 31 years and continuing to do so for the vast majority of users (because ColorTableId has no padding on their 64-bit systems).


marc_culler (claiming to be Marc Culler) added on 2026-10-16 21:07:55:
I really don't believe that allocating N bytes and using memset to set
those N bytes to 0 could possibly fail to clear all of the allocated
memory.  I don't see how memset could possibly know anything about what
sort of struct is going to eventually be copied into the newly allocated
block of memory.  And I don't believe that sizeof would ever fail to
account for any padding needed for alignment.  I think that doing the
straightforward thing and removing the comment would work fine.  Since
there is no allocation function like calloc provided by Tcl, I think
allocating the memory and clearing it with memset is a perfectly standard
thing to do, and I also see that it is done throughout the generic Tk code.

jan.nijtmans added on 2026-10-15 10:09:54:

Thinking more on it, [3939c8d2f32d6ccc|this] is better.

In stead of initializing the whole (large) struct, just use memcpy() to make sure the (eventual) padding bits are copied as well.

On modern systems, there will be padding between `gamma` and `palette`, we - indeed - have to make sure those padding bytes are initialized.


jan.nijtmans added on 2026-10-15 10:02:22:

How about [47acbe2ae9|this]?

I agree that the described behavior is no bug. When using a struct as hash key, we need to assure that all bytes are initialized.

Therefore, I propose to initialize all of the ColorTable in a single `memset`, and just initialize the two fields (id and visualInfo). Or would that be too much?