Centralize error checking for malloc,calloc,strdup.
This commit is contained in:
parent
1880024ade
commit
f8ce51d45f
@ -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 @@ params_new(void)
|
||||
{
|
||||
struct params *p;
|
||||
|
||||
p = malloc(sizeof(*p));
|
||||
p = emalloc(sizeof(*p));
|
||||
params_init(p);
|
||||
return p;
|
||||
}
|
||||
@ -298,9 +298,7 @@ keygen_new(void)
|
||||
{
|
||||
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 @@ print_kvpair_int(FILE *f, int ts, const char *key, int val)
|
||||
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);
|
||||
}
|
||||
|
@ -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 @@ prf_iterate(u_int8_t *r, const u_int8_t *P, int Plen,
|
||||
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 @@ pkcs5_pbkdf2(u_int8_t **r, int dkLen, const u_int8_t *P, int Plen,
|
||||
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++)
|
||||
|
@ -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 @@ __RCSID("$NetBSD: utils.c,v 1.5 2004/08/13 15:03:57 tv Exp $");
|
||||
|
||||
#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 @@ words(const char *line, int *num)
|
||||
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_new(const char *intext, int inlength)
|
||||
{
|
||||
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 @@ string_free(string_t *s)
|
||||
|
||||
if (!s)
|
||||
return;
|
||||
free_notnull(s->text);
|
||||
free(s->text);
|
||||
free(s);
|
||||
}
|
||||
|
||||
@ -185,9 +214,9 @@ string_add(const string_t *a1, const string_t *a2)
|
||||
{
|
||||
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_fromint(int in)
|
||||
{
|
||||
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 @@ bits_new(const void *buf, int len)
|
||||
/* 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 @@ bits_free(bits_t *b)
|
||||
|
||||
if (!b)
|
||||
return;
|
||||
free_notnull(b->text);
|
||||
free(b->text);
|
||||
free(b);
|
||||
}
|
||||
|
||||
@ -320,10 +345,9 @@ bits_xor(const bits_t *x1, const bits_t *x2)
|
||||
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 @@ bits_decode(const string_t *in)
|
||||
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 @@ bits_encode(const bits_t *in)
|
||||
/* 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_fget(FILE *f, int len)
|
||||
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 @@ bits_fprint(FILE *f, const bits_t *bits)
|
||||
string_fprint(f, s);
|
||||
free(s);
|
||||
}
|
||||
|
||||
void
|
||||
free_notnull(void *b)
|
||||
{
|
||||
|
||||
if (b)
|
||||
free(b);
|
||||
}
|
||||
|
@ -1,4 +1,4 @@
|
||||
/* $NetBSD: utils.h,v 1.4 2004/08/13 15:03:57 tv Exp $ */
|
||||
/* $NetBSD: utils.h,v 1.5 2005/03/30 17:10:18 christos Exp $ */
|
||||
|
||||
/*-
|
||||
* Copyright (c) 2002, 2003 The NetBSD Foundation, Inc.
|
||||
@ -50,6 +50,9 @@ struct bits;
|
||||
typedef struct bits bits_t;
|
||||
|
||||
__BEGIN_DECLS
|
||||
void *emalloc(size_t);
|
||||
void *ecalloc(size_t, size_t);
|
||||
char *estrdup(const char *);
|
||||
char **words(const char *, int *);
|
||||
void words_free(char **, int);
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user