diff options
author | Brian Paul <[email protected]> | 2002-10-30 19:40:20 +0000 |
---|---|---|
committer | Brian Paul <[email protected]> | 2002-10-30 19:40:20 +0000 |
commit | c5934054f3f3e2174a9897e901409da17c1a296e (patch) | |
tree | 6ec5306719ee2f8a46493113498901bd2844854b /src/mesa | |
parent | 3160edec3305a81e13aa2b5a3387a0a75c1747bf (diff) |
fix potential bug in _mesa_align_calloc/malloc (Frank van Heesch)
Diffstat (limited to 'src/mesa')
-rw-r--r-- | src/mesa/main/imports.c | 19 |
1 files changed, 13 insertions, 6 deletions
diff --git a/src/mesa/main/imports.c b/src/mesa/main/imports.c index f9c69863ab3..27ba25de94d 100644 --- a/src/mesa/main/imports.c +++ b/src/mesa/main/imports.c @@ -1,4 +1,4 @@ -/* $Id: imports.c,v 1.22 2002/10/25 21:06:28 brianp Exp $ */ +/* $Id: imports.c,v 1.23 2002/10/30 19:40:20 brianp Exp $ */ /* * Mesa 3-D graphics library @@ -104,9 +104,14 @@ _mesa_align_malloc(size_t bytes, unsigned long alignment) ASSERT( alignment > 0 ); - ptr = (unsigned long) _mesa_malloc( bytes + alignment ); + /* Allocate extra memory to accomodate rounding up the address for + * alignment and to record the real malloc address. + */ + ptr = (unsigned long) _mesa_malloc(bytes + alignment + sizeof(void *)); + if (!ptr) + return NULL; - buf = (ptr + alignment) & ~(unsigned long)(alignment - 1); + buf = (ptr + alignment + sizeof(void *)) & ~(unsigned long)(alignment - 1); *(unsigned long *)(buf - sizeof(void *)) = ptr; #ifdef DEBUG @@ -117,7 +122,7 @@ _mesa_align_malloc(size_t bytes, unsigned long alignment) } #endif - return (void *)buf; + return (void *) buf; } @@ -128,9 +133,11 @@ _mesa_align_calloc(size_t bytes, unsigned long alignment) ASSERT( alignment > 0 ); - ptr = (unsigned long) _mesa_calloc( bytes + alignment ); + ptr = (unsigned long) _mesa_calloc(bytes + alignment + sizeof(void *)); + if (!ptr) + return NULL; - buf = (ptr + alignment) & ~(unsigned long)(alignment - 1); + buf = (ptr + alignment + sizeof(void *)) & ~(unsigned long)(alignment - 1); *(unsigned long *)(buf - sizeof(void *)) = ptr; #ifdef DEBUG |