Bug 808217: Make small adjustments to SECITEM_ReallocItemV2. Remove an NSS_3_15_BETA6
authorWan-Teh Chang <wtc@google.com>
Wed, 22 May 2013 12:00:45 -0700
changeset 10788 3c2b5c42ce91cd3e5d284f53e03e969fcbcdcf9b
parent 10787 a50886302a4c82917c620c36e5adf823244887f8
child 10789 77b14ffe595f85b2183b9a7a6e17883394240e6e
push id97
push userwtc@google.com
push dateWed, 22 May 2013 19:01:38 +0000
bugs808217
Bug 808217: Make small adjustments to SECITEM_ReallocItemV2. Remove an incorrect/stale comment. Match the behavior of realloc more closely. Free the data only when arena is NULL. r=kaie.
lib/util/secitem.c
lib/util/secitem.h
--- a/lib/util/secitem.c
+++ b/lib/util/secitem.c
@@ -126,42 +126,45 @@ SECITEM_ReallocItemV2(PLArenaPool *arena
 	return SECFailure;
     }
     
     if (item->len == newlen) {
 	return SECSuccess;
     }
 
     if (!newlen) {
-	SECITEM_FreeItem(item, PR_FALSE);
+	if (!arena) {
+	    PORT_Free(item->data);
+	}
+	item->data = NULL;
+	item->len = 0;
 	return SECSuccess;
     }
     
-    if (!item->len) {
+    if (!item->data) {
 	/* allocate fresh block of memory */
-	PORT_Assert(!item->data);
+	PORT_Assert(!item->len);
 	if (arena) {
 	    newdata = PORT_ArenaAlloc(arena, newlen);
 	} else {
 	    newdata = PORT_Alloc(newlen);
 	}
     } else {
 	/* reallocate or adjust existing block of memory */
 	if (arena) {
 	    if (item->len > newlen) {
 		/* There's no need to realloc a shorter block from the arena,
 		 * because it would result in using even more memory!
 		 * Therefore we'll continue to use the old block and 
 		 * set the item to the shorter size.
 		 */
 		item->len = newlen;
 		return SECSuccess;
-	    } else {
-		newdata = PORT_ArenaGrow(arena, item->data, item->len, newlen);
 	    }
+	    newdata = PORT_ArenaGrow(arena, item->data, item->len, newlen);
 	} else {
 	    newdata = PORT_Realloc(item->data, newlen);
 	}
     }
 
     if (!newdata) {
 	PORT_SetError(SEC_ERROR_NO_MEMORY);
 	return SECFailure;
--- a/lib/util/secitem.h
+++ b/lib/util/secitem.h
@@ -50,18 +50,17 @@ extern SECItem *SECITEM_AllocItem(PLAren
 */
 extern SECStatus SECITEM_ReallocItem( /* deprecated function */
 				     PLArenaPool *arena, SECItem *item,
 				     unsigned int oldlen, unsigned int newlen);
 
 /*
 ** Reallocate the data for the specified "item".  If "arena" is not NULL,
 ** then reallocate from there, otherwise reallocate from the heap.
-** If the item already has at least the request new size,
-** then the item is kept unchanged and SECSuccess is returned.
+** If item->data is NULL, the data is allocated (not reallocated).
 ** In any case, "item" is expected to be a valid SECItem pointer;
 ** SECFailure is returned if it is not, and the item will remain unchanged.
 ** If the allocation succeeds, the item is updated and SECSuccess is returned.
  */
 extern SECStatus SECITEM_ReallocItemV2(PLArenaPool *arena, SECItem *item,
 				       unsigned int newlen);
 
 /*