diff options
author | Joshua M. Clulow <jmc@joyent.com> | 2015-08-05 12:26:27 -0700 |
---|---|---|
committer | Joshua M. Clulow <jmc@joyent.com> | 2015-08-05 19:26:27 +0000 |
commit | e47481f33f57a102a92ce26118ec2d39ce8bde69 (patch) | |
tree | d9d1d27df56dfac48eed48f5e38a1c2302a560ae | |
parent | 9087526185a70cc5f3304e7900eac6a3fa3c5aff (diff) | |
download | illumos-joyent-e47481f33f57a102a92ce26118ec2d39ce8bde69.tar.gz |
OS-4498 custr_cstr() should never return NULL
Reviewed by: Patrick Mooney <patrick.mooney@joyent.com>
Reviewed by: Cody Mello <cody.mello@joyent.com>
-rw-r--r-- | usr/src/common/brand/lx/tools/gen_errno.c | 14 | ||||
-rw-r--r-- | usr/src/lib/libcmdutils/common/custr.c | 13 | ||||
-rw-r--r-- | usr/src/lib/varpd/files/common/libvarpd_files_json.c | 10 |
3 files changed, 19 insertions, 18 deletions
diff --git a/usr/src/common/brand/lx/tools/gen_errno.c b/usr/src/common/brand/lx/tools/gen_errno.c index 1edd924edf..52b2d76d24 100644 --- a/usr/src/common/brand/lx/tools/gen_errno.c +++ b/usr/src/common/brand/lx/tools/gen_errno.c @@ -185,12 +185,8 @@ process_line(const char *line, nvlist_t *nvl) if (custr_alloc(&nam) != 0 || custr_alloc(&num) != 0) { int en = errno; - if (nam != NULL) { - custr_free(nam); - } - if (num != NULL) { - custr_free(num); - } + custr_free(nam); + custr_free(num); errno = en; return (-1); @@ -282,14 +278,12 @@ read_file_into_list(const char *path, nvlist_t *nvl) FILE *f; custr_t *cu = NULL; - if (custr_alloc(&cu) != 0 || custr_append(cu, "") != 0) { - if (cu != NULL) { - custr_free(cu); - } + if (custr_alloc(&cu) != 0) { return (-1); } if ((f = fopen(path, "r")) == NULL) { + custr_free(cu); return (-1); } diff --git a/usr/src/lib/libcmdutils/common/custr.c b/usr/src/lib/libcmdutils/common/custr.c index 03a9561934..8da7b98ac2 100644 --- a/usr/src/lib/libcmdutils/common/custr.c +++ b/usr/src/lib/libcmdutils/common/custr.c @@ -14,7 +14,7 @@ */ /* - * Copyright 2014, Joyent, Inc. + * Copyright 2015 Joyent, Inc. */ #include <stdlib.h> @@ -22,6 +22,7 @@ #include <string.h> #include <stdio.h> #include <stdarg.h> +#include <sys/debug.h> #include "libcmdutils.h" @@ -57,6 +58,16 @@ custr_len(custr_t *cus) const char * custr_cstr(custr_t *cus) { + if (cus->cus_data == NULL) { + VERIFY(cus->cus_strlen == 0); + VERIFY(cus->cus_datalen == 0); + + /* + * This function should never return NULL. If no buffer has + * been allocated, return a pointer to a zero-length string. + */ + return (""); + } return (cus->cus_data); } diff --git a/usr/src/lib/varpd/files/common/libvarpd_files_json.c b/usr/src/lib/varpd/files/common/libvarpd_files_json.c index 66aaefb29b..240c84bd77 100644 --- a/usr/src/lib/varpd/files/common/libvarpd_files_json.c +++ b/usr/src/lib/varpd/files/common/libvarpd_files_json.c @@ -311,9 +311,7 @@ hdlr_bareword(state_t *s) return; } - if ((str = custr_cstr(s->s_collect)) != NULL) { - abort(); - } + str = custr_cstr(s->s_collect); if (strcmp(str, "true") == 0) { s->s_top->pf_value_type = JSON_TYPE_BOOLEAN; s->s_top->pf_value = (void *)B_TRUE; @@ -828,11 +826,9 @@ nvlist_parse_json(const char *buf, size_t buflen, nvlist_t **nvlp, s.s_flags = flag; /* - * Allocate the collect buffer string and ensure it is backed by at - * least some string memory. + * Allocate the collect buffer string. */ - if (custr_alloc(&s.s_collect) != 0 || - custr_append(s.s_collect, "") != 0) { + if (custr_alloc(&s.s_collect) != 0) { s.s_errno = errno; if (errout != NULL) { (void) snprintf(errout->nje_message, |