Do not assume that asprintf() clears the pointer on failure, which
authormillert <millert@openbsd.org>
Wed, 3 Jun 2015 02:24:36 +0000 (02:24 +0000)
committermillert <millert@openbsd.org>
Wed, 3 Jun 2015 02:24:36 +0000 (02:24 +0000)
is non-portable.  Also add missing asprintf() return value checks.
OK deraadt@ guenther@ doug@

21 files changed:
lib/libc/gen/getgrouplist.c
lib/libc/gen/getpwent.c
lib/libutil/pidfile.c
libexec/ld.so/ldconfig/prebind.c
libexec/ld.so/ldconfig/prebind_delete.c
libexec/spamd-setup/spamd-setup.c
sbin/disklabel/disklabel.c
sbin/iked/iked.c
sbin/iked/parse.y
sbin/ipsecctl/parse.y
sbin/isakmpd/field.c
sbin/pfctl/pfctl_parser.c
usr.sbin/httpd/httpd.c
usr.sbin/ldapd/btree.c
usr.sbin/ldapd/index.c
usr.sbin/ldapd/ldape.c
usr.sbin/ldapd/search.c
usr.sbin/relayd/relayd.c
usr.sbin/rtadvd/dump.c
usr.sbin/smtpd/smtpd.c
usr.sbin/snmpd/snmpd.c

index 6512311..79f6da7 100644 (file)
@@ -1,4 +1,4 @@
-/*     $OpenBSD: getgrouplist.c,v 1.24 2014/09/15 06:15:48 guenther Exp $ */
+/*     $OpenBSD: getgrouplist.c,v 1.25 2015/06/03 02:24:36 millert Exp $ */
 /*
  * Copyright (c) 2008 Ingo Schwarze <schwarze@usta.de>
  * Copyright (c) 1991, 1993
@@ -204,8 +204,8 @@ getgrouplist(const char *uname, gid_t agroup, gid_t *groups, int *grpcnt)
                if (getpwnam_r(uname, &pwstore, buf, sizeof buf, NULL) ||
                    (!__ypdomain && yp_get_default_domain(&__ypdomain)))
                        goto out;
-               asprintf(&key, "unix.%u@%s", pwstore.pw_uid, __ypdomain);
-               if (key == NULL)
+               i = asprintf(&key, "unix.%u@%s", pwstore.pw_uid, __ypdomain);
+               if (i == -1)
                        goto out;
 
                /* First scan the static netid file. */
index 45f64fd..2d58e3f 100644 (file)
@@ -1,4 +1,4 @@
-/*     $OpenBSD: getpwent.c,v 1.53 2015/01/16 16:48:51 deraadt Exp $ */
+/*     $OpenBSD: getpwent.c,v 1.54 2015/06/03 02:24:36 millert Exp $ */
 /*
  * Copyright (c) 2008 Theo de Raadt
  * Copyright (c) 1988, 1993
@@ -542,11 +542,17 @@ __yppwlookup(int lookup, char *name, uid_t uid, struct passwd *pw,
                        __ypproto_set(pw, yppbuf, *flagsp, &yp_pw_flags);
                        if (!map) {
                                if (lookup == LOOKUP_BYNAME) {
+                                       if ((name = strdup(name)) == NULL) {
+                                               pw = NULL;
+                                               goto done;
+                                       }
                                        map = PASSWD_BYNAME;
-                                       name = strdup(name);
                                } else {
+                                       if (asprintf(&name, "%u", uid) == -1) {
+                                               pw = NULL;
+                                               goto done;
+                                       }
                                        map = PASSWD_BYUID;
-                                       asprintf(&name, "%u", uid);
                                }
                        }
 
index 9bb68eb..4c365d0 100644 (file)
@@ -1,4 +1,4 @@
-/*     $OpenBSD: pidfile.c,v 1.10 2014/06/30 00:26:22 deraadt Exp $    */
+/*     $OpenBSD: pidfile.c,v 1.11 2015/06/03 02:24:36 millert Exp $    */
 /*     $NetBSD: pidfile.c,v 1.4 2001/02/19 22:43:42 cgd Exp $  */
 
 /*-
@@ -61,8 +61,7 @@ pidfile(const char *basename)
        }
 
        /* _PATH_VARRUN includes trailing / */
-       (void) asprintf(&pidfile_path, "%s%s.pid", _PATH_VARRUN, basename);
-       if (pidfile_path == NULL)
+       if (asprintf(&pidfile_path, "%s%s.pid", _PATH_VARRUN, basename) == -1)
                return (-1);
 
        if ((f = fopen(pidfile_path, "w")) == NULL) {
index cb7e453..785929b 100644 (file)
@@ -1,4 +1,4 @@
-/* $OpenBSD: prebind.c,v 1.28 2015/01/16 16:18:07 deraadt Exp $ */
+/* $OpenBSD: prebind.c,v 1.29 2015/06/03 02:24:36 millert Exp $ */
 /*
  * Copyright (c) 2006 Dale Rahn <drahn@dalerahn.com>
  *
@@ -268,14 +268,20 @@ load_dir(char *name)
                         * NFS will return unknown, since load_file
                         * does stat the file, this just
                         */
-                       asprintf(&buf, "%s/%s", name, dp->d_name);
+                       if (asprintf(&buf, "%s/%s", name, dp->d_name) == -1) {
+                               warn("asprintf");
+                               goto done;
+                       }
                        lstat(buf, &sb);
                        if (sb.st_mode == S_IFREG)
                                load_exe(buf);
                        free(buf);
                        break;
                case DT_REG:
-                       asprintf(&buf, "%s/%s", name, dp->d_name);
+                       if (asprintf(&buf, "%s/%s", name, dp->d_name) == -1) {
+                               warn("asprintf");
+                               goto done;
+                       }
                        load_exe(buf);
                        free(buf);
                        break;
@@ -284,6 +290,7 @@ load_dir(char *name)
                        ;
                }
        }
+done:
        closedir(dirp);
 }
 
index bc5eb77..bbe5a9f 100644 (file)
@@ -1,4 +1,4 @@
-/* $OpenBSD: prebind_delete.c,v 1.12 2013/05/04 09:23:33 jsg Exp $ */
+/* $OpenBSD: prebind_delete.c,v 1.13 2015/06/03 02:24:36 millert Exp $ */
 
 /*
  * Copyright (c) 2006 Dale Rahn <drahn@dalerahn.com>
@@ -101,14 +101,22 @@ strip_dir(char *dir)
                         * NFS will return unknown, since load_file
                         * does stat the file, this just
                         */
-                       asprintf(&buf, "%s/%s", dir, dp->d_name);
+                       if (asprintf(&buf, "%s/%s", dir, dp->d_name) == -1) {
+                               if (verbose)
+                                       warn("asprintf");
+                               goto done;
+                       }
                        lstat(buf, &sb);
                        if (sb.st_mode == S_IFREG)
                                ret = strip_prebind(buf);
                        free(buf);
                        break;
                case DT_REG:
-                       asprintf(&buf, "%s/%s", dir, dp->d_name);
+                       if (asprintf(&buf, "%s/%s", dir, dp->d_name) == -1) {
+                               if (verbose)
+                                       warn("asprintf");
+                               goto done;
+                       }
                        ret = strip_prebind(buf);
                        free(buf);
                        break;
@@ -118,6 +126,7 @@ strip_dir(char *dir)
                        ;
                }
        }
+done:
        closedir(dirp);
        return ret;
 }
index f81430a..d12c84a 100644 (file)
@@ -1,4 +1,4 @@
-/*     $OpenBSD: spamd-setup.c,v 1.45 2015/01/20 16:54:06 millert Exp $ */
+/*     $OpenBSD: spamd-setup.c,v 1.46 2015/06/03 02:24:36 millert Exp $ */
 
 /*
  * Copyright (c) 2003 Bob Beck.  All rights reserved.
@@ -298,8 +298,7 @@ open_file(char *method, char *file)
                return (open(file, O_RDONLY));
        if ((strcmp(method, "http") == 0) ||
            strcmp(method, "ftp") == 0) {
-               asprintf(&url, "%s://%s", method, file);
-               if (url == NULL)
+               if (asprintf(&url, "%s://%s", method, file) == -1)
                        return (-1);
                i = fileget(url);
                free(url);
@@ -308,7 +307,7 @@ open_file(char *method, char *file)
                len = strlen(file);
                argv = calloc(len, sizeof(char *));
                if (argv == NULL)
-                       err(1, NULL);
+                       return (-1);
                for (ap = argv; ap < &argv[len - 1] &&
                    (*ap = strsep(&file, " \t")) != NULL;) {
                        if (**ap != '\0')
index 7b7e5a0..bece106 100644 (file)
@@ -1,4 +1,4 @@
-/*     $OpenBSD: disklabel.c,v 1.201 2015/04/29 16:56:31 henning Exp $ */
+/*     $OpenBSD: disklabel.c,v 1.202 2015/06/03 02:24:36 millert Exp $ */
 
 /*
  * Copyright (c) 1987, 1993
@@ -471,11 +471,15 @@ readlabel(int f)
                        err(4, "ioctl DIOCGDINFO");
        }
 
-       asprintf(&partname, "/dev/%s%c", dkname, 'a');
-       asprintf(&partduid,
+       i = asprintf(&partname, "/dev/%s%c", dkname, 'a');
+       if (i == -1)
+               err(4, NULL);
+       i = asprintf(&partduid,
            "%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx.a",
             lab.d_uid[0], lab.d_uid[1], lab.d_uid[2], lab.d_uid[3],
             lab.d_uid[4], lab.d_uid[5], lab.d_uid[6], lab.d_uid[7]);
+       if (i == -1)
+               err(4, NULL);
        setfsent();
        for (i = 0; i < MAXPARTITIONS; i++) {
                partname[strlen(dkname) + 5] = 'a' + i;
index 182b756..9a9c7cd 100644 (file)
@@ -1,4 +1,4 @@
-/*     $OpenBSD: iked.c,v 1.23 2015/01/16 06:39:58 deraadt Exp $       */
+/*     $OpenBSD: iked.c,v 1.24 2015/06/03 02:24:36 millert Exp $       */
 
 /*
  * Copyright (c) 2010-2013 Reyk Floeter <reyk@openbsd.org>
@@ -289,6 +289,8 @@ parent_sig_handler(int sig, short event, void *arg)
                /* FALLTHROUGH */
        case SIGCHLD:
                do {
+                       int len;
+
                        pid = waitpid(-1, &status, WNOHANG);
                        if (pid <= 0)
                                continue;
@@ -296,17 +298,21 @@ parent_sig_handler(int sig, short event, void *arg)
                        fail = 0;
                        if (WIFSIGNALED(status)) {
                                fail = 1;
-                               asprintf(&cause, "terminated; signal %d",
+                               len = asprintf(&cause, "terminated; signal %d",
                                    WTERMSIG(status));
                        } else if (WIFEXITED(status)) {
                                if (WEXITSTATUS(status) != 0) {
                                        fail = 1;
-                                       asprintf(&cause, "exited abnormally");
+                                       len = asprintf(&cause,
+                                           "exited abnormally");
                                } else
-                                       asprintf(&cause, "exited okay");
+                                       len = asprintf(&cause, "exited okay");
                        } else
                                fatalx("unexpected cause of SIGCHLD");
 
+                       if (len == -1)
+                               fatal("asprintf");
+
                        die = 1;
 
                        for (id = 0; id < PROC_MAX; id++)
index fbf5b29..33ea8c8 100644 (file)
@@ -1,4 +1,4 @@
-/*     $OpenBSD: parse.y,v 1.46 2015/02/08 04:50:32 reyk Exp $ */
+/*     $OpenBSD: parse.y,v 1.47 2015/06/03 02:24:36 millert Exp $      */
 
 /*
  * Copyright (c) 2010-2013 Reyk Floeter <reyk@openbsd.org>
@@ -1770,11 +1770,12 @@ host_v6(const char *s, int prefixlen)
 
        if (prefixlen != 128) {
                ipa->netaddress = 1;
-               asprintf(&ipa->name, "%s/%d", hbuf, prefixlen);
-       } else
-               ipa->name = strdup(hbuf);
-       if (ipa->name == NULL)
-               err(1, "host_v6: strdup");
+               if (asprintf(&ipa->name, "%s/%d", hbuf, prefixlen) == -1)
+                       err(1, "host_v6: asprintf");
+       } else {
+               if ((ipa->name = strdup(hbuf)) == NULL)
+                       err(1, "host_v6: strdup");
+       }
 
        freeaddrinfo(res);
 
index 86d0249..cab02d2 100644 (file)
@@ -1,4 +1,4 @@
-/*     $OpenBSD: parse.y,v 1.161 2014/11/20 05:51:20 jsg Exp $ */
+/*     $OpenBSD: parse.y,v 1.162 2015/06/03 02:24:36 millert Exp $     */
 
 /*
  * Copyright (c) 2002, 2003, 2004 Henning Brauer <henning@openbsd.org>
@@ -1613,11 +1613,12 @@ host_v6(const char *s, int prefixlen)
 
        if (prefixlen != 128) {
                ipa->netaddress = 1;
-               asprintf(&ipa->name, "%s/%d", hbuf, prefixlen);
-       } else
-               ipa->name = strdup(hbuf);
-       if (ipa->name == NULL)
-               err(1, "host_v6: strdup");
+               if (asprintf(&ipa->name, "%s/%d", hbuf, prefixlen) == -1)
+                       err(1, "host_v6: asprintf");
+       } else {
+               if ((ipa->name = strdup(hbuf)) == NULL)
+                       err(1, "host_v6: strdup");
+       }
 
        freeaddrinfo(res);
 
index 67cbffd..f7dccc2 100644 (file)
@@ -1,4 +1,4 @@
-/* $OpenBSD: field.c,v 1.19 2005/11/15 21:49:04 cloder Exp $    */
+/* $OpenBSD: field.c,v 1.20 2015/06/03 02:24:36 millert Exp $   */
 /* $EOM: field.c,v 1.11 2000/02/20 19:58:37 niklas Exp $        */
 
 /*
@@ -95,13 +95,13 @@ extract_val(u_int8_t *buf, size_t len, u_int32_t *val)
 static char *
 field_debug_num(u_int8_t *buf, size_t len, struct constant_map **maps)
 {
-       char           *retval;
+       char           *retval = NULL;
        u_int32_t       val;
 
        if (extract_val(buf, len, &val))
                return 0;
        /* 3 decimal digits are enough to represent each byte.  */
-       asprintf(&retval, "%u", val);
+       (void)asprintf(&retval, "%u", val);
        return retval;
 }
 
index 65f0e03..f9fb8a7 100644 (file)
@@ -1,4 +1,4 @@
-/*     $OpenBSD: pfctl_parser.c,v 1.304 2015/02/14 23:32:41 sthen Exp $ */
+/*     $OpenBSD: pfctl_parser.c,v 1.305 2015/06/03 02:24:36 millert Exp $ */
 
 /*
  * Copyright (c) 2001 Daniel Hartmeier
@@ -543,9 +543,10 @@ print_status(struct pf_status *s, int opts)
        } else
                snprintf(statline, sizeof(statline), "Status: %s", running);
        printf("%-44s", statline);
-       asprintf(&debug, "Debug: %s", loglevel_to_string(s->debug));
-       printf("%15s\n\n", debug);
-       free(debug);
+       if (asprintf(&debug, "Debug: %s", loglevel_to_string(s->debug)) != -1) {
+               printf("%15s\n\n", debug);
+               free(debug);
+       }
 
        if (opts & PF_OPT_VERBOSE) {
                printf("Hostid:   0x%08x\n", ntohl(s->hostid));
index 5019fce..5f6788c 100644 (file)
@@ -1,4 +1,4 @@
-/*     $OpenBSD: httpd.c,v 1.36 2015/05/28 17:08:09 florian Exp $      */
+/*     $OpenBSD: httpd.c,v 1.37 2015/06/03 02:24:36 millert Exp $      */
 
 /*
  * Copyright (c) 2014 Reyk Floeter <reyk@openbsd.org>
@@ -80,6 +80,8 @@ parent_sig_handler(int sig, short event, void *arg)
                /* FALLTHROUGH */
        case SIGCHLD:
                do {
+                       int len;
+
                        pid = waitpid(WAIT_ANY, &status, WNOHANG);
                        if (pid <= 0)
                                continue;
@@ -87,17 +89,21 @@ parent_sig_handler(int sig, short event, void *arg)
                        fail = 0;
                        if (WIFSIGNALED(status)) {
                                fail = 1;
-                               asprintf(&cause, "terminated; signal %d",
+                               len = asprintf(&cause, "terminated; signal %d",
                                    WTERMSIG(status));
                        } else if (WIFEXITED(status)) {
                                if (WEXITSTATUS(status) != 0) {
                                        fail = 1;
-                                       asprintf(&cause, "exited abnormally");
+                                       len = asprintf(&cause,
+                                           "exited abnormally");
                                } else
-                                       asprintf(&cause, "exited okay");
+                                       len = asprintf(&cause, "exited okay");
                        } else
                                fatalx("unexpected cause of SIGCHLD");
 
+                       if (len == -1)
+                               fatal("asprintf");
+
                        die = 1;
 
                        for (id = 0; id < PROC_MAX; id++)
index cbba2ba..eaf0d2f 100644 (file)
@@ -1,4 +1,4 @@
-/*     $OpenBSD: btree.c,v 1.32 2015/01/16 16:04:38 deraadt Exp $ */
+/*     $OpenBSD: btree.c,v 1.33 2015/06/03 02:24:36 millert Exp $ */
 
 /*
  * Copyright (c) 2009, 2010 Martin Hedenfalk <martin@bzero.se>
@@ -3071,7 +3071,10 @@ btree_compact(struct btree *bt)
        if ((txn = btree_txn_begin(bt, 0)) == NULL)
                return BT_FAIL;
 
-       asprintf(&compact_path, "%s.compact.XXXXXX", bt->path);
+       if (asprintf(&compact_path, "%s.compact.XXXXXX", bt->path) == -1) {
+               btree_txn_abort(txn);
+               return BT_FAIL;
+       }
        fd = mkstemp(compact_path);
        if (fd == -1) {
                free(compact_path);
index 8d1b0f8..c8dbb7f 100644 (file)
@@ -1,4 +1,4 @@
-/*     $OpenBSD: index.c,v 1.8 2010/11/26 14:44:01 martinh Exp $ */
+/*     $OpenBSD: index.c,v 1.9 2015/06/03 02:24:36 millert Exp $ */
 
 /*
  * Copyright (c) 2009 Martin Hedenfalk <martin@bzero.se>
@@ -107,6 +107,8 @@ index_attribute(struct namespace *ns, char *attr, struct btval *dn,
                bzero(&key, sizeof(key));
                key.size = asprintf(&t, "%s=%s,%.*s", attr, s, dnsz,
                    (char *)dn->data);
+               if (key.size == (size_t)-1)
+                       return -1;
                key.data = t;
                normalize_dn(key.data);
                rc = btree_txn_put(NULL, ns->indx_txn, &key, &val,
@@ -141,7 +143,9 @@ index_rdn_key(struct namespace *ns, struct btval *dn, struct btval *key)
                ++parent_dn;
        }
 
-       asprintf(&t, "@%.*s,%.*s", pdnsz, parent_dn, rdnsz, (char *)dn->data);
+       if (asprintf(&t, "@%.*s,%.*s", pdnsz, parent_dn, rdnsz,
+           (char *)dn->data) == -1)
+               return -1;
 
        normalize_dn(t);
        key->data = t;
index 58a698f..48bc298 100644 (file)
@@ -1,4 +1,4 @@
-/*     $OpenBSD: ldape.c,v 1.19 2015/01/16 16:04:38 deraadt Exp $ */
+/*     $OpenBSD: ldape.c,v 1.20 2015/06/03 02:24:36 millert Exp $ */
 
 /*
  * Copyright (c) 2009, 2010 Martin Hedenfalk <martin@bzero.se>
@@ -143,11 +143,11 @@ ldap_refer(struct request *req, const char *basedn, struct search *search,
        ber_set_header(ref_root, BER_CLASS_CONTEXT, LDAP_REQ_SEARCH);
        SLIST_FOREACH(ref, refs, next) {
                if (search != NULL)
-                       asprintf(&url, "%s/%s??%s", ref->url, basedn,
+                       rc = asprintf(&url, "%s/%s??%s", ref->url, basedn,
                            scope_str);
                else
-                       asprintf(&url, "%s/%s", ref->url, basedn);
-               if (url == NULL) {
+                       rc = asprintf(&url, "%s/%s", ref->url, basedn);
+               if (rc == -1) {
                        log_warn("asprintf");
                        goto fail;
                }
index 3afd625..d2fa8ba 100644 (file)
@@ -1,4 +1,4 @@
-/*     $OpenBSD: search.c,v 1.14 2010/11/10 08:00:54 martinh Exp $ */
+/*     $OpenBSD: search.c,v 1.15 2015/06/03 02:24:36 millert Exp $ */
 
 /*
  * Copyright (c) 2009, 2010 Martin Hedenfalk <martin@bzero.se>
@@ -619,13 +619,18 @@ add_index(struct plan *plan, const char *fmt, ...)
 {
        struct index            *indx;
        va_list                  ap;
+       int                      rc;
 
        if ((indx = calloc(1, sizeof(*indx))) == NULL)
                return -1;
 
        va_start(ap, fmt);
-       vasprintf(&indx->prefix, fmt, ap);
+       rc = vasprintf(&indx->prefix, fmt, ap);
        va_end(ap);
+       if (rc == -1) {
+               free(indx);
+               return -1;
+       }
 
        normalize_dn(indx->prefix);
 
index b39956e..b4a5a7a 100644 (file)
@@ -1,4 +1,4 @@
-/*     $OpenBSD: relayd.c,v 1.141 2015/05/30 09:47:25 claudio Exp $    */
+/*     $OpenBSD: relayd.c,v 1.142 2015/06/03 02:24:36 millert Exp $    */
 
 /*
  * Copyright (c) 2007 - 2014 Reyk Floeter <reyk@openbsd.org>
@@ -87,6 +87,8 @@ parent_sig_handler(int sig, short event, void *arg)
                /* FALLTHROUGH */
        case SIGCHLD:
                do {
+                       int len;
+
                        pid = waitpid(WAIT_ANY, &status, WNOHANG);
                        if (pid <= 0)
                                continue;
@@ -94,17 +96,21 @@ parent_sig_handler(int sig, short event, void *arg)
                        fail = 0;
                        if (WIFSIGNALED(status)) {
                                fail = 1;
-                               asprintf(&cause, "terminated; signal %d",
+                               len = asprintf(&cause, "terminated; signal %d",
                                    WTERMSIG(status));
                        } else if (WIFEXITED(status)) {
                                if (WEXITSTATUS(status) != 0) {
                                        fail = 1;
-                                       asprintf(&cause, "exited abnormally");
+                                       len = asprintf(&cause,
+                                           "exited abnormally");
                                } else
-                                       asprintf(&cause, "exited okay");
+                                       len = asprintf(&cause, "exited okay");
                        } else
                                fatalx("unexpected cause of SIGCHLD");
 
+                       if (len == -1)
+                               fatal("asprintf");
+
                        die = 1;
 
                        for (id = 0; id < PROC_MAX; id++)
index a65befc..aa8856d 100644 (file)
@@ -1,4 +1,4 @@
-/*     $OpenBSD: dump.c,v 1.14 2013/11/12 22:27:13 deraadt Exp $       */
+/*     $OpenBSD: dump.c,v 1.15 2015/06/03 02:24:36 millert Exp $       */
 /*     $KAME: dump.c,v 1.27 2002/05/29 14:23:55 itojun Exp $   */
 
 /*
@@ -90,12 +90,12 @@ ether_str(sdl)
 char *
 lifetime(int lt)
 {
-       char *str;
+       char *str = NULL;
 
        if (lt == ND6_INFINITE_LIFETIME)
-               asprintf(&str, "infinity");
+               (void)asprintf(&str, "infinity");
        else
-               asprintf(&str, "%ld", (long)lt);
+               (void)asprintf(&str, "%ld", (long)lt);
        return str;
 }
 
index efb3a48..820c675 100644 (file)
@@ -1,4 +1,4 @@
-/*     $OpenBSD: smtpd.c,v 1.238 2015/01/20 17:37:54 deraadt Exp $     */
+/*     $OpenBSD: smtpd.c,v 1.239 2015/06/03 02:24:36 millert Exp $     */
 
 /*
  * Copyright (c) 2008 Gilles Chehade <gilles@poolp.org>
@@ -333,6 +333,8 @@ parent_sig_handler(int sig, short event, void *p)
                /* FALLTHROUGH */
        case SIGCHLD:
                do {
+                       int len;
+
                        pid = waitpid(-1, &status, WNOHANG);
                        if (pid <= 0)
                                continue;
@@ -340,17 +342,21 @@ parent_sig_handler(int sig, short event, void *p)
                        fail = 0;
                        if (WIFSIGNALED(status)) {
                                fail = 1;
-                               asprintf(&cause, "terminated; signal %d",
+                               len = asprintf(&cause, "terminated; signal %d",
                                    WTERMSIG(status));
                        } else if (WIFEXITED(status)) {
                                if (WEXITSTATUS(status) != 0) {
                                        fail = 1;
-                                       asprintf(&cause, "exited abnormally");
+                                       len = asprintf(&cause,
+                                           "exited abnormally");
                                } else
-                                       asprintf(&cause, "exited okay");
+                                       len = asprintf(&cause, "exited okay");
                        } else
                                fatalx("smtpd: unexpected cause of SIGCHLD");
 
+                       if (len == -1)
+                               fatal("asprintf");
+
                        if (pid == purge_pid)
                                purge_pid = -1;
 
@@ -369,8 +375,12 @@ parent_sig_handler(int sig, short event, void *p)
                        case CHILD_MDA:
                                if (WIFSIGNALED(status) &&
                                    WTERMSIG(status) == SIGALRM) {
-                                       free(cause);
-                                       asprintf(&cause, "terminated; timeout");
+                                       char *tmp;
+                                       if (asprintf(&tmp,
+                                           "terminated; timeout") != -1) {
+                                               free(cause);
+                                               cause = tmp;
+                                       }
                                }
                                else if (child->cause &&
                                    WIFSIGNALED(status) &&
index b88154b..f2df1ee 100644 (file)
@@ -1,4 +1,4 @@
-/*     $OpenBSD: snmpd.c,v 1.28 2015/05/28 17:08:09 florian Exp $      */
+/*     $OpenBSD: snmpd.c,v 1.29 2015/06/03 02:24:36 millert Exp $      */
 
 /*
  * Copyright (c) 2007, 2008, 2012 Reyk Floeter <reyk@openbsd.org>
@@ -72,6 +72,8 @@ snmpd_sig_handler(int sig, short event, void *arg)
                /* FALLTHROUGH */
        case SIGCHLD:
                do {
+                       int len;
+
                        pid = waitpid(WAIT_ANY, &status, WNOHANG);
                        if (pid <= 0)
                                continue;
@@ -79,16 +81,20 @@ snmpd_sig_handler(int sig, short event, void *arg)
                        fail = 0;
                        if (WIFSIGNALED(status)) {
                                fail = 1;
-                               asprintf(&cause, "terminated; signal %d",
+                               len = asprintf(&cause, "terminated; signal %d",
                                    WTERMSIG(status));
                        } else if (WIFEXITED(status)) {
                                if (WEXITSTATUS(status) != 0) {
                                        fail = 1;
-                                       asprintf(&cause, "exited abnormally");
+                                       len = asprintf(&cause,
+                                           "exited abnormally");
                                } else
-                                       asprintf(&cause, "exited okay");
+                                       len = asprintf(&cause, "exited okay");
                        } else
                                fatalx("unexpected cause of SIGCHLD");
+
+                       if (len == -1)
+                               fatal("asprintf");
                        
                        for (id = 0; id < PROC_MAX; id++) {
                                if (pid == ps->ps_pid[id] &&