summaryrefslogtreecommitdiffabout
path: root/src
authorSergey Poznyakoff <gray@gnu.org>2014-09-08 22:01:41 (GMT)
committer Sergey Poznyakoff <gray@gnu.org.ua>2015-12-17 13:24:37 (GMT)
commit3d7ee4dacf54e8000fd126d49881dd322f5926b9 (patch) (side-by-side diff)
tree8788c6bdba66a8b47a21a36a3c10138a99eea3a7 /src
parentbfc4869c9fda509e26a038d5c7485d800544bdf1 (diff)
downloadgrecs-3d7ee4dacf54e8000fd126d49881dd322f5926b9.tar.gz
grecs-3d7ee4dacf54e8000fd126d49881dd322f5926b9.tar.bz2
wordsplit: ws_getvar allocates memory.
* src/wordsplit.c (ISVARBEG,ISVARCHR): New macros. (expvar): ws_getvar allocates memory. * src/wordsplit.h (wordsplit)<ws_getvar>: Remove const from the return value: the function should allocate memory.
Diffstat (limited to 'src') (more/less context) (ignore whitespace changes)
-rw-r--r--src/wordsplit.c29
-rw-r--r--src/wordsplit.h2
2 files changed, 20 insertions, 11 deletions
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;

Return to:

Send suggestions and report system problems to the System administrator.