Bug 505880: nss_ZRealloc could use a local NSSArena *arena variable
authornelson%bolyard.com
Mon, 15 Mar 2010 08:29:31 +0000
changeset 9592 f377be0d5779d2f7c9eb654d425a9ff69cb987ab
parent 9588 7be7082b2c758a6b2a6fcc1b5bf949bee4d7b98b
child 9593 b72e73b49559867bbc06e03f58449b2f09602e36
push idunknown
push userunknown
push dateunknown
bugs505880
Bug 505880: nss_ZRealloc could use a local NSSArena *arena variable to make coverity happy Patch contributed by Timeless <timeless@mozdev.org>, r=nelson
security/nss/lib/base/arena.c
--- a/security/nss/lib/base/arena.c
+++ b/security/nss/lib/base/arena.c
@@ -1021,16 +1021,17 @@ nss_ZFreeIf
 
 NSS_EXTERN void *
 nss_ZRealloc
 (
   void *pointer,
   PRUint32 newSize
 )
 {
+  NSSArena *arena;
   struct pointer_header *h, *new_h;
   PRUint32 my_newSize = newSize + sizeof(struct pointer_header);
   void *rv;
 
   if( my_newSize < sizeof(struct pointer_header) ) {
     /* Wrapped */
     nss_SetError(NSS_ERROR_NO_MEMORY);
     return (void *)NULL;
@@ -1046,17 +1047,18 @@ nss_ZRealloc
 
   /* Check any magic here */
 
   if( newSize == h->size ) {
     /* saves thrashing */
     return pointer;
   }
 
-  if( (NSSArena *)NULL == h->arena ) {
+  arena = h->arena;
+  if (!arena) {
     /* Heap */
     new_h = (struct pointer_header *)PR_Calloc(1, my_newSize);
     if( (struct pointer_header *)NULL == new_h ) {
       nss_SetError(NSS_ERROR_NO_MEMORY);
       return (void *)NULL;
     }
 
     new_h->arena = (NSSArena *)NULL;
@@ -1075,32 +1077,32 @@ nss_ZRealloc
     h->size = 0;
     PR_Free(h);
 
     return rv;
   } else {
     void *p;
     /* Arena */
 #ifdef NSSDEBUG
-    if( PR_SUCCESS != nssArena_verifyPointer(h->arena) ) {
+    if (PR_SUCCESS != nssArena_verifyPointer(arena)) {
       return (void *)NULL;
     }
 #endif /* NSSDEBUG */
 
-    if( (PRLock *)NULL == h->arena->lock ) {
+    if (!arena->lock) {
       /* Just got destroyed.. so this pointer is invalid */
       nss_SetError(NSS_ERROR_INVALID_POINTER);
       return (void *)NULL;
     }
-    PR_Lock(h->arena->lock);
+    PR_Lock(arena->lock);
 
 #ifdef ARENA_THREADMARK
-    if( (PRThread *)NULL != h->arena->marking_thread ) {
-      if( PR_GetCurrentThread() != h->arena->marking_thread ) {
-        PR_Unlock(h->arena->lock);
+    if (arena->marking_thread) {
+      if (PR_GetCurrentThread() != arena->marking_thread) {
+        PR_Unlock(arena->lock);
         nss_SetError(NSS_ERROR_ARENA_MARKED_BY_ANOTHER_THREAD);
         return (void *)NULL;
       }
     }
 #endif /* ARENA_THREADMARK */
 
     if( newSize < h->size ) {
       /*
@@ -1112,39 +1114,39 @@ nss_ZRealloc
        * it now."  We'll zero the leftover part, of course.  And
        * in fact we might as well *not* adjust h->size-- this way,
        * if the user reallocs back up to something not greater than
        * the original size, then voila, there's the memory!  This
        * way a thrash big/small/big/small doesn't burn up the arena.
        */
       char *extra = &((char *)pointer)[ newSize ];
       (void)nsslibc_memset(extra, 0, (h->size - newSize));
-      PR_Unlock(h->arena->lock);
+      PR_Unlock(arena->lock);
       return pointer;
     }
 
-    PR_ARENA_ALLOCATE(p, &h->arena->pool, my_newSize);
+    PR_ARENA_ALLOCATE(p, &arena->pool, my_newSize);
     if( (void *)NULL == p ) {
-      PR_Unlock(h->arena->lock);
+      PR_Unlock(arena->lock);
       nss_SetError(NSS_ERROR_NO_MEMORY);
       return (void *)NULL;
     }
 
     new_h = (struct pointer_header *)p;
-    new_h->arena = h->arena;
+    new_h->arena = arena;
     new_h->size = newSize;
     rv = (void *)((char *)new_h + sizeof(struct pointer_header));
     if (rv != pointer) {
 	(void)nsslibc_memcpy(rv, pointer, h->size);
 	(void)nsslibc_memset(pointer, 0, h->size);
     }
     (void)nsslibc_memset(&((char *)rv)[ h->size ], 0, (newSize - h->size));
     h->arena = (NSSArena *)NULL;
     h->size = 0;
-    PR_Unlock(new_h->arena->lock);
+    PR_Unlock(arena->lock);
     return rv;
   }
   /*NOTREACHED*/
 }
 
 PRStatus 
 nssArena_Shutdown(void)
 {