Frederic Bouvier wrote:
> I get memory corruption caused by writing outside an malloc'ated memory
> bloc. I tracked the problem down to the recsize() function ( in hash.c )
> computing a memory size that is not enough for subsequent initialization
> in resize()
Wow, good catch. This was also reported on the nasal list as a difference
between optimized and non-optimized builds on 32 bit linux. I tracked it
down as far as recsize() returning the wrong value, but then wrote it off
as a compiler bug and didn't investigate further. I missed the alignment
issue completely.
Try the following patch, which will force the alignment but still allow the
use of the (IMHO) clever trick to get the memory block size in a single line:
Index: hash.c
===================================================================
RCS file: /home/nasal-cvs/nasal/src/hash.c,v
retrieving revision 1.51
diff -u -r1.51 hash.c
--- hash.c 26 Sep 2008 17:53:29 -0000 1.51
+++ hash.c 25 Nov 2008 17:18:02 -0000
@@ -96,9 +96,12 @@
static int recsize(int lgsz)
{
- HashRec hr;
- hr.lgsz = lgsz;
- return (int)((char*)&TAB(&hr)[POW2(lgsz+1)] - (char*)&hr);
+ /* Union with the pointer for alignment purposes, to guarantee
+ * that the dummy HashRec has the same alignment as the malloc
+ * block that will eventually contain the real one. */
+ union { void* align; HashRec hr; } u;
+ u.hr.lgsz = lgsz;
+ return (int)((char*)&TAB(&u.hr)[POW2(lgsz+1)]) - (char*)&u.hr);
}
static HashRec* resize(struct naHash* hash)
-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
Flightgear-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/flightgear-devel