From: Michael Hofmann Date: Wed, 6 May 2015 14:28:29 +0000 (+0200) Subject: Introduce sstrndup wrapper. X-Git-Tag: 4.11~110^2~1 X-Git-Url: https://git.sur5r.net/?a=commitdiff_plain;h=0319bda1d4aa0f9339d37a3866d606b3ef188d5c;p=i3%2Fi3 Introduce sstrndup wrapper. --- diff --git a/i3bar/src/workspaces.c b/i3bar/src/workspaces.c index 773f8f54..961a41f5 100644 --- a/i3bar/src/workspaces.c +++ b/i3bar/src/workspaces.c @@ -106,7 +106,7 @@ static int workspaces_string_cb(void *params_, const unsigned char *val, size_t if (!strcmp(params->cur_key, "name")) { const char *ws_name = (const char *)val; - params->workspaces_walk->canonical_name = strndup(ws_name, len); + params->workspaces_walk->canonical_name = sstrndup(ws_name, len); if (config.strip_ws_numbers && params->workspaces_walk->num >= 0) { /* Special case: strip off the workspace number */ diff --git a/include/libi3.h b/include/libi3.h index a64b3981..3e6f8427 100644 --- a/include/libi3.h +++ b/include/libi3.h @@ -127,6 +127,13 @@ void *srealloc(void *ptr, size_t size); */ char *sstrdup(const char *str); +/** + * Safe-wrapper around strndup which exits if strndup returns NULL (meaning that + * there is no more memory available) + * + */ +char *sstrndup(const char *str, size_t size); + /** * Safe-wrapper around asprintf which exits if it returns -1 (meaning that * there is no more memory available) diff --git a/libi3/resolve_tilde.c b/libi3/resolve_tilde.c index 3a56cbea..26cbabe5 100644 --- a/libi3/resolve_tilde.c +++ b/libi3/resolve_tilde.c @@ -23,7 +23,7 @@ char *resolve_tilde(const char *path) { char *head, *tail, *result; tail = strchr(path, '/'); - head = strndup(path, tail ? (size_t)(tail - path) : strlen(path)); + head = sstrndup(path, tail ? (size_t)(tail - path) : strlen(path)); int res = glob(head, GLOB_TILDE, NULL, &globbuf); free(head); diff --git a/libi3/safewrappers.c b/libi3/safewrappers.c index 74460f37..f5973cab 100644 --- a/libi3/safewrappers.c +++ b/libi3/safewrappers.c @@ -48,6 +48,13 @@ char *sstrdup(const char *str) { return result; } +char *sstrndup(const char *str, size_t size) { + char *result = strndup(str, size); + if (result == NULL) + err(EXIT_FAILURE, "strndup()"); + return result; +} + int sasprintf(char **strp, const char *fmt, ...) { va_list args; int result;