Date: 18 Jun 2001 03:21:20 +0200 From: Assar Westerlund <assar@freebsd.org> To: freebsd-audit@freebsd.org Subject: Re: duplicate copies of emalloc to libutil? Message-ID: <5ld782twfj.fsf@assaris.sics.se> In-Reply-To: Assar Westerlund's message of "17 Jun 2001 13:41:44 %2B0200" References: <5ld783bafb.fsf@assaris.sics.se>
next in thread | previous in thread | raw e-mail | index | archive | help
--=-=-= I wrote: > Any comments? New and improved patch below. /assar --=-=-= Content-Type: text/x-patch Content-Disposition: attachment; filename=fbsd.diff Index: lib/libutil/Makefile =================================================================== RCS file: /home/ncvs/src/lib/libutil/Makefile,v retrieving revision 1.41 diff -u -w -r1.41 Makefile --- lib/libutil/Makefile 2001/05/18 13:41:23 1.41 +++ lib/libutil/Makefile 2001/06/18 01:02:32 @@ -6,7 +6,8 @@ SHLIB_MINOR= 0 CFLAGS+=-Wall -DLIBC_SCCS -I${.CURDIR} CFLAGS+=-DINET6 -SRCS= _secure_path.c auth.c extattr.c fparseln.c login.c login_auth.c \ +SRCS= _secure_path.c auth.c ecalloc.c emalloc.c erealloc.c estrdup.c \ + extattr.c fparseln.c login.c login_auth.c \ login_cap.c login_class.c login_crypt.c login_ok.c login_times.c \ login_tty.c logout.c logwtmp.c property.c pty.c realhostname.c stub.c \ trimdomain.c uucplock.c @@ -17,6 +18,7 @@ _secure_path.3 uucplock.3 property.3 auth.3 realhostname.3 \ realhostname_sa.3 trimdomain.3 fparseln.3 MAN+= login.conf.5 auth.conf.5 +MAN+= emalloc.3 MLINKS+= property.3 properties_read.3 property.3 properties_free.3 MLINKS+= property.3 property_find.3 MLINKS+= auth.3 auth_getval.3 @@ -38,5 +40,6 @@ MLINKS+=login_auth.3 auth_checknologin.3 login_auth.3 auth_cat.3 MLINKS+=uucplock.3 uu_lock.3 uucplock.3 uu_lock_txfr.3 \ uucplock.3 uu_unlock.3 uucplock.3 uu_lockerr.3 +MLINKS+=emalloc.3 ecalloc.3 erealloc.3 estrdup.3 .include <bsd.lib.mk> Index: lib/libutil/libutil.h =================================================================== RCS file: /home/ncvs/src/lib/libutil/libutil.h,v retrieving revision 1.32 diff -u -w -r1.32 libutil.h --- lib/libutil/libutil.h 2001/03/22 04:05:40 1.32 +++ lib/libutil/libutil.h 2001/06/18 01:02:32 @@ -76,6 +76,10 @@ #ifdef _STDIO_H_ /* avoid adding new includes */ char *fparseln __P((FILE *, size_t *, size_t *, const char[3], int)); #endif +void *emalloc (size_t); +void *ecalloc (size_t, size_t); +void *erealloc (void *, size_t); +char *estrdup (const char *); __END_DECLS #define UU_LOCK_INUSE (1) Index: sbin/fsck/Makefile =================================================================== RCS file: /home/ncvs/src/sbin/fsck/Makefile,v retrieving revision 1.9 diff -u -w -r1.9 Makefile --- sbin/fsck/Makefile 2001/03/26 14:33:01 1.9 +++ sbin/fsck/Makefile 2001/06/18 01:02:33 @@ -5,4 +5,7 @@ SRCS= fsck.c fsutil.c preen.c MAN= fsck.8 +DPADD= ${LIBUTIL} +LDADD= -lutil + .include <bsd.prog.mk> Index: sbin/fsck/fsutil.c =================================================================== RCS file: /home/ncvs/src/sbin/fsck/fsutil.c,v retrieving revision 1.2 diff -u -w -r1.2 fsutil.c --- sbin/fsck/fsutil.c 2001/04/25 07:18:22 1.2 +++ sbin/fsck/fsutil.c 2001/06/18 01:02:40 @@ -56,6 +56,7 @@ #include <sys/param.h> #include <sys/stat.h> #include <sys/mount.h> +#include <libutil.h> #include "fsutil.h" @@ -364,43 +365,3 @@ return (origname); } #endif - - -void * -emalloc(s) - size_t s; -{ - void *p; - - p = malloc(s); - if (p == NULL) - err(1, "malloc failed"); - return (p); -} - - -void * -erealloc(p, s) - void *p; - size_t s; -{ - void *q; - - q = realloc(p, s); - if (q == NULL) - err(1, "realloc failed"); - return (q); -} - - -char * -estrdup(s) - const char *s; -{ - char *p; - - p = strdup(s); - if (p == NULL) - err(1, "strdup failed"); - return (p); -} Index: usr.bin/column/Makefile =================================================================== RCS file: /home/ncvs/src/usr.bin/column/Makefile,v retrieving revision 1.2 diff -u -w -r1.2 Makefile --- usr.bin/column/Makefile 1998/12/06 22:58:18 1.2 +++ usr.bin/column/Makefile 2001/06/18 01:02:59 @@ -3,4 +3,7 @@ PROG= column CFLAGS+=-Wall +DPADD= ${LIBUTIL} +LDADD= -lutil + .include <bsd.prog.mk> Index: usr.bin/column/column.c =================================================================== RCS file: /home/ncvs/src/usr.bin/column/column.c,v retrieving revision 1.5 diff -u -w -r1.5 column.c --- usr.bin/column/column.c 2001/04/03 18:03:29 1.5 +++ usr.bin/column/column.c 2001/06/18 01:02:59 @@ -53,11 +53,11 @@ #include <stdlib.h> #include <string.h> #include <unistd.h> +#include <libutil.h> #define TAB 8 void c_columnate __P((void)); -void *emalloc __P((int)); void input __P((FILE *)); void maketbl __P((void)); void print __P((void)); @@ -216,9 +216,9 @@ TBL *tbl; char **cols; - t = tbl = emalloc(entries * sizeof(TBL)); - cols = emalloc((maxcols = DEFCOLS) * sizeof(char *)); - lens = emalloc(maxcols * sizeof(int)); + t = tbl = ecalloc(entries, sizeof(TBL)); + cols = ecalloc((maxcols = DEFCOLS), sizeof(char *)); + lens = ecalloc(maxcols, sizeof(int)); for (cnt = 0, lp = list; cnt < entries; ++cnt, ++lp, ++t) { for (coloff = 0, p = *lp; (cols[coloff] = strtok(p, separator)); p = NULL) @@ -232,8 +232,8 @@ 0, DEFCOLS * sizeof(int)); maxcols += DEFCOLS; } - t->list = emalloc(coloff * sizeof(char *)); - t->len = emalloc(coloff * sizeof(int)); + t->list = ecalloc(coloff, sizeof(char *)); + t->len = ecalloc(coloff, sizeof(int)); for (t->cols = coloff; --coloff >= 0;) { t->list[coloff] = cols[coloff]; t->len[coloff] = strlen(cols[coloff]); @@ -261,7 +261,7 @@ char *p, buf[MAXLINELEN]; if (!list) - list = emalloc((maxentry = DEFNUM) * sizeof(char *)); + list = ecalloc((maxentry = DEFNUM), sizeof(char *)); while (fgets(buf, MAXLINELEN, fp)) { for (p = buf; *p && isspace(*p); ++p); if (!*p) @@ -283,18 +283,6 @@ } list[entries++] = strdup(buf); } -} - -void * -emalloc(size) - int size; -{ - char *p; - - if (!(p = malloc(size))) - err(1, NULL); - memset(p, 0, size); - return (p); } void Index: usr.bin/find/Makefile =================================================================== RCS file: /home/ncvs/src/usr.bin/find/Makefile,v retrieving revision 1.11 diff -u -w -r1.11 Makefile --- usr.bin/find/Makefile 2001/05/03 18:05:31 1.11 +++ usr.bin/find/Makefile 2001/06/18 01:03:00 @@ -8,4 +8,7 @@ CFLAGS+= -I${.CURDIR}/../../gnu/usr.bin/cvs/lib -DHAVE_CONFIG_H .PATH: ${.CURDIR}/../../contrib/cvs/lib +DPADD= ${LIBUTIL} +LDADD= -lutil + .include <bsd.prog.mk> Index: usr.bin/find/extern.h =================================================================== RCS file: /home/ncvs/src/usr.bin/find/extern.h,v retrieving revision 1.13 diff -u -w -r1.13 extern.h --- usr.bin/find/extern.h 2001/05/03 18:05:33 1.13 +++ usr.bin/find/extern.h 2001/06/18 01:03:00 @@ -37,7 +37,6 @@ #include <sys/cdefs.h> void brace_subst __P((char *, char **, char *, int)); -void *emalloc __P((unsigned int)); PLAN *find_create __P((char ***)); int find_execute __P((PLAN *, char **)); PLAN *find_formplan __P((char **)); Index: usr.bin/find/function.c =================================================================== RCS file: /home/ncvs/src/usr.bin/find/function.c,v retrieving revision 1.30 diff -u -w -r1.30 function.c --- usr.bin/find/function.c 2001/05/03 18:05:34 1.30 +++ usr.bin/find/function.c 2001/06/18 01:03:00 @@ -62,6 +62,7 @@ #include <stdlib.h> #include <string.h> #include <unistd.h> +#include <libutil.h> #include "find.h" Index: usr.bin/find/misc.c =================================================================== RCS file: /home/ncvs/src/usr.bin/find/misc.c,v retrieving revision 1.3 diff -u -w -r1.3 misc.c --- usr.bin/find/misc.c 2000/06/12 11:12:41 1.3 +++ usr.bin/find/misc.c 2001/06/18 01:03:00 @@ -115,18 +115,3 @@ } return (first == 'y'); } - -/* - * emalloc -- - * malloc with error checking. - */ -void * -emalloc(len) - u_int len; -{ - void *p; - - if ((p = malloc(len)) == NULL) - err(1, NULL); - return (p); -} Index: usr.bin/hexdump/Makefile =================================================================== RCS file: /home/ncvs/src/usr.bin/hexdump/Makefile,v retrieving revision 1.4 diff -u -w -r1.4 Makefile --- usr.bin/hexdump/Makefile 2001/03/27 10:51:45 1.4 +++ usr.bin/hexdump/Makefile 2001/06/18 01:03:01 @@ -8,4 +8,7 @@ LINKS= ${BINDIR}/hexdump ${BINDIR}/od LINKS+= ${BINDIR}/hexdump ${BINDIR}/hd +DPADD= ${LIBUTIL} +LDADD= -lutil + .include <bsd.prog.mk> Index: usr.bin/hexdump/display.c =================================================================== RCS file: /home/ncvs/src/usr.bin/hexdump/display.c,v retrieving revision 1.6 diff -u -w -r1.6 display.c --- usr.bin/hexdump/display.c 2000/07/10 09:07:04 1.6 +++ usr.bin/hexdump/display.c 2001/06/18 01:03:01 @@ -48,6 +48,7 @@ #include <stdlib.h> #include <string.h> #include <unistd.h> +#include <libutil.h> #include "hexdump.h" enum _vflag vflag = FIRST; @@ -238,8 +239,8 @@ u_char *tmpp; if (!curp) { - curp = emalloc(blocksize); - savp = emalloc(blocksize); + curp = ecalloc(blocksize, 1); + savp = ecalloc(blocksize, 1); } else { tmpp = curp; curp = savp; @@ -365,22 +366,4 @@ address += cnt; skip -= cnt; } -} - -void * -emalloc(size) - int size; -{ - void *p; - - if ((p = malloc((u_int)size)) == NULL) - nomem(); - bzero(p, size); - return(p); -} - -void -nomem() -{ - err(1, NULL); } Index: usr.bin/hexdump/hexdump.h =================================================================== RCS file: /home/ncvs/src/usr.bin/hexdump/hexdump.h,v retrieving revision 1.2 diff -u -w -r1.2 hexdump.h --- usr.bin/hexdump/hexdump.h 1997/07/10 06:48:18 1.2 +++ usr.bin/hexdump/hexdump.h 2001/06/18 01:03:01 @@ -85,12 +85,10 @@ void conv_u __P((PR *, u_char *)); void display __P((void)); void doskip __P((char *, int)); -void *emalloc __P((int)); void escape __P((char *)); u_char *get __P((void)); void newsyntax __P((int, char ***)); int next __P((char **)); -void nomem __P((void)); void oldsyntax __P((int, char ***)); void rewrite __P((FS *)); int size __P((FS *)); Index: usr.bin/hexdump/parse.c =================================================================== RCS file: /home/ncvs/src/usr.bin/hexdump/parse.c,v retrieving revision 1.4 diff -u -w -r1.4 parse.c --- usr.bin/hexdump/parse.c 1999/08/28 01:02:03 1.4 +++ usr.bin/hexdump/parse.c 2001/06/18 01:03:02 @@ -140,8 +140,7 @@ for (savep = ++p; *p != '"';) if (*p++ == 0) badfmt(fmt); - if (!(tfu->fmt = malloc(p - savep + 1))) - nomem(); + tfu->fmt = emalloc(p - savep + 1); (void) strncpy(tfu->fmt, savep, p - savep); tfu->fmt[p - savep] = '\0'; escape(tfu->fmt); Index: usr.bin/make/Makefile =================================================================== RCS file: /home/ncvs/src/usr.bin/make/Makefile,v retrieving revision 1.19 diff -u -w -r1.19 Makefile --- usr.bin/make/Makefile 2001/05/18 09:05:56 1.19 +++ usr.bin/make/Makefile 2001/06/18 01:03:07 @@ -30,4 +30,7 @@ .error "MAKE_SHELL must be set to one of \"csh\", \"sh\" or \"ksh\"." .endif +DPADD= ${LIBUTIL} +LDADD= -lutil + .include <bsd.prog.mk> Index: usr.bin/make/main.c =================================================================== RCS file: /home/ncvs/src/usr.bin/make/main.c,v retrieving revision 1.49 diff -u -w -r1.49 main.c --- usr.bin/make/main.c 2001/04/25 14:44:41 1.49 +++ usr.bin/make/main.c 2001/06/18 01:03:07 @@ -1242,60 +1242,6 @@ } /* - * emalloc -- - * malloc, but die on error. - */ -void * -emalloc(len) - size_t len; -{ - void *p; - - if ((p = malloc(len)) == NULL) - enomem(); - return(p); -} - -/* - * estrdup -- - * strdup, but die on error. - */ -char * -estrdup(str) - const char *str; -{ - char *p; - - if ((p = strdup(str)) == NULL) - enomem(); - return(p); -} - -/* - * erealloc -- - * realloc, but die on error. - */ -void * -erealloc(ptr, size) - void *ptr; - size_t size; -{ - if ((ptr = realloc(ptr, size)) == NULL) - enomem(); - return(ptr); -} - -/* - * enomem -- - * die when out of memory. - */ -void -enomem() -{ - err(2, NULL); -} - -/* * enunlink -- * Remove a file carefully, avoiding directories. */ Index: usr.bin/make/make.h =================================================================== RCS file: /home/ncvs/src/usr.bin/make/make.h,v retrieving revision 1.15 diff -u -w -r1.15 make.h --- usr.bin/make/make.h 2001/01/21 08:24:41 1.15 +++ usr.bin/make/make.h 2001/06/18 01:03:07 @@ -76,6 +76,7 @@ #include <stdlib.h> #include <unistd.h> #endif +#include <libutil.h> #include "sprite.h" #include "lst.h" #include "config.h" Index: usr.bin/make/nonints.h =================================================================== RCS file: /home/ncvs/src/usr.bin/make/nonints.h,v retrieving revision 1.9 diff -u -w -r1.9 nonints.h --- usr.bin/make/nonints.h 2000/12/02 20:24:38 1.9 +++ usr.bin/make/nonints.h 2001/06/18 01:03:07 @@ -71,13 +71,9 @@ void DieHorribly __P((void)); int PrintAddr __P((void *, void *)); void Finish __P((int)); -char *estrdup __P((const char *)); -void *emalloc __P((size_t)); /* efree(x) works when x==NULL. STDC behavior, may need some different * definition for cross-builds on deficient systems */ #define efree free -void *erealloc __P((void *, size_t)); -void enomem __P((void)); int eunlink __P((const char *)); /* parse.c */ Index: usr.bin/nm/Makefile =================================================================== RCS file: /home/ncvs/src/usr.bin/nm/Makefile,v retrieving revision 1.4 diff -u -w -r1.4 Makefile --- usr.bin/nm/Makefile 2001/03/27 10:51:53 1.4 +++ usr.bin/nm/Makefile 2001/06/18 01:03:10 @@ -5,4 +5,7 @@ BINDIR= /usr/libexec/aout MAN= nm.1aout +DPADD= ${LIBUTIL} +LDADD= -lutil + .include <bsd.prog.mk> Index: usr.bin/nm/nm.c =================================================================== RCS file: /home/ncvs/src/usr.bin/nm/nm.c,v retrieving revision 1.15 diff -u -w -r1.15 nm.c --- usr.bin/nm/nm.c 2001/02/06 11:21:21 1.15 +++ usr.bin/nm/nm.c 2001/06/18 01:03:10 @@ -60,6 +60,7 @@ #include <stdlib.h> #include <string.h> #include <unistd.h> +#include <libutil.h> int ignore_bad_archive_entries = 1; int print_only_external_symbols; @@ -79,7 +80,6 @@ #define SYMBOL_TYPE(x) ((x) & (N_TYPE | N_STAB)) #define SYMBOL_BIND(x) (((x) >> 4) & 0xf) -void *emalloc(); static void usage __P(( void )); int process_file __P(( char * )); int show_archive __P(( char *, FILE * )); @@ -652,18 +652,6 @@ return(fname(a0, b0)); return(a->n_value > b->n_value ? 1 : -1); } -} - -void * -emalloc(size) - size_t size; -{ - char *p; - - /* NOSTRICT */ - if ( (p = malloc(size)) ) - return(p); - err(1, NULL); } static void Index: usr.bin/ranlib/Makefile =================================================================== RCS file: /home/ncvs/src/usr.bin/ranlib/Makefile,v retrieving revision 1.8 diff -u -w -r1.8 Makefile --- usr.bin/ranlib/Makefile 2001/03/27 10:52:05 1.8 +++ usr.bin/ranlib/Makefile 2001/06/18 01:03:10 @@ -8,4 +8,7 @@ BINDIR= /usr/libexec/aout VPATH= ${.CURDIR}/../ar +DPADD= ${LIBUTIL} +LDADD= -lutil + .include <bsd.prog.mk> Index: usr.bin/ranlib/build.c =================================================================== RCS file: /home/ncvs/src/usr.bin/ranlib/build.c,v retrieving revision 1.7 diff -u -w -r1.7 build.c --- usr.bin/ranlib/build.c 1999/08/28 01:05:01 1.7 +++ usr.bin/ranlib/build.c 2001/06/18 01:03:10 @@ -54,6 +54,7 @@ #include <stdlib.h> #include <string.h> #include <unistd.h> +#include <libutil.h> #include "archive.h" @@ -143,7 +144,6 @@ struct nlist nl; off_t r_off, w_off; long strsize; - void *emalloc(); /* Get current offsets for original and tmp files. */ r_off = lseek(rfd, (off_t)0, SEEK_CUR); Index: usr.bin/ranlib/misc.c =================================================================== RCS file: /home/ncvs/src/usr.bin/ranlib/misc.c,v retrieving revision 1.5 diff -u -w -r1.5 misc.c --- usr.bin/ranlib/misc.c 1999/08/28 01:05:02 1.5 +++ usr.bin/ranlib/misc.c 2001/06/18 01:03:10 @@ -79,17 +79,6 @@ return(fd); } -void * -emalloc(len) - int len; -{ - void *p; - - if ((p = malloc((u_int)len)) == NULL) - error(archive); - return(p); -} - char * rname(path) char *path; --- /dev/null Mon Jun 18 02:00:57 2001 +++ lib/libutil/ecalloc.c Sun Jun 17 14:55:39 2001 @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2001 Assar Westerlund + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * $FreeBSD$ + */ + +#include <stdlib.h> +#include <err.h> + +#include <sys/types.h> +#include "libutil.h" + +/* + * Like calloc but never fails. + */ + +void * +ecalloc(size_t number, size_t size) +{ + void *tmp = calloc(number, size); + + if (tmp == NULL && number * size != 0) + errx(1, "calloc %lu failed", (unsigned long)number * size); + return tmp; +} --- /dev/null Mon Jun 18 02:00:57 2001 +++ lib/libutil/emalloc.c Sun Jun 17 14:55:18 2001 @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2001 Assar Westerlund + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * $FreeBSD$ + */ + +#include <stdlib.h> +#include <err.h> + +#include <sys/types.h> +#include "libutil.h" + +/* + * Like malloc but never fails. + */ + +void * +emalloc(size_t sz) +{ + void *tmp = malloc(sz); + + if (tmp == NULL && sz != 0) + errx(1, "malloc %lu failed", (unsigned long)sz); + return tmp; +} --- /dev/null Mon Jun 18 02:00:57 2001 +++ lib/libutil/estrdup.c Sun Jun 17 14:55:52 2001 @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2001 Assar Westerlund + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * $FreeBSD$ + */ + +#include <stdlib.h> +#include <string.h> +#include <err.h> + +#include <sys/types.h> +#include "libutil.h" + +/* + * Like strdup but never fails. + */ + +char * +estrdup(const char *str) +{ + void *tmp = strdup(str); + + if (tmp == NULL) + errx(1, "strdup"); + return tmp; +} --- /dev/null Mon Jun 18 02:00:57 2001 +++ lib/libutil/erealloc.c Sun Jun 17 14:55:30 2001 @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2001 Assar Westerlund + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * $FreeBSD$ + */ + +#include <stdlib.h> +#include <err.h> + +#include <sys/types.h> +#include "libutil.h" + +/* + * Like realloc but never fails. + */ + +void * +erealloc(void *ptr, size_t sz) +{ + void *tmp = realloc(ptr, sz); + + if (tmp == NULL && sz != 0) + errx(1, "realloc %lu failed", (unsigned long)sz); + return tmp; +} --- /dev/null Mon Jun 18 02:00:57 2001 +++ lib/libutil/emalloc.3 Sun Jun 17 12:51:33 2001 @@ -0,0 +1,64 @@ +.\" Copyright (c) 2001 Assar Westerlund +.\" All rights reserved. +.\" +.\" Redistribution and use in source and binary forms, with or without +.\" modification, are permitted provided that the following conditions +.\" are met: +.\" +.\" 1. Redistributions of source code must retain the above copyright +.\" notice, this list of conditions and the following disclaimer. +.\" 2. Redistributions in binary form must reproduce the above copyright +.\" notice, this list of conditions and the following disclaimer in the +.\" documentation and/or other materials provided with the distribution. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND +.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE +.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY +.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +.\" SUCH DAMAGE. +.\" +.\" $FreeBSD$ +.\" +.Dd June 17, 2001 +.Os +.Dt EMALLOC 3 +.Sh NAME +.Nm emalloc , ecalloc , erealloc , estrdup +.Nd non-failing memory allocation functions +.Sh LIBRARY +.Lb libutil +.Sh SYNOPSIS +.Fd #include <sys/types.h> +.Fd #include <libutil.h> +.Ft void * +.Fn emalloc "size_t size" +.Ft void * +.Fn ecalloc "size_t number" "size_t size" +.Ft void * +.Fn erealloc "void *ptr" "size_t size" +.Ft char * +.Fn estrdup "const char *str" +.Sh DESCRIPTION +These functions work as +.Fn malloc , +.Fn calloc , +.Fn realloc , +and +.Fn strdup , +except that they never return +.Dv NULL , +but instead call +.Fn errx +to abort the program if memory allocation fails. +.Sh SEE ALSO +.Xr malloc 3 , +.Xr calloc 3 , +.Xr realloc 3 , +.Xr strdup 3 , +.Xr errx 3 --=-=-=-- To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-audit" in the body of the message
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?5ld782twfj.fsf>