OLD | NEW |
| (Empty) |
1 diff --git a/nspr/lib/ds/plarena.c b/nspr/lib/ds/plarena.c | |
2 index 95e1931..689496d 100644 | |
3 --- a/nspr/lib/ds/plarena.c | |
4 +++ b/nspr/lib/ds/plarena.c | |
5 @@ -93,6 +93,9 @@ PR_IMPLEMENT(void) PL_InitArenaPool( | |
6 pool->mask = PR_BITMASK(PR_CeilingLog2(align)); | |
7 | |
8 pool->first.next = NULL; | |
9 + /* Set all three addresses in pool->first to the same dummy value. | |
10 + * These addresses are only compared with each other, but never | |
11 + * dereferenced. */ | |
12 pool->first.base = pool->first.avail = pool->first.limit = | |
13 (PRUword)PL_ARENA_ALIGN(pool, &pool->first + 1); | |
14 pool->current = &pool->first; | |
15 @@ -144,10 +147,14 @@ PR_IMPLEMENT(void *) PL_ArenaAllocate(PLArenaPool *pool, P
RUint32 nb) | |
16 { | |
17 PLArena *a; | |
18 char *rp; /* returned pointer */ | |
19 + PRUint32 nbOld; | |
20 | |
21 PR_ASSERT((nb & pool->mask) == 0); | |
22 | |
23 + nbOld = nb; | |
24 nb = (PRUword)PL_ARENA_ALIGN(pool, nb); /* force alignment */ | |
25 + if (nb < nbOld) | |
26 + return NULL; | |
27 | |
28 /* attempt to allocate from arenas at pool->current */ | |
29 { | |
30 @@ -208,6 +215,7 @@ PR_IMPLEMENT(void *) PL_ArenaAllocate(PLArenaPool *pool, PRU
int32 nb) | |
31 PL_MAKE_MEM_NOACCESS((void*)a->avail, a->limit - a->avail); | |
32 rp = (char *)a->avail; | |
33 a->avail += nb; | |
34 + PR_ASSERT(a->avail <= a->limit); | |
35 /* the newly allocated arena is linked after pool->current | |
36 * and becomes pool->current */ | |
37 a->next = pool->current->next; | |
38 @@ -230,6 +238,8 @@ PR_IMPLEMENT(void *) PL_ArenaGrow( | |
39 { | |
40 void *newp; | |
41 | |
42 + if (PR_UINT32_MAX - size < incr) | |
43 + return NULL; | |
44 PL_ARENA_ALLOCATE(newp, pool, size + incr); | |
45 if (newp) | |
46 memcpy(newp, p, size); | |
47 diff --git a/nspr/lib/ds/plarena.h b/nspr/lib/ds/plarena.h | |
48 index 8dcfb3e..3e51f83 100644 | |
49 --- a/nspr/lib/ds/plarena.h | |
50 +++ b/nspr/lib/ds/plarena.h | |
51 @@ -139,32 +139,37 @@ void __asan_unpoison_memory_region(void const volatile *ad
dr, size_t size); | |
52 PLArena *_a = (pool)->current; \ | |
53 PRUint32 _nb = PL_ARENA_ALIGN(pool, nb); \ | |
54 PRUword _p = _a->avail; \ | |
55 - PRUword _q = _p + _nb; \ | |
56 - if (_q > _a->limit) { \ | |
57 + if (_nb < nb) { \ | |
58 + _p = 0; \ | |
59 + } else if (_nb > (_a->limit - _a->avail)) { \ | |
60 _p = (PRUword)PL_ArenaAllocate(pool, _nb); \ | |
61 } else { \ | |
62 - _a->avail = _q; \ | |
63 + _a->avail += _nb; \ | |
64 } \ | |
65 p = (void *)_p; \ | |
66 - PL_MAKE_MEM_UNDEFINED(p, nb); \ | |
67 - PL_ArenaCountAllocation(pool, nb); \ | |
68 + if (p) { \ | |
69 + PL_MAKE_MEM_UNDEFINED(p, nb); \ | |
70 + PL_ArenaCountAllocation(pool, nb); \ | |
71 + } \ | |
72 PR_END_MACRO | |
73 | |
74 #define PL_ARENA_GROW(p, pool, size, incr) \ | |
75 PR_BEGIN_MACRO \ | |
76 PLArena *_a = (pool)->current; \ | |
77 PRUint32 _incr = PL_ARENA_ALIGN(pool, incr); \ | |
78 - PRUword _p = _a->avail; \ | |
79 - PRUword _q = _p + _incr; \ | |
80 - if (_p == (PRUword)(p) + PL_ARENA_ALIGN(pool, size) && \ | |
81 - _q <= _a->limit) { \ | |
82 + if (_incr < incr) { \ | |
83 + p = NULL; \ | |
84 + } else if (_a->avail == (PRUword)(p) + PL_ARENA_ALIGN(pool, size) && \ | |
85 + _incr <= (_a->limit - _a->avail)) { \ | |
86 PL_MAKE_MEM_UNDEFINED((unsigned char *)(p) + size, incr); \ | |
87 - _a->avail = _q; \ | |
88 + _a->avail += _incr; \ | |
89 PL_ArenaCountInplaceGrowth(pool, size, incr); \ | |
90 } else { \ | |
91 p = PL_ArenaGrow(pool, p, size, incr); \ | |
92 } \ | |
93 - PL_ArenaCountGrowth(pool, size, incr); \ | |
94 + if (p) {\ | |
95 + PL_ArenaCountGrowth(pool, size, incr); \ | |
96 + } \ | |
97 PR_END_MACRO | |
98 | |
99 #define PL_ARENA_MARK(pool) ((void *) (pool)->current->avail) | |
OLD | NEW |