Module Name:    src
Committed By:   christos
Date:           Sat Dec 14 18:04:56 UTC 2013

Modified Files:
        src/lib/libc/db/btree: bt_overflow.c bt_utils.c
        src/lib/libc/db/recno: rec_get.c rec_put.c rec_utils.c

Log Message:
It's been many years since realloc(NULL, size) == malloc(size). Also don't
destroy pointers on allocation errors so someone can free them later.


To generate a diff of this commit:
cvs rdiff -u -r1.19 -r1.20 src/lib/libc/db/btree/bt_overflow.c
cvs rdiff -u -r1.15 -r1.16 src/lib/libc/db/btree/bt_utils.c
cvs rdiff -u -r1.16 -r1.17 src/lib/libc/db/recno/rec_get.c
cvs rdiff -u -r1.20 -r1.21 src/lib/libc/db/recno/rec_put.c
cvs rdiff -u -r1.13 -r1.14 src/lib/libc/db/recno/rec_utils.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/lib/libc/db/btree/bt_overflow.c
diff -u src/lib/libc/db/btree/bt_overflow.c:1.19 src/lib/libc/db/btree/bt_overflow.c:1.20
--- src/lib/libc/db/btree/bt_overflow.c:1.19	Sat Nov 30 19:22:48 2013
+++ src/lib/libc/db/btree/bt_overflow.c	Sat Dec 14 13:04:56 2013
@@ -1,4 +1,4 @@
-/*	$NetBSD: bt_overflow.c,v 1.19 2013/12/01 00:22:48 christos Exp $	*/
+/*	$NetBSD: bt_overflow.c,v 1.20 2013/12/14 18:04:56 christos Exp $	*/
 
 /*-
  * Copyright (c) 1990, 1993, 1994
@@ -37,7 +37,7 @@
 #endif
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: bt_overflow.c,v 1.19 2013/12/01 00:22:48 christos Exp $");
+__RCSID("$NetBSD: bt_overflow.c,v 1.20 2013/12/14 18:04:56 christos Exp $");
 
 #include "namespace.h"
 #include <sys/param.h>
@@ -97,9 +97,10 @@ __ovfl_get(BTREE *t, void *p, size_t *ss
 #endif
 	/* Make the buffer bigger as necessary. */
 	if (*bufsz < sz) {
-		*buf = *buf == NULL ? malloc(sz) : realloc(*buf, sz);
-		if (*buf == NULL)
+		void *nbuf = realloc(*buf, sz);
+		if (nbuf == NULL)
 			return (RET_ERROR);
+		*buf = nbuf;
 		*bufsz = sz;
 	}
 

Index: src/lib/libc/db/btree/bt_utils.c
diff -u src/lib/libc/db/btree/bt_utils.c:1.15 src/lib/libc/db/btree/bt_utils.c:1.16
--- src/lib/libc/db/btree/bt_utils.c:1.15	Sat Nov 30 19:22:48 2013
+++ src/lib/libc/db/btree/bt_utils.c	Sat Dec 14 13:04:56 2013
@@ -1,4 +1,4 @@
-/*	$NetBSD: bt_utils.c,v 1.15 2013/12/01 00:22:48 christos Exp $	*/
+/*	$NetBSD: bt_utils.c,v 1.16 2013/12/14 18:04:56 christos Exp $	*/
 
 /*-
  * Copyright (c) 1990, 1993, 1994
@@ -37,7 +37,7 @@
 #endif
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: bt_utils.c,v 1.15 2013/12/01 00:22:48 christos Exp $");
+__RCSID("$NetBSD: bt_utils.c,v 1.16 2013/12/14 18:04:56 christos Exp $");
 
 #include <sys/param.h>
 
@@ -88,8 +88,7 @@ __bt_ret(BTREE *t, EPG *e, DBT *key, DBT
 		key->data = rkey->data;
 	} else if (copy || F_ISSET(t, B_DB_LOCK)) {
 		if (bl->ksize > rkey->size) {
-			p = rkey->data == NULL ?
-			    malloc(bl->ksize) : realloc(rkey->data, bl->ksize);
+			p = realloc(rkey->data, bl->ksize);
 			if (p == NULL)
 				return (RET_ERROR);
 			rkey->data = p;
@@ -115,9 +114,7 @@ dataonly:
 	} else if (copy || F_ISSET(t, B_DB_LOCK)) {
 		/* Use +1 in case the first record retrieved is 0 length. */
 		if (bl->dsize + 1 > rdata->size) {
-			p = rdata->data == NULL ?
-			    malloc(bl->dsize + 1) :
-			    realloc(rdata->data, bl->dsize + 1);
+			p = realloc(rdata->data, bl->dsize + 1);
 			if (p == NULL)
 				return (RET_ERROR);
 			rdata->data = p;

Index: src/lib/libc/db/recno/rec_get.c
diff -u src/lib/libc/db/recno/rec_get.c:1.16 src/lib/libc/db/recno/rec_get.c:1.17
--- src/lib/libc/db/recno/rec_get.c:1.16	Thu Sep 11 08:58:00 2008
+++ src/lib/libc/db/recno/rec_get.c	Sat Dec 14 13:04:56 2013
@@ -1,4 +1,4 @@
-/*	$NetBSD: rec_get.c,v 1.16 2008/09/11 12:58:00 joerg Exp $	*/
+/*	$NetBSD: rec_get.c,v 1.17 2013/12/14 18:04:56 christos Exp $	*/
 
 /*-
  * Copyright (c) 1990, 1993, 1994
@@ -34,7 +34,7 @@
 #endif
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: rec_get.c,v 1.16 2008/09/11 12:58:00 joerg Exp $");
+__RCSID("$NetBSD: rec_get.c,v 1.17 2013/12/14 18:04:56 christos Exp $");
 
 #include "namespace.h"
 #include <sys/types.h>
@@ -127,11 +127,10 @@ __rec_fpipe(BTREE *t, recno_t top)
 	uint8_t *p;
 
 	if (t->bt_rdata.size < t->bt_reclen) {
-		t->bt_rdata.data = t->bt_rdata.data == NULL ?
-		    malloc(t->bt_reclen) :
-		    realloc(t->bt_rdata.data, t->bt_reclen);
-		if (t->bt_rdata.data == NULL)
+		void *np = realloc(t->bt_rdata.data, t->bt_reclen);
+		if (np == NULL)
 			return (RET_ERROR);
+		t->bt_rdata.data = np;
 		t->bt_rdata.size = t->bt_reclen;
 	}
 	data.data = t->bt_rdata.data;
@@ -196,13 +195,14 @@ __rec_vpipe(BTREE *t, recno_t top)
 				break;
 			}
 			if (sz == 0) {
+				void *np;
 				len = p - (uint8_t *)t->bt_rdata.data;
-				t->bt_rdata.size += (sz = 256);
-				t->bt_rdata.data = t->bt_rdata.data == NULL ?
-				    malloc(t->bt_rdata.size) :
-				    realloc(t->bt_rdata.data, t->bt_rdata.size);
-				if (t->bt_rdata.data == NULL)
+				sz = t->bt_rdata.size + 256;
+				np = realloc(t->bt_rdata.data, sz);
+				if (np == NULL)
 					return (RET_ERROR);
+				t->bt_rdata.size = sz;
+				t->bt_rdata.data = np;
 				p = (uint8_t *)t->bt_rdata.data + len;
 			}
 		}
@@ -235,11 +235,10 @@ __rec_fmap(BTREE *t, recno_t top)
 	size_t len;
 
 	if (t->bt_rdata.size < t->bt_reclen) {
-		t->bt_rdata.data = t->bt_rdata.data == NULL ?
-		    malloc(t->bt_reclen) :
-		    realloc(t->bt_rdata.data, t->bt_reclen);
-		if (t->bt_rdata.data == NULL)
+		void *np = realloc(t->bt_rdata.data, t->bt_reclen);
+		if (np == NULL)
 			return (RET_ERROR);
+		t->bt_rdata.data = np;
 		t->bt_rdata.size = t->bt_reclen;
 	}
 	data.data = t->bt_rdata.data;

Index: src/lib/libc/db/recno/rec_put.c
diff -u src/lib/libc/db/recno/rec_put.c:1.20 src/lib/libc/db/recno/rec_put.c:1.21
--- src/lib/libc/db/recno/rec_put.c:1.20	Sat Nov 30 19:22:48 2013
+++ src/lib/libc/db/recno/rec_put.c	Sat Dec 14 13:04:56 2013
@@ -1,4 +1,4 @@
-/*	$NetBSD: rec_put.c,v 1.20 2013/12/01 00:22:48 christos Exp $	*/
+/*	$NetBSD: rec_put.c,v 1.21 2013/12/14 18:04:56 christos Exp $	*/
 
 /*-
  * Copyright (c) 1990, 1993, 1994
@@ -34,7 +34,7 @@
 #endif
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: rec_put.c,v 1.20 2013/12/01 00:22:48 christos Exp $");
+__RCSID("$NetBSD: rec_put.c,v 1.21 2013/12/14 18:04:56 christos Exp $");
 
 #include "namespace.h"
 #include <sys/types.h>
@@ -87,11 +87,10 @@ __rec_put(const DB *dbp, DBT *key, const
 			goto einval;
 
 		if (t->bt_rdata.size < t->bt_reclen) {
-			t->bt_rdata.data = t->bt_rdata.data == NULL ?
-			    malloc(t->bt_reclen) :
-			    realloc(t->bt_rdata.data, t->bt_reclen);
-			if (t->bt_rdata.data == NULL)
+			void *np = realloc(t->bt_rdata.data, t->bt_reclen);
+			if (np == NULL)
 				return (RET_ERROR);
+			t->bt_rdata.data = np;
 			t->bt_rdata.size = t->bt_reclen;
 		}
 		memmove(t->bt_rdata.data, data->data, data->size);

Index: src/lib/libc/db/recno/rec_utils.c
diff -u src/lib/libc/db/recno/rec_utils.c:1.13 src/lib/libc/db/recno/rec_utils.c:1.14
--- src/lib/libc/db/recno/rec_utils.c:1.13	Sat Nov 30 19:22:48 2013
+++ src/lib/libc/db/recno/rec_utils.c	Sat Dec 14 13:04:56 2013
@@ -1,4 +1,4 @@
-/*	$NetBSD: rec_utils.c,v 1.13 2013/12/01 00:22:48 christos Exp $	*/
+/*	$NetBSD: rec_utils.c,v 1.14 2013/12/14 18:04:56 christos Exp $	*/
 
 /*-
  * Copyright (c) 1990, 1993, 1994
@@ -34,7 +34,7 @@
 #endif
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: rec_utils.c,v 1.13 2013/12/01 00:22:48 christos Exp $");
+__RCSID("$NetBSD: rec_utils.c,v 1.14 2013/12/14 18:04:56 christos Exp $");
 
 #include <sys/param.h>
 
@@ -71,9 +71,7 @@ __rec_ret(BTREE *t, EPG *e, recno_t nrec
 
 	/* We have to copy the key, it's not on the page. */
 	if (sizeof(nrec) > t->bt_rkey.size) {
-		p = t->bt_rkey.data == NULL ?
-		    malloc(sizeof(nrec)) :
-		    realloc(t->bt_rkey.data, sizeof(nrec));
+		p = realloc(t->bt_rkey.data, sizeof(nrec));
 		if (p == NULL)
 			return (RET_ERROR);
 		t->bt_rkey.data = p;
@@ -101,9 +99,7 @@ dataonly:
 	} else if (F_ISSET(t, B_DB_LOCK)) {
 		/* Use +1 in case the first record retrieved is 0 length. */
 		if (rl->dsize + 1 > t->bt_rdata.size) {
-			p = t->bt_rdata.data == NULL ?
-			    malloc(rl->dsize + 1) :
-			    realloc(t->bt_rdata.data, rl->dsize + 1);
+			p = realloc(t->bt_rdata.data, rl->dsize + 1);
 			if (p == NULL)
 				return (RET_ERROR);
 			t->bt_rdata.data = p;

Reply via email to