cproc

Unnamed repository; edit this file 'description' to name the repository.
git clone git://git.nihaljere.xyz/cproc
Log | Files | Refs | Submodules | README | LICENSE

commit a62e33a86dc36fff10c0a0d9d2aaecd311c2b08f
parent 240af0bbd03f4eef58a5b7c6da9f74a13682f172
Author: Nihal Jere <nihal@nihaljere.xyz>
Date:   Thu, 25 Mar 2021 00:08:54 -0500

expr.c: use temporary buffer when parsing string literal

This way we can read from the temporary buffer when converting between
different character widths

Diffstat:
Mexpr.c | 15+++++++++------
1 file changed, 9 insertions(+), 6 deletions(-)

diff --git a/expr.c b/expr.c @@ -456,8 +456,9 @@ primaryexpr(struct scope *s) struct expr *e; struct decl *d; struct type *t; - char *src, *dst, *end; + char *src, *dst, *end, *tmp; int base; + size_t tmpsize; switch (tok.kind) { case TIDENT: @@ -473,21 +474,23 @@ primaryexpr(struct scope *s) next(); break; case TSTRINGLIT: + tmp = NULL; + tmpsize = 0; e = mkexpr(EXPRSTRING, mkarraytype(&typechar, QUALNONE, 0)); e->lvalue = true; - e->string.size = 0; - e->string.data = NULL; do { - e->string.data = xreallocarray(e->string.data, e->string.size + strlen(tok.lit), 1); - dst = e->string.data + e->string.size; + tmp = xreallocarray(e->string.data, tmpsize + strlen(tok.lit), 1); + dst = tmp + tmpsize; src = tok.lit; if (*src != '"') fatal("wide string literal not yet implemented"); for (++src; *src != '"'; ++dst) *dst = unescape(&src); - e->string.size = dst - e->string.data; + tmpsize = dst - tmp; next(); } while (tok.kind == TSTRINGLIT); + e->string.data = tmp; + e->string.size = tmpsize; e->type->array.length = e->string.size + 1; e->type->size = e->type->array.length * e->type->base->size; e->type->incomplete = false;