Source-Changes-HG archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
[src/trunk]: src/lib/libc/db It's been many years since realloc(NULL, size) =...
details: https://anonhg.NetBSD.org/src/rev/2596137f7278
branches: trunk
changeset: 325238:2596137f7278
user: christos <christos%NetBSD.org@localhost>
date: Sat Dec 14 18:04:56 2013 +0000
description:
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.
diffstat:
lib/libc/db/btree/bt_overflow.c | 9 +++++----
lib/libc/db/btree/bt_utils.c | 11 ++++-------
lib/libc/db/recno/rec_get.c | 29 ++++++++++++++---------------
lib/libc/db/recno/rec_put.c | 11 +++++------
lib/libc/db/recno/rec_utils.c | 12 ++++--------
5 files changed, 32 insertions(+), 40 deletions(-)
diffs (211 lines):
diff -r 2c8d42ceeb10 -r 2596137f7278 lib/libc/db/btree/bt_overflow.c
--- a/lib/libc/db/btree/bt_overflow.c Sat Dec 14 18:04:00 2013 +0000
+++ b/lib/libc/db/btree/bt_overflow.c Sat Dec 14 18:04:56 2013 +0000
@@ -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 @@
#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;
}
diff -r 2c8d42ceeb10 -r 2596137f7278 lib/libc/db/btree/bt_utils.c
--- a/lib/libc/db/btree/bt_utils.c Sat Dec 14 18:04:00 2013 +0000
+++ b/lib/libc/db/btree/bt_utils.c Sat Dec 14 18:04:56 2013 +0000
@@ -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 @@
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 @@
} 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;
diff -r 2c8d42ceeb10 -r 2596137f7278 lib/libc/db/recno/rec_get.c
--- a/lib/libc/db/recno/rec_get.c Sat Dec 14 18:04:00 2013 +0000
+++ b/lib/libc/db/recno/rec_get.c Sat Dec 14 18:04:56 2013 +0000
@@ -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 @@
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 @@
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 @@
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;
diff -r 2c8d42ceeb10 -r 2596137f7278 lib/libc/db/recno/rec_put.c
--- a/lib/libc/db/recno/rec_put.c Sat Dec 14 18:04:00 2013 +0000
+++ b/lib/libc/db/recno/rec_put.c Sat Dec 14 18:04:56 2013 +0000
@@ -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 @@
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);
diff -r 2c8d42ceeb10 -r 2596137f7278 lib/libc/db/recno/rec_utils.c
--- a/lib/libc/db/recno/rec_utils.c Sat Dec 14 18:04:00 2013 +0000
+++ b/lib/libc/db/recno/rec_utils.c Sat Dec 14 18:04:56 2013 +0000
@@ -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 @@
/* 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 @@
} 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;
Home |
Main Index |
Thread Index |
Old Index