commit 4b2a9917e3ec7526fabc5af45ffb7fee72aca398
parent 8a24dd52a20eb53612d4ea3cf36d7d99d5d9b229
Author: Bor Grošelj Simić <bor.groseljsimic@telemach.net>
Date: Mon, 15 Mar 2021 19:32:56 +0100
do not treat string constants as null-terminated
Diffstat:
3 files changed, 10 insertions(+), 4 deletions(-)
diff --git a/src/check.c b/src/check.c
@@ -446,8 +446,12 @@ check_expr_assert(struct context *ctx,
// XXX: Should these abort immediately?
if (aexpr->assert.message != NULL) {
if (!cond) {
- return error(aexpr->assert.cond->loc, expr, errors,
- "Static assertion failed: %s",
+ char format[40];
+ snprintf(format, 40, "Static assertion failed %%%lds",
+ expr->assert.message->constant.string.len);
+ return error(aexpr->assert.cond->loc,
+ expr,
+ errors, format,
expr->assert.message->constant.string.value);
}
} else if (!cond) {
diff --git a/src/eval.c b/src/eval.c
@@ -461,7 +461,7 @@ constant_default(struct context *ctx, struct expression *v)
assert(r == EVAL_OK);
break;
case STORAGE_STRING:
- v->constant.string.value = strdup("");
+ v->constant.string.value = NULL;
v->constant.string.len = 0;
break;
case STORAGE_ARRAY:
diff --git a/src/gen.c b/src/gen.c
@@ -2731,8 +2731,10 @@ gen_data_item(struct gen_context *ctx, struct expression *expr,
def->name = gen_name(ctx, "strdata.%d");
def->kind = Q_DATA;
def->data.items.type = QD_STRING;
- def->data.items.str = strdup(expr->constant.string.value);
+ def->data.items.str = xcalloc(1, expr->constant.string.len);
def->data.items.sz = expr->constant.string.len;
+ memcpy(def->data.items.str, expr->constant.string.value,
+ expr->constant.string.len);
item->type = QD_VALUE;
if (expr->constant.string.len != 0) {