possible over-allocation on 64-bit platforms
Closed / Fixed3129448fffffffffffffffffffffffffffffffffCreated 16 years agoUpdated 16 years ago
Descriptionplain2010-12-06 08:44:52
Inspired by bug #3127687, noted that on 64-bit platforms, where sizeof(integer) == 8, some structures are over-allocated because sizeof() might count additional padding bytes. That's no big deal, except that it is strange anyway to use [4] as the number of bytes in the structure. Solution: use the TclOffset macro to calculate the right size, in stead of substracting two sizeof()'s. That will correctly take padding bytes into account. patch attached
nijtmans Ticket [3129448fff]: 1 change
[History diff]2010-12-06 15:45:26
nijtmansplain2010-12-06 15:46:41
File Added - 395354: 3129448.patch
nijtmansplain2010-12-06 16:02:31
allow_comments - 1 Fixed in HEAD don't bother for Tcl 8.5 and lower
dgpplain2010-12-06 21:49:51
what does "integer" mean in this report? int ? long int ?
dgpplain2010-12-06 21:54:28
Hmmm... the actual patch applies doesn't appear to have anything to do with integers, but with pointer sizes and alignment matters. Is there a platform on which the effectiveness of the patch is actually tested?
nijtmansplain2010-12-06 23:12:12
Here is a small test program (try it on any UNIX machine) which shows the
dirrerence between substracting two sizes or determining the offset.
This program prints:
12 8
Here I used a long long as data type in stead of an int. It will occur
on any machine where sizeof(int) = 8.
=====================================================
#include <stdio.h>
#define TclOffset(type, field) \
((int) ((char *) &((type *) 0)->field))
typedef struct {
long long dummy;
char buf[4];
} mytype;
void main() {
mytype x;
int answer1 = sizeof(x) - sizeof(x.buf);
int answer2 = (unsigned)TclOffset(mytype, buf);
printf("%d %d\n", answer1, answer2);
}
nijtmansplain2010-12-11 04:55:55
Part 2, a few more places where the same pattern was used. Patch attached.
nijtmansplain2010-12-11 04:56:25
File Added - 395809: 3129448_2.patch
nijtmansplain2011-01-25 22:57:50
backported strcpy->memcpy changes to 8.5 and 8.4 as well, not any other struct change
Log in (anonymous is fine) to add a comment.
Attachments:
- 3129448_2.patch [download] added by nijtmans on 2010-12-11 04:56:25. [details]
- 3129448.patch [download] added by nijtmans on 2010-12-06 15:46:40. [details]