| OLD | NEW |
| 1 /* This Source Code Form is subject to the terms of the Mozilla Public | 1 /* This Source Code Form is subject to the terms of the Mozilla Public |
| 2 * License, v. 2.0. If a copy of the MPL was not distributed with this | 2 * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 4 | 4 |
| 5 /* | 5 /* |
| 6 * secport.c - portability interfaces for security libraries | 6 * secport.c - portability interfaces for security libraries |
| 7 * | 7 * |
| 8 * This file abstracts out libc functionality that libsec depends on | 8 * This file abstracts out libc functionality that libsec depends on |
| 9 * | 9 * |
| 10 * NOTE - These are not public interfaces | 10 * NOTE - These are not public interfaces |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 62 unsigned long port_allocFailures; | 62 unsigned long port_allocFailures; |
| 63 | 63 |
| 64 /* locations for registering Unicode conversion functions. | 64 /* locations for registering Unicode conversion functions. |
| 65 * XXX is this the appropriate location? or should they be | 65 * XXX is this the appropriate location? or should they be |
| 66 * moved to client/server specific locations? | 66 * moved to client/server specific locations? |
| 67 */ | 67 */ |
| 68 PORTCharConversionFunc ucs4Utf8ConvertFunc; | 68 PORTCharConversionFunc ucs4Utf8ConvertFunc; |
| 69 PORTCharConversionFunc ucs2Utf8ConvertFunc; | 69 PORTCharConversionFunc ucs2Utf8ConvertFunc; |
| 70 PORTCharConversionWSwapFunc ucs2AsciiConvertFunc; | 70 PORTCharConversionWSwapFunc ucs2AsciiConvertFunc; |
| 71 | 71 |
| 72 /* NSPR memory allocation functions (PR_Malloc, PR_Calloc, and PR_Realloc) |
| 73 * use the PRUint32 type for the size parameter. Before we pass a size_t or |
| 74 * unsigned long size to these functions, we need to ensure it is <= half of |
| 75 * the maximum PRUint32 value to avoid truncation and catch a negative size. |
| 76 */ |
| 77 #define MAX_SIZE (PR_UINT32_MAX >> 1) |
| 78 |
| 72 void * | 79 void * |
| 73 PORT_Alloc(size_t bytes) | 80 PORT_Alloc(size_t bytes) |
| 74 { | 81 { |
| 75 void *rv; | 82 void *rv = NULL; |
| 76 | 83 |
| 77 /* Always allocate a non-zero amount of bytes */ | 84 if (bytes <= MAX_SIZE) { |
| 78 rv = (void *)PR_Malloc(bytes ? bytes : 1); | 85 » /* Always allocate a non-zero amount of bytes */ |
| 86 » rv = PR_Malloc(bytes ? bytes : 1); |
| 87 } |
| 79 if (!rv) { | 88 if (!rv) { |
| 80 ++port_allocFailures; | 89 ++port_allocFailures; |
| 81 PORT_SetError(SEC_ERROR_NO_MEMORY); | 90 PORT_SetError(SEC_ERROR_NO_MEMORY); |
| 82 } | 91 } |
| 83 return rv; | 92 return rv; |
| 84 } | 93 } |
| 85 | 94 |
| 86 void * | 95 void * |
| 87 PORT_Realloc(void *oldptr, size_t bytes) | 96 PORT_Realloc(void *oldptr, size_t bytes) |
| 88 { | 97 { |
| 89 void *rv; | 98 void *rv = NULL; |
| 90 | 99 |
| 91 rv = (void *)PR_Realloc(oldptr, bytes); | 100 if (bytes <= MAX_SIZE) { |
| 101 » rv = PR_Realloc(oldptr, bytes); |
| 102 } |
| 92 if (!rv) { | 103 if (!rv) { |
| 93 ++port_allocFailures; | 104 ++port_allocFailures; |
| 94 PORT_SetError(SEC_ERROR_NO_MEMORY); | 105 PORT_SetError(SEC_ERROR_NO_MEMORY); |
| 95 } | 106 } |
| 96 return rv; | 107 return rv; |
| 97 } | 108 } |
| 98 | 109 |
| 99 void * | 110 void * |
| 100 PORT_ZAlloc(size_t bytes) | 111 PORT_ZAlloc(size_t bytes) |
| 101 { | 112 { |
| 102 void *rv; | 113 void *rv = NULL; |
| 103 | 114 |
| 104 /* Always allocate a non-zero amount of bytes */ | 115 if (bytes <= MAX_SIZE) { |
| 105 rv = (void *)PR_Calloc(1, bytes ? bytes : 1); | 116 » /* Always allocate a non-zero amount of bytes */ |
| 117 » rv = PR_Calloc(1, bytes ? bytes : 1); |
| 118 } |
| 106 if (!rv) { | 119 if (!rv) { |
| 107 ++port_allocFailures; | 120 ++port_allocFailures; |
| 108 PORT_SetError(SEC_ERROR_NO_MEMORY); | 121 PORT_SetError(SEC_ERROR_NO_MEMORY); |
| 109 } | 122 } |
| 110 return rv; | 123 return rv; |
| 111 } | 124 } |
| 112 | 125 |
| 113 void | 126 void |
| 114 PORT_Free(void *ptr) | 127 PORT_Free(void *ptr) |
| 115 { | 128 { |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 202 * only operation, other than destroying the arenapool, that potentially | 215 * only operation, other than destroying the arenapool, that potentially |
| 203 * reduces the number of arenas on the stack. Otherwise, the stack grows | 216 * reduces the number of arenas on the stack. Otherwise, the stack grows |
| 204 * until the arenapool is destroyed, at which point all the arenas are | 217 * until the arenapool is destroyed, at which point all the arenas are |
| 205 * freed or returned to a "free arena list", depending on their sizes. | 218 * freed or returned to a "free arena list", depending on their sizes. |
| 206 */ | 219 */ |
| 207 PLArenaPool * | 220 PLArenaPool * |
| 208 PORT_NewArena(unsigned long chunksize) | 221 PORT_NewArena(unsigned long chunksize) |
| 209 { | 222 { |
| 210 PORTArenaPool *pool; | 223 PORTArenaPool *pool; |
| 211 | 224 |
| 225 if (chunksize > MAX_SIZE) { |
| 226 PORT_SetError(SEC_ERROR_NO_MEMORY); |
| 227 return NULL; |
| 228 } |
| 212 pool = PORT_ZNew(PORTArenaPool); | 229 pool = PORT_ZNew(PORTArenaPool); |
| 213 if (!pool) { | 230 if (!pool) { |
| 214 return NULL; | 231 return NULL; |
| 215 } | 232 } |
| 216 pool->magic = ARENAPOOL_MAGIC; | 233 pool->magic = ARENAPOOL_MAGIC; |
| 217 pool->lock = PZ_NewLock(nssILockArena); | 234 pool->lock = PZ_NewLock(nssILockArena); |
| 218 if (!pool->lock) { | 235 if (!pool->lock) { |
| 219 ++port_allocFailures; | 236 ++port_allocFailures; |
| 220 PORT_Free(pool); | 237 PORT_Free(pool); |
| 221 return NULL; | 238 return NULL; |
| 222 } | 239 } |
| 223 PL_InitArenaPool(&pool->arena, "security", chunksize, sizeof(double)); | 240 PL_InitArenaPool(&pool->arena, "security", chunksize, sizeof(double)); |
| 224 return(&pool->arena); | 241 return(&pool->arena); |
| 225 } | 242 } |
| 226 | 243 |
| 227 #define MAX_SIZE 0x7fffffffUL | |
| 228 | |
| 229 void * | 244 void * |
| 230 PORT_ArenaAlloc(PLArenaPool *arena, size_t size) | 245 PORT_ArenaAlloc(PLArenaPool *arena, size_t size) |
| 231 { | 246 { |
| 232 void *p = NULL; | 247 void *p = NULL; |
| 233 | 248 |
| 234 PORTArenaPool *pool = (PORTArenaPool *)arena; | 249 PORTArenaPool *pool = (PORTArenaPool *)arena; |
| 235 | 250 |
| 236 if (size <= 0) { | 251 if (size <= 0) { |
| 237 size = 1; | 252 size = 1; |
| 238 } | 253 } |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 323 PZ_DestroyLock(lock); | 338 PZ_DestroyLock(lock); |
| 324 } | 339 } |
| 325 } | 340 } |
| 326 | 341 |
| 327 void * | 342 void * |
| 328 PORT_ArenaGrow(PLArenaPool *arena, void *ptr, size_t oldsize, size_t newsize) | 343 PORT_ArenaGrow(PLArenaPool *arena, void *ptr, size_t oldsize, size_t newsize) |
| 329 { | 344 { |
| 330 PORTArenaPool *pool = (PORTArenaPool *)arena; | 345 PORTArenaPool *pool = (PORTArenaPool *)arena; |
| 331 PORT_Assert(newsize >= oldsize); | 346 PORT_Assert(newsize >= oldsize); |
| 332 | 347 |
| 348 if (newsize > MAX_SIZE) { |
| 349 PORT_SetError(SEC_ERROR_NO_MEMORY); |
| 350 return NULL; |
| 351 } |
| 352 |
| 333 if (ARENAPOOL_MAGIC == pool->magic ) { | 353 if (ARENAPOOL_MAGIC == pool->magic ) { |
| 334 PZ_Lock(pool->lock); | 354 PZ_Lock(pool->lock); |
| 335 /* Do we do a THREADMARK check here? */ | 355 /* Do we do a THREADMARK check here? */ |
| 336 PL_ARENA_GROW(ptr, arena, oldsize, ( newsize - oldsize ) ); | 356 PL_ARENA_GROW(ptr, arena, oldsize, ( newsize - oldsize ) ); |
| 337 PZ_Unlock(pool->lock); | 357 PZ_Unlock(pool->lock); |
| 338 } else { | 358 } else { |
| 339 PL_ARENA_GROW(ptr, arena, oldsize, ( newsize - oldsize ) ); | 359 PL_ARENA_GROW(ptr, arena, oldsize, ( newsize - oldsize ) ); |
| 340 } | 360 } |
| 341 | 361 |
| 342 return(ptr); | 362 return(ptr); |
| (...skipping 346 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 689 const unsigned char *b = (const unsigned char*) ib; | 709 const unsigned char *b = (const unsigned char*) ib; |
| 690 size_t i; | 710 size_t i; |
| 691 unsigned char r = 0; | 711 unsigned char r = 0; |
| 692 | 712 |
| 693 for (i = 0; i < n; ++i) { | 713 for (i = 0; i < n; ++i) { |
| 694 r |= *a++ ^ *b++; | 714 r |= *a++ ^ *b++; |
| 695 } | 715 } |
| 696 | 716 |
| 697 return r; | 717 return r; |
| 698 } | 718 } |
| OLD | NEW |