diff options
author | joerg <joerg> | 2008-08-02 20:33:50 +0000 |
---|---|---|
committer | joerg <joerg> | 2008-08-02 20:33:50 +0000 |
commit | 7be93b44a0d2e33aa0dd07de5f5b673bf95b27c8 (patch) | |
tree | 8901b5966bac844405a560018181d94cc4eccd35 | |
parent | f3a58140319f67643ea733850de213b695037b07 (diff) | |
download | pkgsrc-7be93b44a0d2e33aa0dd07de5f5b673bf95b27c8.tar.gz |
Most memory allocation failures were fatal already and the majority of
the rest lacked an explicit check. Add the usual x* wrappers around
malloc and friends that explicitly terminate on error and use them in
all but Dewey.
29 files changed, 281 insertions, 333 deletions
diff --git a/pkgtools/pkg_install/files/add/perform.c b/pkgtools/pkg_install/files/add/perform.c index 156afc0d92e..d2c51244fd9 100644 --- a/pkgtools/pkg_install/files/add/perform.c +++ b/pkgtools/pkg_install/files/add/perform.c @@ -1,4 +1,4 @@ -/* $NetBSD: perform.c,v 1.70.4.13 2008/07/30 22:26:03 joerg Exp $ */ +/* $NetBSD: perform.c,v 1.70.4.14 2008/08/02 20:33:50 joerg Exp $ */ #if HAVE_CONFIG_H #include "config.h" #endif @@ -6,7 +6,7 @@ #if HAVE_SYS_CDEFS_H #include <sys/cdefs.h> #endif -__RCSID("$NetBSD: perform.c,v 1.70.4.13 2008/07/30 22:26:03 joerg Exp $"); +__RCSID("$NetBSD: perform.c,v 1.70.4.14 2008/08/02 20:33:50 joerg Exp $"); /*- * Copyright (c) 2003 Grant Beattie <grant@NetBSD.org> @@ -137,10 +137,7 @@ mkdir_p(const char *path) if (errno != ENOENT) return -1; - if ((p = strdup(path)) == NULL) - err(EXIT_FAILURE, "strdup failed"); - - cur_end = p; + cur_end = p = xstrdup(path); for (;;) { /* @@ -231,8 +228,7 @@ skip_header: warnx("package meta data too large to process"); return -1; } - if ((*target = malloc(size + 1)) == NULL) - err(2, "cannot allocate meta data"); + *target = xmalloc(size + 1); if (archive_read_data(pkg->archive, *target, size) != size) { warnx("cannot read package meta data"); return -1; @@ -284,7 +280,7 @@ pkg_parse_plist(struct pkg_task *pkg) return -1; } if (pkg->pkgname == NULL) - pkg->pkgname = strdup(p->name); + pkg->pkgname = xstrdup(p->name); else if (strcmp(pkg->pkgname, p->name) != 0) { warnx("Signature and PLIST differ on package name"); return -1; @@ -307,12 +303,10 @@ pkg_parse_plist(struct pkg_task *pkg) } else pkg->prefix = p->name; - if (Destdir != NULL) { - if (asprintf(&pkg->install_prefix, "%s/%s", Destdir, - pkg->prefix) == -1) - err(EXIT_FAILURE, "asprintf failed"); - } else if ((pkg->install_prefix = strdup(pkg->prefix)) == NULL) - err(EXIT_FAILURE, "strdup failed"); + if (Destdir != NULL) + pkg->install_prefix = xasprintf("%s/%s", Destdir, pkg->prefix); + else + pkg->install_prefix = xstrdup(pkg->prefix); return 0; } @@ -328,9 +322,7 @@ dup_value(const char *line, const char *eol) char *val; key = strchr(line, '='); - val = malloc(eol - key); - if (val == NULL) - err(2, "malloc failed"); + val = xmalloc(eol - key); memcpy(val, key + 1, eol - key - 1); val[eol - key - 1] = '\0'; return val; @@ -376,10 +368,8 @@ check_other_installed(struct pkg_task *pkg) plist_t *p; int status; - if ((pkgbase = strdup(pkg->pkgname)) == NULL) { - warnx("strdup failed"); - return -1; - } + pkgbase = xstrdup(pkg->pkgname); + if ((iter = strrchr(pkgbase, '-')) == NULL) { free(pkgbase); warnx("Invalid package name %s", pkg->pkgname); @@ -542,11 +532,8 @@ write_meta_data(struct pkg_task *pkg) descr->entry_offset); if (*target == NULL) continue; - if (asprintf(&filename, "%s/%s", pkg->install_logdir, - descr->entry_filename) == -1) { - warn("asprintf failed"); - return -1; - } + filename = xasprintf("%s/%s", pkg->install_logdir, + descr->entry_filename); (void)unlink(filename); fd = open(filename, O_WRONLY | O_TRUNC | O_CREAT, descr->perm); if (fd == -1) { @@ -668,10 +655,7 @@ extract_files(struct pkg_task *pkg) p->name, archive_entry_pathname(pkg->entry)); goto out; } - if (asprintf(&fullpath, "%s/%s", pkg->prefix, p->name) == -1) { - warnx("asprintf failed"); - goto out; - } + fullpath = xasprintf("%s/%s", pkg->prefix, p->name); pkgdb_store(fullpath, pkg->pkgname); free(fullpath); if (Verbose) @@ -778,8 +762,7 @@ pkg_register_depends(struct pkg_task *pkg) if (pkg->other_version != NULL) return; /* XXX It's using the old dependencies. */ - if (asprintf(&text, "%s\n", pkg->pkgname) == -1) - err(2, "asprintf failed"); + text = xasprintf("%s\n", pkg->pkgname); text_len = strlen(text); for (i = 0; i < pkg->dep_length; ++i) { @@ -972,11 +955,7 @@ check_implicit_conflict(struct pkg_task *pkg) } else if (p->type != PLIST_FILE) continue; - if (asprintf(&fullpath, "%s/%s", pkg->prefix, p->name) == -1) { - warnx("asprintf failed"); - status = -1; - break; - } + fullpath = xasprintf("%s/%s", pkg->prefix, p->name); existing = pkgdb_retrieve(fullpath); free(fullpath); if (existing == NULL) @@ -1056,17 +1035,8 @@ check_dependencies(struct pkg_task *pkg) if (pkg->dep_length + 1 >= pkg->dep_allocated) { char **tmp; pkg->dep_allocated = 2 * pkg->dep_allocated + 1; - tmp = realloc(pkg->dependencies, + pkg->dependencies = xrealloc(pkg->dependencies, pkg->dep_allocated * sizeof(*tmp)); - if (tmp == NULL) { - warnx("realloc failed"); - free(pkg->dependencies); - pkg->dependencies = NULL; - pkg->dep_length = pkg->dep_allocated = 0; - free(best_installed); - return -1; - } - pkg->dependencies = tmp; } pkg->dependencies[pkg->dep_length++] = best_installed; } @@ -1217,8 +1187,7 @@ pkg_do(const char *pkgpath, int mark_automatic) void *signature_cookie; struct pkg_task *pkg; - if ((pkg = calloc(1, sizeof(*pkg))) == NULL) - err(2, "malloc failed"); + pkg = xcalloc(1, sizeof(*pkg)); status = -1; @@ -1249,21 +1218,18 @@ pkg_do(const char *pkgpath, int mark_automatic) warnx("mtree specification in pkg `%s' ignored", pkg->pkgname); if (pkg->meta_data.meta_views != NULL) { - if ((pkg->logdir = strdup(pkg->prefix)) == NULL) - err(EXIT_FAILURE, "strdup failed"); + pkg->logdir = xstrdup(pkg->prefix); _pkgdb_setPKGDB_DIR(dirname_of(pkg->logdir)); } else { - if (asprintf(&pkg->logdir, "%s/%s", _pkgdb_getPKGDB_DIR(), - pkg->pkgname) == -1) - err(EXIT_FAILURE, "asprintf failed"); + pkg->logdir = xasprintf("%s/%s", _pkgdb_getPKGDB_DIR(), + pkg->pkgname); } if (Destdir != NULL) { - if (asprintf(&pkg->install_logdir, "%s/%s", Destdir, pkg->logdir) == -1) - err(EXIT_FAILURE, "asprintf failed"); + pkg->install_logdir = xasprintf("%s/%s", Destdir, pkg->logdir); _pkgdb_setPKGDB_DIR(dirname_of(pkg->install_logdir)); - } else if ((pkg->install_logdir = strdup(pkg->logdir)) == NULL) - err(EXIT_FAILURE, "strdup failed"); + } else + pkg->install_logdir = xstrdup(pkg->logdir); if (NoRecord && !Fake) { const char *tmpdir; @@ -1273,8 +1239,7 @@ pkg_do(const char *pkgpath, int mark_automatic) tmpdir = "/tmp"; free(pkg->install_logdir); - if (asprintf(&pkg->install_logdir, "%s/pkg_install.XXXXXX", tmpdir) == -1) - err(EXIT_FAILURE, "asprintf failed"); + pkg->install_logdir = xasprintf("%s/pkg_install.XXXXXX", tmpdir); /* XXX pkg_add -u... */ if (mkdtemp(pkg->install_logdir) == NULL) { warn("mkdtemp failed"); diff --git a/pkgtools/pkg_install/files/admin/audit.c b/pkgtools/pkg_install/files/admin/audit.c index 1c9fb888dcd..2243686689f 100644 --- a/pkgtools/pkg_install/files/admin/audit.c +++ b/pkgtools/pkg_install/files/admin/audit.c @@ -1,4 +1,4 @@ -/* $NetBSD: audit.c,v 1.8.2.2 2008/07/27 16:22:53 joerg Exp $ */ +/* $NetBSD: audit.c,v 1.8.2.3 2008/08/02 20:33:50 joerg Exp $ */ #if HAVE_CONFIG_H #include "config.h" @@ -8,7 +8,7 @@ #include <sys/cdefs.h> #endif #ifndef lint -__RCSID("$NetBSD: audit.c,v 1.8.2.2 2008/07/27 16:22:53 joerg Exp $"); +__RCSID("$NetBSD: audit.c,v 1.8.2.3 2008/08/02 20:33:50 joerg Exp $"); #endif /*- @@ -363,8 +363,7 @@ fetch_pkg_vulnerabilities(int argc, char **argv) err(EXIT_FAILURE, "pkg-vulnerabilities is too large"); buf_len = st.size; - if ((buf = malloc(buf_len + 1)) == NULL) - err(EXIT_FAILURE, "malloc failed"); + buf = xmalloc(buf_len + 1); if (fetchIO_read(f, buf, buf_len) != buf_len) err(EXIT_FAILURE, "Failure during fetch of pkg-vulnerabilities"); @@ -455,9 +454,7 @@ check_pkg_history1(const char *pkg, const char *pattern) open_brace = inner_brace; } - expanded_pkg = malloc(strlen(pattern)); /* {} are going away... */ - if (expanded_pkg == NULL) - err(EXIT_FAILURE, "malloc failed"); + expanded_pkg = xmalloc(strlen(pattern)); /* {} are going away... */ prefix_len = open_brace - pattern; suffix = close_brace + 1; diff --git a/pkgtools/pkg_install/files/admin/check.c b/pkgtools/pkg_install/files/admin/check.c index 07ee2b84c39..d2b835f6c4e 100644 --- a/pkgtools/pkg_install/files/admin/check.c +++ b/pkgtools/pkg_install/files/admin/check.c @@ -1,4 +1,4 @@ -/* $NetBSD: check.c,v 1.2.4.2 2008/05/23 15:57:04 joerg Exp $ */ +/* $NetBSD: check.c,v 1.2.4.3 2008/08/02 20:33:50 joerg Exp $ */ #if HAVE_CONFIG_H #include "config.h" @@ -8,7 +8,7 @@ #include <sys/cdefs.h> #endif #ifndef lint -__RCSID("$NetBSD: check.c,v 1.2.4.2 2008/05/23 15:57:04 joerg Exp $"); +__RCSID("$NetBSD: check.c,v 1.2.4.3 2008/08/02 20:33:50 joerg Exp $"); #endif /*- @@ -234,8 +234,7 @@ check_pkg(const char *pkg, int *filecnt, int *pkgcnt, int allow_unmatched) errx(EXIT_FAILURE, "No matching pkg for %s.", pkg); } - if (asprintf(&pattern, "%s-[0-9]*", pkg) == -1) - errx(EXIT_FAILURE, "asprintf failed"); + pattern = xasprintf("%s-[0-9]*", pkg); if (match_installed_pkgs(pattern, checkpattern_fn, &arg) == -1) errx(EXIT_FAILURE, "Cannot process pkdbdb"); diff --git a/pkgtools/pkg_install/files/admin/main.c b/pkgtools/pkg_install/files/admin/main.c index 515795a7926..19cb7bfc8d6 100644 --- a/pkgtools/pkg_install/files/admin/main.c +++ b/pkgtools/pkg_install/files/admin/main.c @@ -1,4 +1,4 @@ -/* $NetBSD: main.c,v 1.42.2.5 2008/06/04 11:23:13 joerg Exp $ */ +/* $NetBSD: main.c,v 1.42.2.6 2008/08/02 20:33:50 joerg Exp $ */ #if HAVE_CONFIG_H #include "config.h" @@ -8,7 +8,7 @@ #include <sys/cdefs.h> #endif #ifndef lint -__RCSID("$NetBSD: main.c,v 1.42.2.5 2008/06/04 11:23:13 joerg Exp $"); +__RCSID("$NetBSD: main.c,v 1.42.2.6 2008/08/02 20:33:50 joerg Exp $"); #endif /*- @@ -660,7 +660,7 @@ set_unset_variable(char **argv, Boolean unset) if ((eq=strchr(argv[0], '=')) == NULL) usage(); - variable = malloc(eq-argv[0]+1); + variable = xmalloc(eq-argv[0]+1); strlcpy(variable, argv[0], eq-argv[0]+1); arg.variable = variable; @@ -692,8 +692,7 @@ set_unset_variable(char **argv, Boolean unset) warnx("no matching pkg for `%s'", *argv); ret++; } else { - if (asprintf(&pattern, "%s-[0-9]*", *argv) == -1) - errx(EXIT_FAILURE, "asprintf failed"); + pattern = xasprintf("%s-[0-9]*", *argv); if (match_installed_pkgs(pattern, set_installed_info_var, &arg) == -1) errx(EXIT_FAILURE, "Cannot process pkdbdb"); diff --git a/pkgtools/pkg_install/files/create/build.c b/pkgtools/pkg_install/files/create/build.c index 4cc1a405e21..c61337b3bd3 100644 --- a/pkgtools/pkg_install/files/create/build.c +++ b/pkgtools/pkg_install/files/create/build.c @@ -1,4 +1,4 @@ -/* $NetBSD: build.c,v 1.8 2008/04/23 16:58:07 joerg Exp $ */ +/* $NetBSD: build.c,v 1.8.2.1 2008/08/02 20:33:50 joerg Exp $ */ #if HAVE_CONFIG_H #include "config.h" @@ -11,7 +11,7 @@ #if 0 static const char *rcsid = "from FreeBSD Id: perform.c,v 1.38 1997/10/13 15:03:51 jkh Exp"; #else -__RCSID("$NetBSD: build.c,v 1.8 2008/04/23 16:58:07 joerg Exp $"); +__RCSID("$NetBSD: build.c,v 1.8.2.1 2008/08/02 20:33:50 joerg Exp $"); #endif #endif @@ -251,8 +251,7 @@ make_dist(const char *pkg, const char *suffix, const package_t *plist) else archive_write_set_compression_none(archive); - if (asprintf(&archive_name, "%s.%s", pkg, suffix) == -1) - err(2, "cannot compute output name"); + archive_name = xasprintf("%s.%s", pkg, suffix); if (archive_write_open_file(archive, archive_name)) errx(2, "cannot create archive: %s", archive_error_string(archive)); diff --git a/pkgtools/pkg_install/files/create/perform.c b/pkgtools/pkg_install/files/create/perform.c index 73d12c74771..6cdcbadbe47 100644 --- a/pkgtools/pkg_install/files/create/perform.c +++ b/pkgtools/pkg_install/files/create/perform.c @@ -1,4 +1,4 @@ -/* $NetBSD: perform.c,v 1.19.2.1 2008/04/26 17:44:23 joerg Exp $ */ +/* $NetBSD: perform.c,v 1.19.2.2 2008/08/02 20:33:50 joerg Exp $ */ #if HAVE_CONFIG_H #include "config.h" @@ -11,7 +11,7 @@ #if 0 static const char *rcsid = "from FreeBSD Id: perform.c,v 1.38 1997/10/13 15:03:51 jkh Exp"; #else -__RCSID("$NetBSD: perform.c,v 1.19.2.1 2008/04/26 17:44:23 joerg Exp $"); +__RCSID("$NetBSD: perform.c,v 1.19.2.2 2008/08/02 20:33:50 joerg Exp $"); #endif #endif @@ -106,7 +106,7 @@ fileGetContents(char *fname) errx(2, "can't stat '%s'", fname); } - contents = (char *) malloc((size_t) (sb.st_size) + 1); + contents = xmalloc((size_t) (sb.st_size) + 1); fd = open(fname, O_RDONLY, 0); if (fd == FAIL) { cleanup(0); @@ -129,7 +129,7 @@ static void get_dash_string(char **s) { if (**s == '-') - *s = strdup(*s + 1); + *s = xstrdup(*s + 1); else *s = fileGetContents(*s); } @@ -146,8 +146,7 @@ pkg_perform(const char *pkg) /* Break the package name into base and desired suffix (if any) */ if ((cp = strrchr(pkg, '.')) != NULL) { - if ((allocated_pkg = malloc(cp - pkg + 1)) == NULL) - err(2, "malloc failed"); + allocated_pkg = xmalloc(cp - pkg + 1); memcpy(allocated_pkg, pkg, cp - pkg); allocated_pkg[cp - pkg] = '\0'; suffix = cp + 1; diff --git a/pkgtools/pkg_install/files/create/pl.c b/pkgtools/pkg_install/files/create/pl.c index 7d19c77736b..5da4fa731d0 100644 --- a/pkgtools/pkg_install/files/create/pl.c +++ b/pkgtools/pkg_install/files/create/pl.c @@ -1,4 +1,4 @@ -/* $NetBSD: pl.c,v 1.10.8.1 2008/05/23 15:51:22 joerg Exp $ */ +/* $NetBSD: pl.c,v 1.10.8.2 2008/08/02 20:33:50 joerg Exp $ */ #if HAVE_CONFIG_H #include "config.h" @@ -11,7 +11,7 @@ #if 0 static const char *rcsid = "from FreeBSD Id: pl.c,v 1.11 1997/10/08 07:46:35 charnier Exp"; #else -__RCSID("$NetBSD: pl.c,v 1.10.8.1 2008/05/23 15:51:22 joerg Exp $"); +__RCSID("$NetBSD: pl.c,v 1.10.8.2 2008/08/02 20:33:50 joerg Exp $"); #endif #endif @@ -98,22 +98,18 @@ reorder(package_t *pkg, int dirc) char **dirv; int i; - if ((dirv = (char **) calloc(dirc, sizeof(char *))) == (char **) NULL) { - warn("No directory re-ordering will be done"); - } else { - for (p = pkg->head, i = 0; p; p = p->next) { - if (p->type == PLIST_DIR_RM) { - dirv[i++] = p->name; - } - } - qsort(dirv, dirc, sizeof(char *), dircmp); - for (p = pkg->head, i = 0; p; p = p->next) { - if (p->type == PLIST_DIR_RM) { - p->name = dirv[i++]; - } - } - (void) free(dirv); + dirv = xcalloc(dirc, sizeof(char *)); + + for (p = pkg->head, i = 0; p; p = p->next) { + if (p->type == PLIST_DIR_RM) + dirv[i++] = p->name; + } + qsort(dirv, dirc, sizeof(char *), dircmp); + for (p = pkg->head, i = 0; p; p = p->next) { + if (p->type == PLIST_DIR_RM) + p->name = dirv[i++]; } + free(dirv); } /* @@ -210,7 +206,7 @@ check_list(package_t *pkg, const char *PkgName) } target[SymlinkHeaderLen + cc] = 0x0; tmp = new_plist_entry(); - tmp->name = strdup(target); + tmp->name = xstrdup(target); tmp->type = PLIST_COMMENT; tmp->next = p->next; tmp->prev = p; @@ -231,7 +227,7 @@ check_list(package_t *pkg, const char *PkgName) sizeof(buf)); if (MD5File(name, &buf[ChecksumHeaderLen]) != (char *) NULL) { tmp = new_plist_entry(); - tmp->name = strdup(buf); + tmp->name = xstrdup(buf); tmp->type = PLIST_COMMENT; /* PLIST_MD5 - HF */ tmp->next = p->next; tmp->prev = p; diff --git a/pkgtools/pkg_install/files/create/util.c b/pkgtools/pkg_install/files/create/util.c index 351bb0e8cf0..d17b1fed763 100644 --- a/pkgtools/pkg_install/files/create/util.c +++ b/pkgtools/pkg_install/files/create/util.c @@ -95,13 +95,10 @@ make_memory_file(const char *archive_name, void *data, size_t len, { struct memory_file *file; - if ((file = malloc(sizeof(*file))) == NULL) - err(2, "malloc failed"); - + file = xmalloc(sizeof(*file)); file->name = archive_name; file->owner = owner; file->group = group; - file->data = data; file->len = len; @@ -126,9 +123,7 @@ load_memory_file(const char *disk_name, struct memory_file *file; int fd; - if ((file = malloc(sizeof(*file))) == NULL) - err(2, "malloc failed"); - + file = xmalloc(sizeof(*file)); file->name = archive_name; file->owner = owner; file->group = group; @@ -144,9 +139,9 @@ load_memory_file(const char *disk_name, if ((file->st.st_mode & S_IFMT) != S_IFREG) errx(1, "meta data file %s is not regular file", disk_name); - if (file->st.st_size > SSIZE_MAX || - (file->data = malloc(file->st.st_size)) == NULL) - errx(2, "cannot allocate memory for file %s", disk_name); + if (file->st.st_size > SSIZE_MAX) + errx(2, "meta data file too large: %s", disk_name); + file->data = xmalloc(file->st.st_size); if (read(fd, file->data, file->st.st_size) != file->st.st_size) err(2, "cannot read file into memory %s", disk_name); diff --git a/pkgtools/pkg_install/files/delete/main.c b/pkgtools/pkg_install/files/delete/main.c index 26e44c46bd1..c72dd43a8b0 100644 --- a/pkgtools/pkg_install/files/delete/main.c +++ b/pkgtools/pkg_install/files/delete/main.c @@ -1,4 +1,4 @@ -/* $NetBSD: main.c,v 1.21.8.1 2008/07/30 15:38:37 joerg Exp $ */ +/* $NetBSD: main.c,v 1.21.8.2 2008/08/02 20:33:50 joerg Exp $ */ #if HAVE_CONFIG_H #include "config.h" @@ -11,7 +11,7 @@ #if 0 static char *rcsid = "from FreeBSD Id: main.c,v 1.11 1997/10/08 07:46:48 charnier Exp"; #else -__RCSID("$NetBSD: main.c,v 1.21.8.1 2008/07/30 15:38:37 joerg Exp $"); +__RCSID("$NetBSD: main.c,v 1.21.8.2 2008/08/02 20:33:50 joerg Exp $"); #endif #endif @@ -94,8 +94,7 @@ main(int argc, char **argv) break; case 'K': - if ((Pkgdb = strdup(optarg)) == NULL) - err(EXIT_FAILURE, "strdup failed"); + Pkgdb = xstrdup(optarg); break; case 'N': @@ -149,13 +148,12 @@ main(int argc, char **argv) TAILQ_INIT(&pkgs); if (Pkgdb == NULL) - Pkgdb = strdup(_pkgdb_getPKGDB_DIR()); + Pkgdb = xstrdup(_pkgdb_getPKGDB_DIR()); if (Destdir != NULL) { char *pkgdbdir; - if (asprintf(&pkgdbdir, "%s/%s", Destdir, Pkgdb) == -1) - err(EXIT_FAILURE, "asprintf failed"); + pkgdbdir = xasprintf("%s/%s", Destdir, Pkgdb); _pkgdb_setPKGDB_DIR(pkgdbdir); free(pkgdbdir); } else { diff --git a/pkgtools/pkg_install/files/info/main.c b/pkgtools/pkg_install/files/info/main.c index b6a17261a13..b5d75ce8bf1 100644 --- a/pkgtools/pkg_install/files/info/main.c +++ b/pkgtools/pkg_install/files/info/main.c @@ -1,4 +1,4 @@ -/* $NetBSD: main.c,v 1.22 2007/11/05 09:39:38 joerg Exp $ */ +/* $NetBSD: main.c,v 1.22.6.1 2008/08/02 20:33:50 joerg Exp $ */ #if HAVE_CONFIG_H #include "config.h" @@ -11,7 +11,7 @@ #if 0 static char *rcsid = "from FreeBSD Id: main.c,v 1.14 1997/10/08 07:47:26 charnier Exp"; #else -__RCSID("$NetBSD: main.c,v 1.22 2007/11/05 09:39:38 joerg Exp $"); +__RCSID("$NetBSD: main.c,v 1.22.6.1 2008/08/02 20:33:50 joerg Exp $"); #endif #endif @@ -271,11 +271,9 @@ main(int argc, char **argv) s = pkgdb_retrieve(CheckPkg); - if (s) { - CheckPkg = strdup(s); - } else { + if (s == NULL) errx(EXIT_FAILURE, "No matching pkg for %s.", CheckPkg); - } + CheckPkg = xstrdup(s); pkgdb_close(); } diff --git a/pkgtools/pkg_install/files/info/perform.c b/pkgtools/pkg_install/files/info/perform.c index 8f6f7e1183b..37d02712d7b 100644 --- a/pkgtools/pkg_install/files/info/perform.c +++ b/pkgtools/pkg_install/files/info/perform.c @@ -1,4 +1,4 @@ -/* $NetBSD: perform.c,v 1.46.2.3 2008/07/18 19:10:55 joerg Exp $ */ +/* $NetBSD: perform.c,v 1.46.2.4 2008/08/02 20:33:50 joerg Exp $ */ #if HAVE_CONFIG_H #include "config.h" @@ -17,7 +17,7 @@ #if 0 static const char *rcsid = "from FreeBSD Id: perform.c,v 1.23 1997/10/13 15:03:53 jkh Exp"; #else -__RCSID("$NetBSD: perform.c,v 1.46.2.3 2008/07/18 19:10:55 joerg Exp $"); +__RCSID("$NetBSD: perform.c,v 1.46.2.4 2008/08/02 20:33:50 joerg Exp $"); #endif #endif @@ -186,10 +186,7 @@ read_meta_data_from_archive(struct archive *archive, found_required = 0; - if ((meta = malloc(sizeof(*meta))) == NULL) - err(2, "cannot allocate meta data header"); - - memset(meta, 0, sizeof(*meta)); + meta = xcalloc(1, sizeof(*meta)); last_descr = 0; if (entry != NULL) @@ -227,8 +224,7 @@ has_entry: size = archive_entry_size(entry); if (size > SSIZE_MAX - 1) errx(2, "package meta data too large to process"); - if ((*target = malloc(size + 1)) == NULL) - err(2, "cannot allocate meta data"); + *target = xmalloc(size + 1); if (archive_read_data(archive, *target, size) != size) errx(2, "cannot read package meta data"); (*target)[size] = '\0'; @@ -259,10 +255,7 @@ read_meta_data_from_pkgdb(const char *pkg) int fd; struct stat st; - if ((meta = malloc(sizeof(*meta))) == NULL) - err(2, "cannot allocate meta data header"); - - memset(meta, 0, sizeof(*meta)); + meta = xcalloc(1, sizeof(*meta)); for (descr = pkg_meta_descriptors; descr->entry_filename; ++descr) { if ((descr->entry_mask & desired_meta_data) == 0) @@ -285,8 +278,7 @@ read_meta_data_from_pkgdb(const char *pkg) errx(1, "meta data is not regular file"); if (st.st_size > SSIZE_MAX - 1) err(2, "meta data file too large to process"); - if ((*target = malloc(st.st_size + 1)) == NULL) - err(2, "cannot allocate meta data"); + *target = xmalloc(st.st_size + 1); if (read(fd, *target, st.st_size) != st.st_size) err(2, "cannot read meta data"); (*target)[st.st_size] = '\0'; @@ -512,8 +504,7 @@ CheckForPkg(const char *pkgname) if (arg.got_match == 0 && !ispkgpattern(pkgname)) { char *pattern; - if (asprintf(&pattern, "%s-[0-9]*", pkgname) == -1) - errx(EXIT_FAILURE, "asprintf failed"); + pattern = xasprintf("%s-[0-9]*", pkgname); arg.pattern = pattern; arg.got_match = 0; @@ -548,9 +539,7 @@ CheckForBestPkg(const char *pkgname) if (ispkgpattern(pkgname)) return 1; - if (asprintf(&pattern, "%s-[0-9]*", pkgname) == -1) - errx(EXIT_FAILURE, "asprintf failed"); - + pattern = xasprintf("%s-[0-9]*", pkgname); best_match = find_best_matching_installed_pkg(pattern); free(pattern); } diff --git a/pkgtools/pkg_install/files/lib/Makefile.in b/pkgtools/pkg_install/files/lib/Makefile.in index 42f071ce1f0..4f8fc5b5455 100644 --- a/pkgtools/pkg_install/files/lib/Makefile.in +++ b/pkgtools/pkg_install/files/lib/Makefile.in @@ -1,4 +1,4 @@ -# $NetBSD: Makefile.in,v 1.21.2.5 2008/05/26 15:29:03 joerg Exp $ +# $NetBSD: Makefile.in,v 1.21.2.6 2008/08/02 20:33:50 joerg Exp $ srcdir= @srcdir@ @@ -29,7 +29,7 @@ LIB= libinstall.a OBJS= automatic.o conflicts.o decompress.o dewey.o fexec.o file.o \ global.o iterate.o lpkg.o opattern.o \ parse-config.o path.o pkgdb.o plist.o \ - str.o var.o version.o vulnerabilities-file.o + str.o var.o version.o vulnerabilities-file.o xwrapper.o CPPFLAGS+= -DSYSCONFDIR=\"$(sysconfdir)\" diff --git a/pkgtools/pkg_install/files/lib/conflicts.c b/pkgtools/pkg_install/files/lib/conflicts.c index 01468ce0ff6..ba9b3a582ea 100644 --- a/pkgtools/pkg_install/files/lib/conflicts.c +++ b/pkgtools/pkg_install/files/lib/conflicts.c @@ -35,17 +35,6 @@ struct package_conflict { char **conflicting_pattern; }; -static void * -nonnull(void *p) -{ - - if (p == NULL) { - err(EXIT_FAILURE, "NullPointerException"); - /* NOTREACHED */ - } - return p; -} - static FILE * fopen_contents(const char *pkgname, const char *mode) { @@ -88,8 +77,8 @@ check_package_conflict(const char *pkgname, void *v) continue; if (pkg_match(p->name, conflict->pkgname) == 1) { - *(conflict->conflicting_pkgname) = nonnull(strdup(pkgname)); - *(conflict->conflicting_pattern) = nonnull(strdup(p->name)); + *(conflict->conflicting_pkgname) = xstrdup(pkgname); + *(conflict->conflicting_pattern) = xstrdup(p->name); rv = 1 /* nonzero, stop iterating */; break; } diff --git a/pkgtools/pkg_install/files/lib/decompress.c b/pkgtools/pkg_install/files/lib/decompress.c index 79448def1d2..da7facbf627 100644 --- a/pkgtools/pkg_install/files/lib/decompress.c +++ b/pkgtools/pkg_install/files/lib/decompress.c @@ -37,7 +37,7 @@ #include <sys/cdefs.h> #endif -__RCSID("$NetBSD: decompress.c,v 1.1 2008/02/19 15:16:24 joerg Exp $"); +__RCSID("$NetBSD: decompress.c,v 1.1.4.1 2008/08/02 20:33:50 joerg Exp $"); #ifdef BOOTSTRAP #include "lib.h" @@ -71,8 +71,7 @@ decompress_bzip2(const char *in, size_t in_len, char **out, size_t *out_len) *out_len = in_len * 10; else *out_len = in_len; - if ((*out = malloc(*out_len + 1)) == NULL) - err(EXIT_FAILURE, "malloc failed"); + *out = xmalloc(*out_len + 1); stream.next_in = (char *)in; stream.avail_in = in_len; @@ -92,9 +91,7 @@ decompress_bzip2(const char *in, size_t in_len, char **out, size_t *out_len) if (BZ2_bzDecompressEnd(&stream) != Z_OK) errx(EXIT_FAILURE, "inflateEnd failed"); output_produced = *out_len - stream.avail_out; - *out = realloc(*out, output_produced + 1); - if (*out == NULL) - err(EXIT_FAILURE, "realloc failed"); + *out = xrealloc(*out, output_produced + 1); *out_len = output_produced; (*out)[*out_len] = '\0'; return; @@ -104,7 +101,7 @@ decompress_bzip2(const char *in, size_t in_len, char **out, size_t *out_len) *out_len *= 2; else errx(EXIT_FAILURE, "input too large"); - *out = realloc(*out, *out_len + 1); + *out = xrealloc(*out, *out_len + 1); stream.next_out = *out + output_produced; stream.avail_out = *out_len - output_produced; break; @@ -124,8 +121,7 @@ decompress_zlib(const char *in, size_t in_len, char **out, size_t *out_len) *out_len = in_len * 10; else *out_len = in_len; - if ((*out = malloc(*out_len + 1)) == NULL) - err(EXIT_FAILURE, "malloc failed"); + *out = xmalloc(*out_len + 1); stream.next_in = (unsigned char *)in; stream.avail_in = in_len; @@ -145,9 +141,7 @@ decompress_zlib(const char *in, size_t in_len, char **out, size_t *out_len) if (inflateEnd(&stream) != Z_OK) errx(EXIT_FAILURE, "inflateEnd failed"); output_produced = *out_len - stream.avail_out; - *out = realloc(*out, output_produced + 1); - if (*out == NULL) - err(EXIT_FAILURE, "realloc failed"); + *out = xrealloc(*out, output_produced + 1); *out_len = output_produced; (*out)[*out_len] = '\0'; return; @@ -159,7 +153,7 @@ decompress_zlib(const char *in, size_t in_len, char **out, size_t *out_len) errx(EXIT_FAILURE, "input too large"); else *out_len = SSIZE_MAX - 1; - *out = realloc(*out, *out_len + 1); + *out = xrealloc(*out, *out_len + 1); stream.next_out = (unsigned char *)*out + output_produced; stream.avail_out = *out_len - output_produced; break; diff --git a/pkgtools/pkg_install/files/lib/fexec.c b/pkgtools/pkg_install/files/lib/fexec.c index c8c5185f9fc..b3b6a9da11f 100644 --- a/pkgtools/pkg_install/files/lib/fexec.c +++ b/pkgtools/pkg_install/files/lib/fexec.c @@ -58,7 +58,7 @@ #include "lib.h" #ifndef lint -__RCSID("$NetBSD: fexec.c,v 1.9.8.1 2008/05/23 15:57:04 joerg Exp $"); +__RCSID("$NetBSD: fexec.c,v 1.9.8.2 2008/08/02 20:33:50 joerg Exp $"); #endif static int vfcexec(const char *, int, const char *, va_list); @@ -106,8 +106,7 @@ vfcexec(const char *path, int skipempty, const char *arg, va_list ap) int retval; argv_size = 16; - if ((argv = malloc(argv_size * sizeof(*argv))) == NULL) - err(EXIT_FAILURE, "vfcexec: malloc failed"); + argv = xcalloc(argv_size, sizeof(*argv)); argv[0] = arg; argc = 1; @@ -115,9 +114,7 @@ vfcexec(const char *path, int skipempty, const char *arg, va_list ap) do { if (argc == argv_size) { argv_size *= 2; - argv = realloc(argv, argv_size * sizeof(*argv)); - if (argv == NULL) - err(EXIT_FAILURE, "vfcexec: realloc failed"); + argv = xrealloc(argv, argv_size * sizeof(*argv)); } arg = va_arg(ap, const char *); if (skipempty && arg && strlen(arg) == 0) diff --git a/pkgtools/pkg_install/files/lib/iterate.c b/pkgtools/pkg_install/files/lib/iterate.c index 36ddc996789..b7aa342e314 100644 --- a/pkgtools/pkg_install/files/lib/iterate.c +++ b/pkgtools/pkg_install/files/lib/iterate.c @@ -280,8 +280,7 @@ match_best_installed(const char *pkg, void *cookie) case 1: /* Current package is better, remember it. */ free(arg->best_current_match); - if ((arg->best_current_match = strdup(pkg)) == NULL) - return -1; + arg->best_current_match = xstrdup(pkg); break; } return 0; @@ -367,8 +366,7 @@ match_best_file(const char *filename, void *cookie) warnx("filename %s does not contain a recognized suffix", filename); return -1; } - if ((filtered_filename = malloc(len - 4 + 1)) == NULL) - err(EXIT_FAILURE, "malloc failed"); + filtered_filename = xmalloc(len - 4 + 1); memcpy(filtered_filename, filename, len - 4); filtered_filename[len - 4] = '\0'; active_filename = filtered_filename; @@ -390,12 +388,11 @@ match_best_file(const char *filename, void *cookie) /* Current package is better, remember it. */ free(arg->best_current_match); free(arg->best_current_match_filtered); - if ((arg->best_current_match = strdup(filename)) == NULL) - err(EXIT_FAILURE, "strdup failed"); + arg->best_current_match = xstrdup(filename); if (filtered_filename != NULL) arg->best_current_match_filtered = filtered_filename; - else if ((arg->best_current_match_filtered = strdup(active_filename)) == NULL) - err(EXIT_FAILURE, "strdup failed"); + else + arg->best_current_match_filtered = xstrdup(active_filename); return 0; default: errx(EXIT_FAILURE, "Invalid error from pkg_order"); @@ -450,8 +447,7 @@ match_file_and_call(const char *filename, void *cookie) warnx("filename %s does not contain a recognized suffix", filename); return -1; } - if ((filtered_filename = malloc(len - 4 + 1)) == NULL) - err(EXIT_FAILURE, "malloc failed"); + filtered_filename = xmalloc(len - 4 + 1); memcpy(filtered_filename, filename, len - 4); filtered_filename[len - 4] = '\0'; active_filename = filtered_filename; diff --git a/pkgtools/pkg_install/files/lib/lib.h b/pkgtools/pkg_install/files/lib/lib.h index ab8616d7dfd..915eeefb68e 100644 --- a/pkgtools/pkg_install/files/lib/lib.h +++ b/pkgtools/pkg_install/files/lib/lib.h @@ -1,4 +1,4 @@ -/* $NetBSD: lib.h,v 1.42.2.9 2008/07/30 15:38:37 joerg Exp $ */ +/* $NetBSD: lib.h,v 1.42.2.10 2008/08/02 20:33:50 joerg Exp $ */ /* from FreeBSD Id: lib.h,v 1.25 1997/10/08 07:48:03 charnier Exp */ @@ -226,7 +226,7 @@ typedef struct _lfile_t { TAILQ_HEAD(_lfile_head_t, _lfile_t); typedef struct _lfile_head_t lfile_head_t; #define LFILE_ADD(lfhead,lfp,str) do { \ - lfp = malloc(sizeof(lfile_t)); \ + lfp = xmalloc(sizeof(lfile_t)); \ lfp->lf_name = str; \ TAILQ_INSERT_TAIL(lfhead,lfp,lf_link); \ } while(0) @@ -393,6 +393,12 @@ int easy_pkcs7_sign(const char *, size_t, char **, size_t *, const char *, const char *); #endif +char *xstrdup(const char *); +void *xrealloc(void *, size_t); +void *xcalloc(size_t, size_t); +void *xmalloc(size_t); +char *xasprintf(const char *, ...); + /* Externs */ extern Boolean Verbose; extern Boolean Fake; diff --git a/pkgtools/pkg_install/files/lib/lpkg.c b/pkgtools/pkg_install/files/lib/lpkg.c index 58325d03568..09c079c62bb 100644 --- a/pkgtools/pkg_install/files/lib/lpkg.c +++ b/pkgtools/pkg_install/files/lib/lpkg.c @@ -1,4 +1,4 @@ -/* $NetBSD: lpkg.c,v 1.5 2003/09/23 07:13:53 grant Exp $ */ +/* $NetBSD: lpkg.c,v 1.5.38.1 2008/08/02 20:33:50 joerg Exp $ */ /* * Copyright (c) 1999 Christian E. Hopps @@ -46,10 +46,8 @@ alloc_lpkg(const char *pkgname) { lpkg_t *lpp; - if ((lpp = malloc(sizeof(*lpp))) == 0) - err(EXIT_FAILURE, "cannot allocate recursion data"); - if ((lpp->lp_name = strdup(pkgname)) == 0) - err(EXIT_FAILURE, "cannot allocate recursion data"); + lpp = xmalloc(sizeof(*lpp)); + lpp->lp_name = xstrdup(pkgname); return (lpp); } diff --git a/pkgtools/pkg_install/files/lib/opattern.c b/pkgtools/pkg_install/files/lib/opattern.c index 0dd55b28fe3..cf41237ad16 100644 --- a/pkgtools/pkg_install/files/lib/opattern.c +++ b/pkgtools/pkg_install/files/lib/opattern.c @@ -1,4 +1,4 @@ -/* $NetBSD: opattern.c,v 1.4.6.1 2008/05/12 12:12:07 joerg Exp $ */ +/* $NetBSD: opattern.c,v 1.4.6.2 2008/08/02 20:33:50 joerg Exp $ */ #if HAVE_CONFIG_H #include "config.h" @@ -11,7 +11,7 @@ #if 0 static const char *rcsid = "Id: str.c,v 1.5 1997/10/08 07:48:21 charnier Exp"; #else -__RCSID("$NetBSD: opattern.c,v 1.4.6.1 2008/05/12 12:12:07 joerg Exp $"); +__RCSID("$NetBSD: opattern.c,v 1.4.6.2 2008/08/02 20:33:50 joerg Exp $"); #endif #endif @@ -178,8 +178,7 @@ pkg_match(const char *pattern, const char *pkg) char *pattern_ver; int retval; - if (asprintf(&pattern_ver, "%s-[0-9]*", pattern) == -1) - errx(EXIT_FAILURE, "Out of memory"); + pattern_ver = xasprintf("%s-[0-9]*", pattern); retval = glob_match(pattern_ver, pkg); free(pattern_ver); return retval; diff --git a/pkgtools/pkg_install/files/lib/parse-config.c b/pkgtools/pkg_install/files/lib/parse-config.c index 72cc5136d2c..6b4aef72bea 100644 --- a/pkgtools/pkg_install/files/lib/parse-config.c +++ b/pkgtools/pkg_install/files/lib/parse-config.c @@ -1,4 +1,4 @@ -/* $NetBSD: parse-config.c,v 1.1.2.3 2008/05/19 10:42:41 joerg Exp $ */ +/* $NetBSD: parse-config.c,v 1.1.2.4 2008/08/02 20:33:50 joerg Exp $ */ #if HAVE_CONFIG_H #include "config.h" @@ -8,7 +8,7 @@ #include <sys/cdefs.h> #endif #ifndef lint -__RCSID("$NetBSD: parse-config.c,v 1.1.2.3 2008/05/19 10:42:41 joerg Exp $"); +__RCSID("$NetBSD: parse-config.c,v 1.1.2.4 2008/08/02 20:33:50 joerg Exp $"); #endif /*- @@ -82,7 +82,6 @@ void pkg_install_config(void) { char *value; - int ret; struct config_variable *var; for (var = config_variables; var->name != NULL; ++var) { @@ -93,16 +92,11 @@ pkg_install_config(void) if (pkg_vulnerabilities_dir == NULL) pkg_vulnerabilities_dir = _pkgdb_getPKGDB_DIR(); - ret = asprintf(&value, "%s/pkg-vulnerabilities", pkg_vulnerabilities_dir); - pkg_vulnerabilities_file = value; - if (ret == -1) - err(EXIT_FAILURE, "asprintf failed"); + pkg_vulnerabilities_file = xasprintf("%s/pkg-vulnerabilities", + pkg_vulnerabilities_dir); if (pkg_vulnerabilities_url == NULL) { - ret = asprintf(&value, "%s/pkg-vulnerabilities.gz", + pkg_vulnerabilities_url = xasprintf("%s/pkg-vulnerabilities.gz", tnf_vulnerability_base); - pkg_vulnerabilities_url = value; - if (ret == -1) - err(EXIT_FAILURE, "asprintf failed"); } if (verified_installation == NULL) verified_installation = "never"; diff --git a/pkgtools/pkg_install/files/lib/path.c b/pkgtools/pkg_install/files/lib/path.c index 5ef484ebffd..fac576ca850 100644 --- a/pkgtools/pkg_install/files/lib/path.c +++ b/pkgtools/pkg_install/files/lib/path.c @@ -1,4 +1,4 @@ -/* $NetBSD: path.c,v 1.6 2004/12/29 12:16:56 agc Exp $ */ +/* $NetBSD: path.c,v 1.6.28.1 2008/08/02 20:33:50 joerg Exp $ */ /*- * Copyright (c)2002 YAMAMOTO Takashi, @@ -34,7 +34,7 @@ #include <sys/cdefs.h> #endif #ifndef lint -__RCSID("$NetBSD: path.c,v 1.6 2004/12/29 12:16:56 agc Exp $"); +__RCSID("$NetBSD: path.c,v 1.6.28.1 2008/08/02 20:33:50 joerg Exp $"); #endif #if HAVE_ERR_H @@ -113,29 +113,18 @@ path_new_entry(const char *cp, size_t len) { struct path *new; - new = malloc(sizeof(*new)); - if (new == NULL) - err(EXIT_FAILURE, "path_create"); + new = xmalloc(sizeof(*new)); if (!IS_FULLPATH(cp) && !IS_URL(cp)) { /* this is a relative path */ - size_t total; char cwd[MaxPathSize]; - size_t cwdlen; if (getcwd(cwd, sizeof(cwd)) == NULL) err(EXIT_FAILURE, "getcwd"); - cwdlen = strlen(cwd); - total = cwdlen + 1 + len + 1; - new->pl_path = malloc(total); - if (new->pl_path == NULL) - err(EXIT_FAILURE, "path_create"); - snprintf(new->pl_path, total, "%s/%*.*s", cwd, (int)len, (int)len, cp); + new->pl_path = xasprintf("%s/%*.*s", cwd, (int)len, (int)len, cp); } else { - new->pl_path = malloc(len + 1); - if (new->pl_path == NULL) - err(EXIT_FAILURE, "path_create"); + new->pl_path = xmalloc(len + 1); memcpy(new->pl_path, cp, len); new->pl_path[len] = '\0'; } @@ -183,9 +172,7 @@ path_setenv(const char *envname) TAILQ_FOREACH(p, &PkgPath, pl_entry) len += strlen(p->pl_path) + 1; - env = malloc(len); - if (env == NULL) - err(EXIT_FAILURE, "path_setenv"); + env = xmalloc(len); env0 = env; envend = env + len; diff --git a/pkgtools/pkg_install/files/lib/pkcs7.c b/pkgtools/pkg_install/files/lib/pkcs7.c index cfeb1570180..2449eb1af4b 100644 --- a/pkgtools/pkg_install/files/lib/pkcs7.c +++ b/pkgtools/pkg_install/files/lib/pkcs7.c @@ -1,4 +1,4 @@ -/* $NetBSD: pkcs7.c,v 1.1.2.6 2008/07/23 18:59:18 joerg Exp $ */ +/* $NetBSD: pkcs7.c,v 1.1.2.7 2008/08/02 20:33:50 joerg Exp $ */ #if HAVE_CONFIG_H #include "config.h" #endif @@ -7,7 +7,7 @@ #include <sys/cdefs.h> #endif -__RCSID("$NetBSD: pkcs7.c,v 1.1.2.6 2008/07/23 18:59:18 joerg Exp $"); +__RCSID("$NetBSD: pkcs7.c,v 1.1.2.7 2008/08/02 20:33:50 joerg Exp $"); /*- * Copyright (c) 2004, 2008 The NetBSD Foundation, Inc. @@ -308,7 +308,7 @@ easy_pkcs7_sign(const char *content, size_t len, out = BIO_new(BIO_s_mem()); PEM_write_bio_PKCS7(out, p7); *signature_len = BIO_get_mem_data(out, &tmp_sig); - *signature = malloc(*signature_len); + *signature = xmalloc(*signature_len); memcpy(*signature, tmp_sig, *signature_len); BIO_free_all(out); diff --git a/pkgtools/pkg_install/files/lib/pkg_io.c b/pkgtools/pkg_install/files/lib/pkg_io.c index 8a6eada3a79..2b0c13be733 100644 --- a/pkgtools/pkg_install/files/lib/pkg_io.c +++ b/pkgtools/pkg_install/files/lib/pkg_io.c @@ -1,4 +1,4 @@ -/* $NetBSD: pkg_io.c,v 1.1.2.2 2008/05/21 20:29:24 joerg Exp $ */ +/* $NetBSD: pkg_io.c,v 1.1.2.3 2008/08/02 20:33:50 joerg Exp $ */ /*- * Copyright (c) 2008 Joerg Sonnenberger <joerg@NetBSD.org>. * All rights reserved. @@ -36,7 +36,7 @@ #include <sys/cdefs.h> #endif -__RCSID("$NetBSD: pkg_io.c,v 1.1.2.2 2008/05/21 20:29:24 joerg Exp $"); +__RCSID("$NetBSD: pkg_io.c,v 1.1.2.3 2008/08/02 20:33:50 joerg Exp $"); #include <archive.h> #include <archive_entry.h> @@ -94,9 +94,7 @@ open_archive_by_url(struct url *url, void **cookie) struct fetch_archive *f; struct archive *a; - f = malloc(sizeof(*f)); - if (f == NULL) - err(2, "cannot allocate memory for remote archive"); + f = xmalloc(sizeof(*f)); f->url = url; a = archive_read_new(); @@ -185,10 +183,7 @@ find_best_package(struct url *url, const char *pattern, struct url **best_url) (pattern[i]) != '-') break; } - if (asprintf(&url_pattern, "%*.*s*", (int)i, (int)i, pattern) == -1) { - free(best_match); - return -1; - } + url_pattern = xasprintf("%*.*s*", (int)i, (int)i, pattern); fetchInitURLList(&ue); if (fetchList(&ue, url, url_pattern, "")) { diff --git a/pkgtools/pkg_install/files/lib/pkg_signature.c b/pkgtools/pkg_install/files/lib/pkg_signature.c index 35c6c21b28a..38b46f36c1d 100644 --- a/pkgtools/pkg_install/files/lib/pkg_signature.c +++ b/pkgtools/pkg_install/files/lib/pkg_signature.c @@ -1,4 +1,4 @@ -/* $NetBSD: pkg_signature.c,v 1.1.2.4 2008/07/18 19:10:55 joerg Exp $ */ +/* $NetBSD: pkg_signature.c,v 1.1.2.5 2008/08/02 20:33:50 joerg Exp $ */ #if HAVE_CONFIG_H #include "config.h" @@ -7,7 +7,7 @@ #if HAVE_SYS_CDEFS_H #include <sys/cdefs.h> #endif -__RCSID("$NetBSD: pkg_signature.c,v 1.1.2.4 2008/07/18 19:10:55 joerg Exp $"); +__RCSID("$NetBSD: pkg_signature.c,v 1.1.2.5 2008/08/02 20:33:50 joerg Exp $"); /*- * Copyright (c) 2008 Joerg Sonnenberger <joerg@NetBSD.org>. @@ -196,8 +196,7 @@ retry: return 1; } *len = archive_entry_size(*entry); - if ((*content = malloc(*len + 1)) == NULL) - err(2, "cannot allocate memory for %s", fname); + *content = xmalloc(*len + 1); if (archive_read_data(archive, *content, *len) != *len) { warnx("cannot read complete %s from archive", fname); @@ -230,8 +229,7 @@ parse_hash_file(const char *hash_file, char **pkgname, hash_file += strlen(block1); len = strcspn(hash_file, "\n"); - if ((*pkgname = malloc(len + 1)) == NULL) - err(2, "cannot allocate pkgname"); + *pkgname = xmalloc(len + 1); memcpy(*pkgname, hash_file, len); (*pkgname)[len] = '\0'; for (i = 0; i < len; ++i) { @@ -277,19 +275,14 @@ parse_hash_file(const char *hash_file, char **pkgname, state->sign_block_number = (state->pkg_size + state->sign_block_len - 1) / state->sign_block_len; - if ((state->sign_buf = malloc(state->sign_block_len)) == NULL) - err(2, "cannot allocate hash buffer"); - - state->sign_blocks = calloc(state->sign_block_number, sizeof(char *)); - if (state->sign_blocks == NULL) - err(2, "cannot allocate signature block"); + state->sign_buf = xmalloc(state->sign_block_len); + state->sign_blocks = xcalloc(state->sign_block_number, sizeof(char *)); for (i = 0; i < state->sign_block_number; ++i) { len = strspn(hash_file, "01234567889abcdef"); if (len != SHA512_DIGEST_LENGTH * 2 || hash_file[len] != '\n') goto cleanup_hashes; - if ((state->sign_blocks[i] = malloc(len + 1)) == NULL) - err(2, "cannot allocate signature block"); + state->sign_blocks[i] = xmalloc(len + 1); memcpy(state->sign_blocks[i], hash_file, len); state->sign_blocks[i][len] = '\0'; hash_file += len + 1; @@ -327,9 +320,7 @@ pkg_verify_signature(struct archive **archive, struct archive_entry **entry, *pkgname = NULL; *cookie = NULL; - if ((state = malloc(sizeof(*state))) == NULL) - err(2, "cannot allocate signature state"); - + state = xmalloc(sizeof(*state)); state->sign_blocks = NULL; state->sign_buf = NULL; state->archive = NULL; @@ -457,8 +448,7 @@ extract_pkgname(int fd) } len = archive_entry_size(entry); - if ((buf = malloc(len + 1)) == NULL) - err(2, "Cannot allocate memory for +CONTENTS"); + buf = xmalloc(len + 1); if (archive_read_data(a, buf, len) != len) { warnx("Short read when extracing +CONTENTS"); @@ -476,8 +466,7 @@ extract_pkgname(int fd) free(buf); p = find_plist(&plist, PLIST_NAME); if (p != NULL) { - if ((buf = strdup(p->name)) == NULL) - err(2, "strdup failed"); + buf = xstrdup(p->name); } else { warnx("Invalid PLIST: missing @name"); buf = NULL; @@ -526,9 +515,8 @@ pkg_sign(const char *name, const char *output, const char *key_file, const char archive_entry_copy_stat(entry, &sb); pkgname = extract_pkgname(fd); - if (asprintf(&hash_file, hash_template, pkgname, - (long long)archive_entry_size(entry)) == -1) - err(2, "asprintf failed"); + hash_file = xasprintf(hash_template, pkgname, + (long long)archive_entry_size(entry)); free(pkgname); for (i = 0; i < archive_entry_size(entry); i += block_len) { @@ -539,13 +527,11 @@ pkg_sign(const char *name, const char *output, const char *key_file, const char if (read(fd, block, block_len) != block_len) err(2, "short read"); hash_block(block, block_len, hash); - if (asprintf(&tmp, "%s%s\n", hash_file, hash) == -1) - err(2, "asprintf failed"); + tmp = xasprintf("%s%s\n", hash_file, hash); free(hash_file); hash_file = tmp; } - if (asprintf(&tmp, "%s%s", hash_file, hash_trailer) == -1) - err(2, "asprintf failed"); + tmp = xasprintf("%s%s", hash_file, hash_trailer); free(hash_file); hash_file = tmp; diff --git a/pkgtools/pkg_install/files/lib/pkgdb.c b/pkgtools/pkg_install/files/lib/pkgdb.c index dafff849a30..1e6caf11297 100644 --- a/pkgtools/pkg_install/files/lib/pkgdb.c +++ b/pkgtools/pkg_install/files/lib/pkgdb.c @@ -1,4 +1,4 @@ -/* $NetBSD: pkgdb.c,v 1.29.4.1 2008/05/23 15:57:04 joerg Exp $ */ +/* $NetBSD: pkgdb.c,v 1.29.4.2 2008/08/02 20:33:50 joerg Exp $ */ #if HAVE_CONFIG_H #include "config.h" @@ -8,7 +8,7 @@ #include <sys/cdefs.h> #endif #ifndef lint -__RCSID("$NetBSD: pkgdb.c,v 1.29.4.1 2008/05/23 15:57:04 joerg Exp $"); +__RCSID("$NetBSD: pkgdb.c,v 1.29.4.2 2008/08/02 20:33:50 joerg Exp $"); #endif /*- @@ -341,10 +341,5 @@ _pkgdb_setPKGDB_DIR(const char *dir) char * pkgdb_pkg_file(const char *pkg, const char *file) { - char *buf; - - if (asprintf(&buf, "%s/%s/%s", _pkgdb_getPKGDB_DIR(), pkg, file) == -1) - err(EXIT_FAILURE, "asprintf failed"); - - return buf; + return xasprintf("%s/%s/%s", _pkgdb_getPKGDB_DIR(), pkg, file); } diff --git a/pkgtools/pkg_install/files/lib/plist.c b/pkgtools/pkg_install/files/lib/plist.c index 832ea4d6080..da9853c68ab 100644 --- a/pkgtools/pkg_install/files/lib/plist.c +++ b/pkgtools/pkg_install/files/lib/plist.c @@ -1,4 +1,4 @@ -/* $NetBSD: plist.c,v 1.17.4.5 2008/08/01 19:14:42 joerg Exp $ */ +/* $NetBSD: plist.c,v 1.17.4.6 2008/08/02 20:33:50 joerg Exp $ */ #if HAVE_CONFIG_H #include "config.h" @@ -11,7 +11,7 @@ #if 0 static const char *rcsid = "from FreeBSD Id: plist.c,v 1.24 1997/10/08 07:48:15 charnier Exp"; #else -__RCSID("$NetBSD: plist.c,v 1.17.4.5 2008/08/01 19:14:42 joerg Exp $"); +__RCSID("$NetBSD: plist.c,v 1.17.4.6 2008/08/02 20:33:50 joerg Exp $"); #endif #endif @@ -118,7 +118,7 @@ add_plist(package_t *p, pl_ent_t type, const char *arg) plist_t *tmp; tmp = new_plist_entry(); - tmp->name = (arg == (char *) NULL) ? (char *) NULL : strdup(arg); + tmp->name = (arg == NULL) ? NULL : xstrdup(arg); tmp->type = type; if (!p->head) { p->head = p->tail = tmp; @@ -138,7 +138,7 @@ add_plist_top(package_t *p, pl_ent_t type, const char *arg) plist_t *tmp; tmp = new_plist_entry(); - tmp->name = (arg == (char *) NULL) ? (char *) NULL : strdup(arg); + tmp->name = (arg == NULL) ? NULL : xstrdup(arg); tmp->type = type; if (!p->head) { p->head = p->tail = tmp; @@ -239,13 +239,7 @@ delete_plist(package_t *pkg, Boolean all, pl_ent_t type, char *name) plist_t * new_plist_entry(void) { - plist_t *ret; - - if ((ret = (plist_t *) malloc(sizeof(plist_t))) == (plist_t *) NULL) { - err(EXIT_FAILURE, "can't allocate %ld bytes", (long) sizeof(plist_t)); - } - memset(ret, 0, sizeof(plist_t)); - return ret; + return xcalloc(1, sizeof(plist_t)); } /* @@ -290,9 +284,7 @@ plist_cmd(const char *s, char **arg) while (isspace((unsigned char)*sp)) ++sp; - *arg = strdup(sp); - if (*arg == NULL) - err(2, "strdup failed"); + *arg = xstrdup(sp); if (*sp) { sp2 = *arg + strlen(*arg) - 1; /* @@ -333,9 +325,7 @@ parse_plist(package_t *pkg, const char *buf) if (len == 0) continue; - line = malloc(len + 1); - if (line == NULL) - err(2, "malloc failed"); + line = xmalloc(len + 1); memcpy(line, buf, len); line[len] = '\0'; @@ -460,8 +450,7 @@ stringify_plist(package_t *pkg, char **real_buf, size_t *real_len, } /* Pass Two: build actual string. */ - if ((buf = malloc(len + 1)) == NULL) - err(2, "malloc failed"); + buf = xmalloc(len + 1); *real_buf = buf; *real_len = len; ++len; diff --git a/pkgtools/pkg_install/files/lib/var.c b/pkgtools/pkg_install/files/lib/var.c index bc902bd6702..75827357229 100644 --- a/pkgtools/pkg_install/files/lib/var.c +++ b/pkgtools/pkg_install/files/lib/var.c @@ -1,4 +1,4 @@ -/* $NetBSD: var.c,v 1.6 2008/02/02 16:21:46 joerg Exp $ */ +/* $NetBSD: var.c,v 1.6.4.1 2008/08/02 20:33:50 joerg Exp $ */ /*- * Copyright (c) 2005, 2008 The NetBSD Foundation, Inc. @@ -40,7 +40,7 @@ #include <sys/cdefs.h> #endif #ifndef lint -__RCSID("$NetBSD: var.c,v 1.6 2008/02/02 16:21:46 joerg Exp $"); +__RCSID("$NetBSD: var.c,v 1.6.4.1 2008/08/02 20:33:50 joerg Exp $"); #endif #if HAVE_SYS_STAT_H @@ -128,11 +128,11 @@ var_get(const char *fname, const char *variable) thislen = line+len - p; if (value) { - value = realloc(value, valuelen+thislen+2); + value = xrealloc(value, valuelen+thislen+2); value[valuelen++] = '\n'; } else { - value = malloc(thislen+1); + value = xmalloc(thislen+1); } sprintf(value+valuelen, "%.*s", (int)thislen, p); valuelen += thislen; @@ -171,11 +171,11 @@ var_get_memory(const char *buf, const char *variable) thislen = buf + len - data; if (value) { - value = realloc(value, valuelen+thislen+2); + value = xrealloc(value, valuelen+thislen+2); value[valuelen++] = '\n'; } else { - value = malloc(thislen+1); + value = xmalloc(thislen+1); } sprintf(value + valuelen, "%.*s", (int)thislen, data); valuelen += thislen; @@ -214,9 +214,8 @@ var_set(const char *fname, const char *variable, const char *value) return 0; /* Nothing to do */ } - tmpname = malloc(strlen(fname)+8); - sprintf(tmpname, "%s.XXXXXX", fname); - if ((fd=mkstemp(tmpname)) < 0) { + tmpname = xasprintf("%s.XXXXXX", fname); + if ((fd = mkstemp(tmpname)) < 0) { free(tmpname); if (fp != NULL) fclose(fp); diff --git a/pkgtools/pkg_install/files/lib/vulnerabilities-file.c b/pkgtools/pkg_install/files/lib/vulnerabilities-file.c index 12bcbfe95b0..0c783064f5b 100644 --- a/pkgtools/pkg_install/files/lib/vulnerabilities-file.c +++ b/pkgtools/pkg_install/files/lib/vulnerabilities-file.c @@ -36,7 +36,7 @@ #if HAVE_SYS_CDEFS_H #include <sys/cdefs.h> #endif -__RCSID("$NetBSD: vulnerabilities-file.c,v 1.3.4.5 2008/07/05 17:26:40 joerg Exp $"); +__RCSID("$NetBSD: vulnerabilities-file.c,v 1.3.4.6 2008/08/02 20:33:50 joerg Exp $"); #if HAVE_SYS_STAT_H #include <sys/stat.h> @@ -346,27 +346,21 @@ add_vulnerability(struct pkg_vulnerabilities *pv, size_t *allocated, const char *allocated *= 2; else errx(EXIT_FAILURE, "Too many vulnerabilities"); - pv->vulnerability = realloc(pv->vulnerability, + pv->vulnerability = xrealloc(pv->vulnerability, sizeof(char *) * *allocated); - pv->classification = realloc(pv->classification, + pv->classification = xrealloc(pv->classification, sizeof(char *) * *allocated); - pv->advisory = realloc(pv->advisory, + pv->advisory = xrealloc(pv->advisory, sizeof(char *) * *allocated); - if (pv->vulnerability == NULL || - pv->classification == NULL || pv->advisory == NULL) - errx(EXIT_FAILURE, "realloc failed"); } - if ((pv->vulnerability[pv->entries] = malloc(len_pattern + 1)) == NULL) - errx(EXIT_FAILURE, "malloc failed"); + pv->vulnerability[pv->entries] = xmalloc(len_pattern + 1); memcpy(pv->vulnerability[pv->entries], start_pattern, len_pattern); pv->vulnerability[pv->entries][len_pattern] = '\0'; - if ((pv->classification[pv->entries] = malloc(len_class + 1)) == NULL) - errx(EXIT_FAILURE, "malloc failed"); + pv->classification[pv->entries] = xmalloc(len_class + 1); memcpy(pv->classification[pv->entries], start_class, len_class); pv->classification[pv->entries][len_class] = '\0'; - if ((pv->advisory[pv->entries] = malloc(len_url + 1)) == NULL) - errx(EXIT_FAILURE, "malloc failed"); + pv->advisory[pv->entries] = xmalloc(len_url + 1); memcpy(pv->advisory[pv->entries], start_url, len_url); pv->advisory[pv->entries][len_url] = '\0'; @@ -400,8 +394,7 @@ read_pkg_vulnerabilities(const char *path, int ignore_missing, int check_sum) input_len = (size_t)st.st_size; if (input_len < 4) err(EXIT_FAILURE, "Input too short for a pkg_vulnerability file"); - if ((input = malloc(input_len + 1)) == NULL) - err(EXIT_FAILURE, "malloc failed"); + input = xmalloc(input_len + 1); if ((bytes_read = read(fd, input, input_len)) == -1) err(1, "Failed to read input"); if (bytes_read != st.st_size) @@ -429,9 +422,7 @@ parse_pkg_vulnerabilities(const char *input, size_t input_len, int check_sum) size_t allocated_vulns; int in_pgp_msg; - pv = malloc(sizeof(*pv)); - if (pv == NULL) - err(EXIT_FAILURE, "malloc failed"); + pv = xmalloc(sizeof(*pv)); allocated_vulns = pv->entries = 0; pv->vulnerability = NULL; @@ -531,15 +522,12 @@ parse_pkg_vulnerabilities(const char *input, size_t input_len, int check_sum) } if (pv->entries != allocated_vulns) { - pv->vulnerability = realloc(pv->vulnerability, + pv->vulnerability = xrealloc(pv->vulnerability, sizeof(char *) * pv->entries); - pv->classification = realloc(pv->classification, + pv->classification = xrealloc(pv->classification, sizeof(char *) * pv->entries); - pv->advisory = realloc(pv->advisory, + pv->advisory = xrealloc(pv->advisory, sizeof(char *) * pv->entries); - if (pv->vulnerability == NULL || - pv->classification == NULL || pv->advisory == NULL) - errx(EXIT_FAILURE, "realloc failed"); } return pv; diff --git a/pkgtools/pkg_install/files/lib/xwrapper.c b/pkgtools/pkg_install/files/lib/xwrapper.c new file mode 100644 index 00000000000..d0dd2b93100 --- /dev/null +++ b/pkgtools/pkg_install/files/lib/xwrapper.c @@ -0,0 +1,102 @@ +/* $NetBSD: xwrapper.c,v 1.1.2.1 2008/08/02 20:33:51 joerg Exp $ */ + +/*- + * Copyright (c) 2008 Joerg Sonnenberger <joerg@NetBSD.org>. + * 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 COPYRIGHT HOLDERS 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 + * COPYRIGHT HOLDERS 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. + */ + +#if HAVE_CONFIG_H +#include "config.h" +#endif +#include <nbcompat.h> +#if HAVE_SYS_CDEFS_H +#include <sys/cdefs.h> +#endif +__RCSID("$NetBSD: xwrapper.c,v 1.1.2.1 2008/08/02 20:33:51 joerg Exp $"); + +#if HAVE_ERR_H +#include <err.h> +#endif +#include <stdarg.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include "lib.h" + +char * +xasprintf(const char *fmt, ...) +{ + va_list ap; + char *buf; + + va_start(ap, fmt); + if (vasprintf(&buf, fmt, ap) == -1) + err(1, "asprintf failed"); + va_end(ap); + return buf; +} + +void * +xmalloc(size_t len) +{ + void *ptr; + + if ((ptr = malloc(len)) == NULL) + err(1, "malloc failed"); + return ptr; +} + +void * +xcalloc(size_t len, size_t n) +{ + void *ptr; + + if ((ptr = calloc(len, n)) == NULL) + err(1, "calloc failed"); + return ptr; +} + +void * +xrealloc(void *buf, size_t len) +{ + void *ptr; + + if ((ptr = realloc(buf, len)) == NULL) + err(1, "realloc failed"); + return ptr; +} + +char * +xstrdup(const char *str) +{ + char *buf; + + if ((buf = strdup(str)) == NULL) + err(1, "strdup failed"); + return buf; +} |