From 7c8f51ed0d1303503f1accb75eac7a18c3a9f576 Mon Sep 17 00:00:00 2001 From: ketmar Date: Thu, 3 Oct 2013 16:10:06 +0300 Subject: [PATCH] fixed idiotic bug in expander: single-quoted strings was BAD --- src/expand.c | 45 +++++++++++++++++++++++---------------------- 1 file changed, 23 insertions(+), 22 deletions(-) diff --git a/src/expand.c b/src/expand.c index f645f34..e374bb9 100644 --- a/src/expand.c +++ b/src/expand.c @@ -245,7 +245,6 @@ static const char *parse_modifier (const char *s, const char *e, LOL *lol, char /* do it easy way: collect string and pass it to var_expand() */ /* note that quoting works only for the whole string, not for string parts */ if (s < e) { - int need_expand = 0; if (*s == '\'') { /* single-quoted string; the easiest thing */ for (++s; s < e; ++s) { @@ -253,9 +252,10 @@ static const char *parse_modifier (const char *s, const char *e, LOL *lol, char ++s; if (s >= e || *s != '\'') break; } - if (rep != NULL) dstr_push_char(rep, *s++); + dstr_push_char(rep, *s); } } else { + int need_expand = 0; char qch = (*s == '"' ? '"' : 0); if (qch) ++s; while (s < e) { @@ -264,7 +264,7 @@ static const char *parse_modifier (const char *s, const char *e, LOL *lol, char /* variable access */ const char *ve = skip_varaccess(s, e); if (ve == NULL) { printf("FATAL: invalid var access: '%.*s'\n", (int)(e-s), s); exit(42); } - if (rep != NULL) dstr_push_memrange(rep, s, ve); + dstr_push_memrange(rep, s, ve); need_expand = 1; s = ve; continue; @@ -275,14 +275,14 @@ static const char *parse_modifier (const char *s, const char *e, LOL *lol, char int n; ++s; switch (*s++) { - case 'a': if (rep != NULL) dstr_push_char(rep, '\a'); break; - case 'b': if (rep != NULL) dstr_push_char(rep, '\b'); break; - case 'e': if (rep != NULL) dstr_push_char(rep, '\x1b'); break; - case 'f': if (rep != NULL) dstr_push_char(rep, '\f'); break; - case 'n': if (rep != NULL) dstr_push_char(rep, '\n'); break; - case 'r': if (rep != NULL) dstr_push_char(rep, '\r'); break; - case 't': if (rep != NULL) dstr_push_char(rep, '\t'); break; - case 'v': if (rep != NULL) dstr_push_char(rep, '\v'); break; + case 'a': dstr_push_char(rep, '\a'); break; + case 'b': dstr_push_char(rep, '\b'); break; + case 'e': dstr_push_char(rep, '\x1b'); break; + case 'f': dstr_push_char(rep, '\f'); break; + case 'n': dstr_push_char(rep, '\n'); break; + case 'r': dstr_push_char(rep, '\r'); break; + case 't': dstr_push_char(rep, '\t'); break; + case 'v': dstr_push_char(rep, '\v'); break; case 'x': if (s >= e) { printf("FATAL: invalid hex escape!\n"); exit(42); } n = digit(*s++, 16); /* first digit */ @@ -294,17 +294,17 @@ static const char *parse_modifier (const char *s, const char *e, LOL *lol, char n = n*16+d; } if (n == 0) { printf("FATAL: invalid hex escape!\n"); exit(42); } - if (rep != NULL) dstr_push_char(rep, n); + dstr_push_char(rep, n); break; /*TODO: add '\uXXXX'?*/ default: if (isalnum(s[-1])) { printf("FATAL: invalid escape: '%c'!\n", s[-1]); exit(42); } - if (rep != NULL) dstr_push_char(rep, s[-1]); + dstr_push_char(rep, s[-1]); break; } } else { if (s+1 < e) ++s; - if (rep != NULL) dstr_push_char(rep, *s); + dstr_push_char(rep, *s); ++s; } continue; @@ -316,16 +316,16 @@ static const char *parse_modifier (const char *s, const char *e, LOL *lol, char break; } /* normal char */ - if (rep != NULL) dstr_push_char(rep, *s); + dstr_push_char(rep, *s); ++s; } - } - /* now expand collected string */ - if (rep != NULL && need_expand) { - LIST *l = var_expand(dstr_cstr(rep), NULL, lol, 0); - dstr_clear(rep); - if (l != L0) dstr_set_cstr(rep, l->string); - list_free(l); + /* now expand collected string */ + if (rep != NULL && need_expand) { + LIST *l = var_expand(dstr_cstr(rep), NULL, lol, 0); + dstr_clear(rep); + if (l != L0) dstr_set_cstr(rep, l->string); + list_free(l); + } } } } @@ -439,6 +439,7 @@ static void process_selectors (dstring_t *res, const char *s, const char *e, LOL continue; } s = parse_modifier(s, e, lol, &mc, &mval, &wasass); + if (DEBUG_VAREXP) printf("mod: '%c'; len=%d; s=<%s>\n", mc, dstr_len(&mval), dstr_cstr(&mval)); if (s == NULL) abort(); /* the thing that should not be */ { int mvlen = (wasass ? dstr_len(&mval) : 0); -- 2.11.4.GIT