fehQlibs 26
Qlibs
Loading...
Searching...
No Matches
alloc.c
Go to the documentation of this file.
1#include <stdlib.h>
2#include <errno.h>
3#include <limits.h>
4#include "byte.h"
5#include "alloc.h"
6
7#define ALIGNMENT 16 /* XXX: assuming that this alignment is enough */
8#define SPACE 4096 /* must be multiple of ALIGNMENT */
9#define space ((char *)realspace)
10
18typedef union {
19 char irrelevant[ALIGNMENT];
20 double d;
21} aligned;
22
23static aligned realspace[SPACE / ALIGNMENT];
24static unsigned int avail = SPACE; /* multiple of ALIGNMENT; 0<=avail<=SPACE */
25
26// /*@null@*//*@out@*/char *alloc(unsigned int n) -- old style
27void *alloc(unsigned int n)
28{
29 char *x;
30
31/* Guninski exploit + patch from Qualys (CVE-2005-1513) */
32
33 if (n >= (INT_MAX >> 3)) {
34 errno = ENOMEM;
35 return 0;
36 }
37
38 n = ALIGNMENT + n - (n & (ALIGNMENT - 1)); /* XXX: could overflow */
39 if (n <= avail) { avail -= n; return space + avail; }
40 x = malloc(n);
41 if (!x) errno = ENOMEM;
42 return x;
43}
44
45void alloc_free(void *x)
46{
47 if (x >= space)
48 if (x < space + SPACE)
49 return; /* XXX: assuming that pointers are flat */
50 free(x);
51}
52
53int alloc_re(void **x,unsigned int m,unsigned int n)
54{
55 char *y;
56
57 y = alloc(n);
58 if (!y) return 0;
59 byte_copy(y,m,*x);
60 qfree(*x);
61 *x = y;
62 return 1;
63}
#define space
Definition: alloc.c:9
#define ALIGNMENT
Definition: alloc.c:7
#define SPACE
Definition: alloc.c:8
int alloc_re(void **x, unsigned int m, unsigned int n)
Definition: alloc.c:53
void alloc_free(void *x)
Definition: alloc.c:45
void * alloc(unsigned int n)
Definition: alloc.c:27
#define qfree
Definition: alloc.h:9
void byte_copy(void *, unsigned int, const void *)
Definition: byte.c:20
Definition: alloc.c:18
double d
Definition: alloc.c:20