Source-Changes-HG archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
[src/trunk]: src/sbin/cgdconfig Centralize error checking for malloc, calloc, s...
details: https://anonhg.NetBSD.org/src/rev/bb56e35c6110
branches: trunk
changeset: 579887:bb56e35c6110
user: christos <christos%NetBSD.org@localhost>
date: Wed Mar 30 17:10:18 2005 +0000
description:
Centralize error checking for malloc,calloc,strdup.
diffstat:
sbin/cgdconfig/params.c | 15 ++---
sbin/cgdconfig/pkcs5_pbkdf2.c | 10 +--
sbin/cgdconfig/utils.c | 100 +++++++++++++++++++++--------------------
sbin/cgdconfig/utils.h | 5 +-
4 files changed, 65 insertions(+), 65 deletions(-)
diffs (truncated from 321 to 300 lines):
diff -r 95423a2cfda9 -r bb56e35c6110 sbin/cgdconfig/params.c
--- a/sbin/cgdconfig/params.c Wed Mar 30 17:09:28 2005 +0000
+++ b/sbin/cgdconfig/params.c Wed Mar 30 17:10:18 2005 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: params.c,v 1.12 2005/01/04 04:52:50 elric Exp $ */
+/* $NetBSD: params.c,v 1.13 2005/03/30 17:10:18 christos Exp $ */
/*-
* Copyright (c) 2002, 2003 The NetBSD Foundation, Inc.
@@ -38,7 +38,7 @@
#include <sys/cdefs.h>
#ifndef lint
-__RCSID("$NetBSD: params.c,v 1.12 2005/01/04 04:52:50 elric Exp $");
+__RCSID("$NetBSD: params.c,v 1.13 2005/03/30 17:10:18 christos Exp $");
#endif
#include <sys/types.h>
@@ -87,7 +87,7 @@
{
struct params *p;
- p = malloc(sizeof(*p));
+ p = emalloc(sizeof(*p));
params_init(p);
return p;
}
@@ -298,9 +298,7 @@
{
struct keygen *kg;
- kg = malloc(sizeof(*kg));
- if (!kg)
- return NULL;
+ kg = emalloc(sizeof(*kg));
kg->kg_method = KEYGEN_UNKNOWN;
kg->kg_iterations = -1;
kg->kg_salt = NULL;
@@ -625,9 +623,8 @@
if (!key || val == -1)
return;
- asprintf(&tmp, "%d", val);
- if (!tmp)
- err(1, "malloc");
+ if (asprintf(&tmp, "%d", val) == -1)
+ err(1, NULL);
print_kvpair_cstr(f, ts, key, tmp);
free(tmp);
}
diff -r 95423a2cfda9 -r bb56e35c6110 sbin/cgdconfig/pkcs5_pbkdf2.c
--- a/sbin/cgdconfig/pkcs5_pbkdf2.c Wed Mar 30 17:09:28 2005 +0000
+++ b/sbin/cgdconfig/pkcs5_pbkdf2.c Wed Mar 30 17:10:18 2005 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: pkcs5_pbkdf2.c,v 1.6 2005/01/04 04:55:18 elric Exp $ */
+/* $NetBSD: pkcs5_pbkdf2.c,v 1.7 2005/03/30 17:10:18 christos Exp $ */
/*-
* Copyright (c) 2002, 2003 The NetBSD Foundation, Inc.
@@ -53,7 +53,7 @@
#include <sys/cdefs.h>
#ifndef lint
-__RCSID("$NetBSD: pkcs5_pbkdf2.c,v 1.6 2005/01/04 04:55:18 elric Exp $");
+__RCSID("$NetBSD: pkcs5_pbkdf2.c,v 1.7 2005/03/30 17:10:18 christos Exp $");
#endif
#include <sys/resource.h>
@@ -100,7 +100,7 @@
u_int8_t *data;
u_int8_t tmp[EVP_MAX_MD_SIZE];
- data = malloc(Slen + 4);
+ data = emalloc(Slen + 4);
memcpy(data, S, Slen);
int_encode(data + Slen, ind);
datalen = Slen + 4;
@@ -144,9 +144,7 @@
l = (dkLen + PRF_BLOCKLEN - 1) / PRF_BLOCKLEN;
/* allocate the output */
- *r = malloc(l * PRF_BLOCKLEN);
- if (!*r)
- return -1;
+ *r = emalloc(l * PRF_BLOCKLEN);
/* Step 3 */
for (i=0; i < l; i++)
diff -r 95423a2cfda9 -r bb56e35c6110 sbin/cgdconfig/utils.c
--- a/sbin/cgdconfig/utils.c Wed Mar 30 17:09:28 2005 +0000
+++ b/sbin/cgdconfig/utils.c Wed Mar 30 17:10:18 2005 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: utils.c,v 1.5 2004/08/13 15:03:57 tv Exp $ */
+/* $NetBSD: utils.c,v 1.6 2005/03/30 17:10:18 christos Exp $ */
/*-
* Copyright (c) 2002, 2003 The NetBSD Foundation, Inc.
@@ -38,13 +38,14 @@
#include <sys/cdefs.h>
#ifndef lint
-__RCSID("$NetBSD: utils.c,v 1.5 2004/08/13 15:03:57 tv Exp $");
+__RCSID("$NetBSD: utils.c,v 1.6 2005/03/30 17:10:18 christos Exp $");
#endif
#include <sys/param.h>
#include <stdlib.h>
#include <string.h>
+#include <err.h>
/* include the resolver gunk in order that we can use b64 routines */
#include <netinet/in.h>
@@ -53,6 +54,34 @@
#include "utils.h"
+
+void *
+emalloc(size_t len)
+{
+ void *ptr = malloc(len);
+ if (ptr == NULL)
+ err(1, NULL);
+ return ptr;
+}
+
+void *
+ecalloc(size_t nel, size_t len)
+{
+ void *ptr = calloc(nel, len);
+ if (ptr == NULL)
+ err(1, NULL);
+ return ptr;
+}
+
+char *
+estrdup(const char *str)
+{
+ char *ptr = strdup(str);
+ if (ptr == NULL)
+ err(1, NULL);
+ return ptr;
+}
+
/* just strsep(3), but skips empty fields. */
static char *
@@ -91,10 +120,10 @@
nwords++;
tmp++;
}
- ret = malloc((nwords+1) * sizeof(char *));
- tmp1 = tmp = strdup(line);
+ ret = emalloc((nwords+1) * sizeof(char *));
+ tmp1 = tmp = estrdup(line);
while ((cur = strsep_getnext(&tmp, " \t")) != NULL)
- ret[i++] = strdup(cur);
+ ret[i++] = estrdup(cur);
ret[i] = NULL;
free(tmp1);
*num = nwords;
@@ -147,9 +176,9 @@
{
string_t *out;
- out = malloc(sizeof(*out));
+ out = emalloc(sizeof(*out));
out->length = inlength;
- out->text = malloc(out->length + 1);
+ out->text = emalloc(out->length + 1);
memcpy(out->text, intext, out->length);
out->text[out->length] = '\0';
return out;
@@ -168,7 +197,7 @@
if (!s)
return;
- free_notnull(s->text);
+ free(s->text);
free(s);
}
@@ -185,9 +214,9 @@
{
string_t *sum;
- sum = malloc(sizeof(*sum));
+ sum = emalloc(sizeof(*sum));
sum->length = a1->length + a2->length;
- sum->text = malloc(sum->length + 1);
+ sum->text = emalloc(sum->length + 1);
memcpy(sum->text, a1->text, a1->length);
memcpy(sum->text + a1->length, a2->text, a2->length);
sum->text[sum->length] = '\0';
@@ -224,14 +253,10 @@
{
string_t *ret;
- ret = malloc(sizeof(*ret));
- if (!ret)
- return NULL;
+ ret = emalloc(sizeof(*ret));
ret->length = asprintf(&ret->text, "%d", in);
- if (ret->length == -1) {
- free(ret);
- ret = NULL;
- }
+ if (ret->length == -1)
+ err(1, NULL);
return ret;
}
@@ -255,7 +280,7 @@
/* XXX do some level of error checking here */
b = malloc(sizeof(*b));
b->length = len;
- b->text = malloc(BITS2BYTES(b->length));
+ b->text = emalloc(BITS2BYTES(b->length));
memcpy(b->text, buf, BITS2BYTES(b->length));
return b;
}
@@ -273,7 +298,7 @@
if (!b)
return;
- free_notnull(b->text);
+ free(b->text);
free(b);
}
@@ -320,10 +345,9 @@
bits_t *b;
int i;
- /* XXX do some level of error checking here */
- b = malloc(sizeof(*b));
+ b = emalloc(sizeof(*b));
b->length = MAX(x1->length, x2->length);
- b->text = calloc(1, BITS2BYTES(b->length));
+ b->text = ecalloc(1, BITS2BYTES(b->length));
for (i=0; i < BITS2BYTES(MIN(x1->length, x2->length)); i++)
b->text[i] = x1->text[i] ^ x2->text[i];
return b;
@@ -356,9 +380,7 @@
char *tmp;
len = in->length;
- tmp = malloc(len);
- if (!tmp)
- return NULL;
+ tmp = emalloc(len);
len = __b64_pton(in->text, tmp, len);
@@ -405,14 +427,8 @@
/* compute the total size of the input stream */
len = BITS2BYTES(in->length) + 4;
- tmp = malloc(len);
- out = malloc(len * 2);
- if (!tmp || !out) {
- free_notnull(tmp);
- free_notnull(out);
- return NULL;
- }
-
+ tmp = emalloc(len);
+ out = emalloc(len * 2);
/* stuff the length up front */
*((u_int32_t *)tmp) = htonl(in->length);
memcpy(tmp + 4, in->text, len - 4);
@@ -440,15 +456,9 @@
bits_t *bits;
int ret;
- bits = malloc(sizeof(*bits));
- if (!bits)
- return NULL;
+ bits = emalloc(sizeof(*bits));
bits->length = len;
- bits->text = malloc(BITS2BYTES(bits->length));
- if (!bits->text) {
- free(bits);
- return NULL;
- }
+ bits->text = emalloc(BITS2BYTES(bits->length));
ret = fread(bits->text, BITS2BYTES(bits->length), 1, f);
if (ret != 1) {
bits_free(bits);
@@ -491,11 +501,3 @@
string_fprint(f, s);
free(s);
}
-
-void
-free_notnull(void *b)
-{
-
- if (b)
Home |
Main Index |
Thread Index |
Old Index