aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--expr.c7
-rw-r--r--init.c2
-rw-r--r--test/hello.qbe2
-rw-r--r--test/initializer-replace-local.qbe10
-rw-r--r--test/initializer-replace-static.qbe2
-rw-r--r--test/initializer-string-braces.qbe2
-rw-r--r--test/initializer-string.qbe2
-rw-r--r--test/initializer-unsigned-string.qbe2
8 files changed, 15 insertions, 14 deletions
diff --git a/expr.c b/expr.c
index 8b10bf4..fd8bc63 100644
--- a/expr.c
+++ b/expr.c
@@ -478,7 +478,7 @@ primaryexpr(struct scope *s)
e->string.size = 0;
e->string.data = NULL;
do {
- e->string.data = xreallocarray(e->string.data, e->string.size + strlen(tok.lit), 1);
+ e->string.data = xreallocarray(e->string.data, e->string.size + strlen(tok.lit) + 1, 1);
dst = e->string.data + e->string.size;
src = tok.lit;
if (*src != '"')
@@ -488,7 +488,8 @@ primaryexpr(struct scope *s)
e->string.size = dst - e->string.data;
next();
} while (tok.kind == TSTRINGLIT);
- e->type->array.length = e->string.size + 1;
+ *dst = '\0';
+ e->type->array.length = ++e->string.size;
e->type->size = e->type->array.length * e->type->base->size;
e->type->incomplete = false;
e = decay(e);
@@ -620,7 +621,7 @@ builtinfunc(struct scope *s, enum builtinkind kind)
break;
case BUILTINNANF:
e = assignexpr(s);
- if (!e->decayed || e->base->kind != EXPRSTRING || e->base->string.size > 0)
+ if (!e->decayed || e->base->kind != EXPRSTRING || e->base->string.size > 1)
error(&tok.loc, "__builtin_nanf currently only supports empty string literals");
e = mkexpr(EXPRCONST, &typefloat);
/* TODO: use NAN here when we can handle musl's math.h */
diff --git a/init.c b/init.c
index 0e890c0..aadf951 100644
--- a/init.c
+++ b/init.c
@@ -251,7 +251,7 @@ parseinit(struct scope *s, struct type *t)
break;
expr = expr->base;
if (t->incomplete)
- updatearray(t, expr->string.size);
+ updatearray(t, expr->string.size - 1);
goto add;
case TYPESTRUCT:
case TYPEUNION:
diff --git a/test/hello.qbe b/test/hello.qbe
index 3e695ab..1826315 100644
--- a/test/hello.qbe
+++ b/test/hello.qbe
@@ -1,4 +1,4 @@
-data $.Lstring.2 = align 1 { b "hello", z 1, }
+data $.Lstring.2 = align 1 { b "hello\000", }
export
function w $main() {
@start.1
diff --git a/test/initializer-replace-local.qbe b/test/initializer-replace-local.qbe
index 72ad90a..316edb0 100644
--- a/test/initializer-replace-local.qbe
+++ b/test/initializer-replace-local.qbe
@@ -13,10 +13,10 @@ function $f() {
storeb 108, %.5
%.6 =l add %.1, 4
storeb 111, %.6
- %.7 =l add %.1, 1
- %.8 =w copy 97
- storeb %.8, %.7
- %.9 =l add %.1, 5
- storeb 0, %.9
+ %.7 =l add %.1, 5
+ storeb 0, %.7
+ %.8 =l add %.1, 1
+ %.9 =w copy 97
+ storeb %.9, %.8
ret
}
diff --git a/test/initializer-replace-static.qbe b/test/initializer-replace-static.qbe
index 18b774e..8d8f2cd 100644
--- a/test/initializer-replace-static.qbe
+++ b/test/initializer-replace-static.qbe
@@ -1 +1 @@
-export data $x = align 1 { b "hallo", z 1, }
+export data $x = align 1 { b "hallo\000", }
diff --git a/test/initializer-string-braces.qbe b/test/initializer-string-braces.qbe
index 46be123..5d44117 100644
--- a/test/initializer-string-braces.qbe
+++ b/test/initializer-string-braces.qbe
@@ -1 +1 @@
-export data $s = align 1 { b "abc", z 1, }
+export data $s = align 1 { b "abc\000", }
diff --git a/test/initializer-string.qbe b/test/initializer-string.qbe
index ba992da..ee83e50 100644
--- a/test/initializer-string.qbe
+++ b/test/initializer-string.qbe
@@ -1,4 +1,4 @@
-export data $x = align 1 { b "hello", z 1, }
+export data $x = align 1 { b "hello\000", }
export
function $f() {
@start.1
diff --git a/test/initializer-unsigned-string.qbe b/test/initializer-unsigned-string.qbe
index 46be123..5d44117 100644
--- a/test/initializer-unsigned-string.qbe
+++ b/test/initializer-unsigned-string.qbe
@@ -1 +1 @@
-export data $s = align 1 { b "abc", z 1, }
+export data $s = align 1 { b "abc\000", }