From 3d7ee4dacf54e8000fd126d49881dd322f5926b9 Mon Sep 17 00:00:00 2001 From: Sergey Poznyakoff Date: Tue, 9 Sep 2014 01:01:41 +0300 Subject: wordsplit: ws_getvar allocates memory. * src/wordsplit.c (ISVARBEG,ISVARCHR): New macros. (expvar): ws_getvar allocates memory. * src/wordsplit.h (wordsplit): Remove const from the return value: the function should allocate memory. --- src/wordsplit.c | 29 +++++++++++++++++++---------- src/wordsplit.h | 2 +- 2 files changed, 20 insertions(+), 11 deletions(-) (limited to 'src') diff --git a/src/wordsplit.c b/src/wordsplit.c index b694b64..b5a757f 100644 --- a/src/wordsplit.c +++ b/src/wordsplit.c @@ -48,6 +48,9 @@ #define ISALNUM(c) (ISALPHA(c) || ISDIGIT(c)) #define ISPRINT(c) (' ' <= ((unsigned) (c)) && ((unsigned) (c)) <= 127) +#define ISVARBEG(c) (ISALPHA(c) || c == '_') +#define ISVARCHR(c) (ISALNUM(c) || c == '_') + #define ALLOC_INIT 128 #define ALLOC_INCR 128 @@ -703,15 +706,16 @@ expvar (struct wordsplit *wsp, const char *str, size_t len, struct wordsplit_node **ptail, const char **pend, int flg) { size_t i = 0; - const char *value; + const char *defstr = NULL; + char *value; const char *vptr; struct wordsplit_node *newnode; const char *start = str - 1; - if (ISALPHA (str[0]) || str[0] == '_') + if (ISVARBEG (str[0])) { for (i = 1; i < len; i++) - if (!(ISALNUM (str[i]) || str[i] == '_')) + if (!ISVARCHR (str[i])) break; *pend = str + i - 1; } @@ -789,7 +793,11 @@ expvar (struct wordsplit *wsp, const char *str, size_t len, if (wsp->ws_flags & WRDSF_KEEPUNDEF) value = NULL; else - value = ""; + { + value = strdup (""); + if (!value) + return _wsplt_nomem (wsp); + } } /* FIXME: handle defstr */ if (value) @@ -801,12 +809,11 @@ expvar (struct wordsplit *wsp, const char *str, size_t len, wsnode_insert (wsp, newnode, *ptail, 0); *ptail = newnode; newnode->flags = _WSNF_WORD | _WSNF_NOEXPAND | flg; - newnode->v.word = strdup (value); - if (!newnode->v.word) - return _wsplt_nomem (wsp); + newnode->v.word = value; } else if (*value == 0) { + free (value); /* Empty string is a special case */ if (wsnode_new (wsp, &newnode)) return 1; @@ -817,11 +824,13 @@ expvar (struct wordsplit *wsp, const char *str, size_t len, else { struct wordsplit ws; - int i; + int i, rc; ws.ws_delim = wsp->ws_delim; - if (wordsplit (value, &ws, - WRDSF_NOVAR | WRDSF_NOCMD | WRDSF_DELIM | WRDSF_WS)) + rc = wordsplit (value, &ws, + WRDSF_NOVAR | WRDSF_NOCMD | WRDSF_DELIM | WRDSF_WS); + free (value); + if (rc) { wordsplit_free (&ws); return 1; diff --git a/src/wordsplit.h b/src/wordsplit.h index 35e125a..4e2b117 100644 --- a/src/wordsplit.h +++ b/src/wordsplit.h @@ -36,7 +36,7 @@ struct wordsplit __attribute__ ((__format__ (__printf__, 1, 2))); const char **ws_env; - const char *(*ws_getvar) (const char *, size_t, void *); + char *(*ws_getvar) (const char *, size_t, void *); void *ws_closure; const char *ws_input; -- cgit v1.2.1