Commit 9a43ba71 authored by Per Cederqvist's avatar Per Cederqvist
Browse files

Include <inttypes.h> or <stdint.h>.

(union overhead): New union.  Use two of these instead of two
	unsigned ints as the overhead at the front of a malloced block.
	This ensures we get the alignment that we need.
(smalloc): Updated to use union overhead.  Some useless casts removed.
(sfree): Ditto.
(srealloc): Ditto.
parent f24e28c2
/*
* $Id: ram-smalloc.c,v 0.38 2002/11/04 07:42:57 ceder Exp $
* $Id: ram-smalloc.c,v 0.39 2003/01/07 22:14:19 ceder Exp $
* Copyright (C) 1991-1996, 1998-1999, 2001-2002 Lysator Academic Computer Association.
*
* This file is part of the LysKOM server.
......@@ -102,6 +102,14 @@
#include <sys/types.h>
#include <assert.h>
#include <errno.h>
/* The order between inttypes.h and stdint.h is mandated by autoconf-2.57. */
#if HAVE_INTTYPES_H
# include <inttypes.h>
#else
# if HAVE_STDINT_H
# include <stdint.h>
# endif
#endif
#include "exp.h"
#include "s-string.h"
......@@ -116,13 +124,13 @@
static int no_of_allocated_blocks = 0;
/* When using our own malloc guard areas, the memory layout looks like
this, where U is sizeof(unsigned int), and S is the size of the
this, where U is sizeof(union overhead), and S is the size of the
requested block.
offset what
====== ====
0: magic cookie (an unsigned int) (see below)
U: the size S (an unsigned int)
0: magic cookie (stored in an union overhead) (see below)
U: the size S (an union overhead)
2*U: S bytes of data returned to the user.
S+2*U: guard byte: 0x89
1+S+2*U: guard byte: 0xA7
......@@ -133,7 +141,7 @@ static int no_of_allocated_blocks = 0;
system malloc.
The magic cookie reflects the status of the block and is used for
defensive checks. The constants are defined in smalloc.h:
defensive checks. The constants are defined below:
SMALLOC_MAGIC_ALLOC: an allocated block.
SMALLOC_MAGIC_FREE: a block that was previously allocated.
......@@ -141,7 +149,41 @@ static int no_of_allocated_blocks = 0;
*/
#ifdef USE_MALLOC_GUARDS
# define OVERHEAD(s) ((s) + (2*sizeof(unsigned int) + 2))
/* A union of "all types", used to get the maximum alignment needed
for any type. */
union overhead {
/* We prefer to store stuf in a size_t, since we are storing a
size. Use an unsigned int if size_t isn't available. */
# ifdef HAVE_SIZE_T
size_t val;
unsigned int a;
# else
unsigned int val;
# endif
void *b;
long c;
long *d;
long long e;
long long *f;
float g;
double h;
void (*i)(void);
long double j;
# ifdef HAVE_INTPTR_T
intptr_t k;
# endif
# ifdef HAVE_INTMAX_T
intmax_t l;
# endif
};
# define OVERHEAD(s) ((s) + (2*sizeof(union overhead) + 2))
# define SMALLOC_MAGIC_ALLOC 0x12FE56A0u
# define SMALLOC_MAGIC_FREE 0xCA348E63u
#else
......@@ -190,9 +232,9 @@ trace_smalloc(size_t size,
EXPORT void *
smalloc(size_t size)
{
unsigned int *p;
union overhead *p;
p = (unsigned int *) malloc(OVERHEAD(size));
p = malloc(OVERHEAD(size));
if (p == NULL)
restart_kom("Can't allocate %lu bytes.\n", (unsigned long)size);
......@@ -200,8 +242,10 @@ smalloc(size_t size)
++no_of_allocated_blocks;
#ifdef USE_MALLOC_GUARDS
*p++ = SMALLOC_MAGIC_ALLOC;
*p++ = size;
p->val = SMALLOC_MAGIC_ALLOC;
p++;
p->val = size;
p++;
((unsigned char *) p)[size] = 0x89;
((unsigned char *) p)[size+1] = 0xA7;
#endif
......@@ -210,7 +254,7 @@ smalloc(size_t size)
trace_smalloc(size, p);
#endif
return (void *) p;
return p;
}
#ifdef TRACED_ALLOCATIONS
......@@ -228,7 +272,7 @@ EXPORT void
sfree(void * ptr) /* it is legal to sfree a NULL pointer */
{
#ifdef USE_MALLOC_GUARDS
unsigned int *ip;
union overhead *ip;
#endif
if ( ptr != NULL )
......@@ -241,17 +285,18 @@ sfree(void * ptr) /* it is legal to sfree a NULL pointer */
free(ptr);
--no_of_allocated_blocks;
#else
ip = (unsigned int *) ptr;
ip = (union overhead *)ptr;
ip -= 2;
switch (*ip)
switch (ip->val)
{
case SMALLOC_MAGIC_ALLOC:
if (((unsigned char *) (ip+2))[ip[1]] != 0x89 ||
((unsigned char *) (ip+2))[ip[1]+1] != 0xA7)
restart_kom("SFREE: Buffer overflow, bsize = %ul\n", ip[1]);
if (((unsigned char *) (ip+2))[ip[1].val] != 0x89 ||
((unsigned char *) (ip+2))[ip[1].val+1] != 0xA7)
restart_kom("SFREE: Buffer overflow, bsize = %ul\n",
ip[1].val);
--no_of_allocated_blocks;
*ip = SMALLOC_MAGIC_FREE;
free( ip );
ip->val = SMALLOC_MAGIC_FREE;
free(ip);
break;
case SMALLOC_MAGIC_FREE:
......@@ -280,16 +325,16 @@ trace_srealloc(size_t size,
EXPORT void *
srealloc(void * ptr, size_t size) /* Never fails. It is legal to */
{ /* realloc the NULL ptr. */
unsigned int * ip;
unsigned int * new_ptr;
union overhead * ip;
union overhead * new_ptr;
if ( ptr == NULL )
return smalloc(size);
ip = (unsigned int *) ptr;
ip = (union overhead *)ptr;
#ifdef USE_MALLOC_GUARDS
ip -= 2;
switch (*ip)
switch (ip->val)
{
case SMALLOC_MAGIC_ALLOC:
break;
......@@ -301,21 +346,23 @@ srealloc(void * ptr, size_t size) /* Never fails. It is legal to */
restart_kom("SREALLOC: Illegal magic number\n");
}
if (((unsigned char *) (ip+2))[ip[1]] != 0x89 ||
((unsigned char *) (ip+2))[ip[1]+1] != 0xA7)
if (((unsigned char *) (ip+2))[ip[1].val] != 0x89 ||
((unsigned char *) (ip+2))[ip[1].val+1] != 0xA7)
restart_kom("SREALLOC: Buffer overflow, osize = %ul, nsize = %lu.\n",
ip[1], (unsigned long)size);
ip[1].val, (unsigned long)size);
*ip = SMALLOC_MAGIC_FREE;
ip->val = SMALLOC_MAGIC_FREE;
#endif
if ((new_ptr = (unsigned int *)realloc((void*)ip, OVERHEAD(size))) == NULL)
if ((new_ptr = realloc(ip, OVERHEAD(size))) == NULL)
{
restart_kom("Out of memory - can't realloc. ptr = %lu size = %lu.\n",
(unsigned long)ptr, (unsigned long)size);
}
#ifdef USE_MALLOC_GUARDS
*new_ptr++ = SMALLOC_MAGIC_ALLOC;
*new_ptr++ = size;
new_ptr->val = SMALLOC_MAGIC_ALLOC;
new_ptr++;
new_ptr->val = size;
new_ptr++;
((unsigned char *) new_ptr)[size] = 0x89;
((unsigned char *) new_ptr)[size+1] = 0xA7;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment