----- "Andy Ross" a écrit :
> 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:
The patch you proposed doesn't solve the issue because void* has the
same size than an int. As you want the data to be aligned on the
size of a naRef, the modified patch below did the trick :
--- simgear/nasal/hash.c 26 Sep 2008 18:22:12 -0000 1.9
+++ simgear/nasal/hash.c 25 Nov 2008 19:55:47 -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 naRef 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 { naRef 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)
-Fred
--
Frédéric Bouvier
http://my.fotolia.com/frfoto/ Photo gallery - album photo
http://fgsd.sourceforge.net/ FlightGear Scenery Designer
-------------------------------------------------------------------------
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