Skip to content
This repository has been archived by the owner on Jun 20, 2019. It is now read-only.

Commit

Permalink
Make a copy of string AST value to a null terminated temporary
Browse files Browse the repository at this point in the history
  • Loading branch information
ibuclaw committed May 28, 2018
1 parent 44ffbdf commit b9d36fc
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
5 changes: 5 additions & 0 deletions gcc/d/ChangeLog
@@ -1,3 +1,8 @@
2018-05-28 Iain Buclaw <ibuclaw@gdcproject.org>

* expr.cc (ExprVisitor::visit(StringExp)): Copy string literal from
the frontend to a null terminated string.

2018-05-21 Iain Buclaw <ibuclaw@gdcproject.org>

* expr.cc (ExprVisitor::binary_op): Don't do complex conversions if
Expand Down
19 changes: 11 additions & 8 deletions gcc/d/expr.cc
Expand Up @@ -2595,14 +2595,12 @@ class ExprVisitor : public Visitor
build_float_cst (cimagl (e->value), tnext));
}

/* Build a string literal. */
/* Build a string literal, all strings are null terminated except for
static arrays. */

void visit (StringExp *e)
{
Type *tb = e->type->toBasetype ();

/* All strings are null terminated except static arrays. */
const char *string = (const char *)(e->len ? e->string : "");
tree type = build_ctype (e->type);

if (tb->ty == Tsarray)
Expand All @@ -2624,10 +2622,15 @@ class ExprVisitor : public Visitor
}
else
{
/* Array type string length includes the null terminator. */
dinteger_t length = (e->len * e->sz) + 1;
tree value = build_string (length, string);
TREE_TYPE (value) = make_array_type (tb->nextOf (), length);
/* Copy the string contents to a null terminated string. */
dinteger_t length = (e->len * e->sz);
char *string = XALLOCAVEC (char, length + 1);
memcpy (string, e->string, length);
string[length] = '\0';

/* String value and type includes the null terminator. */
tree value = build_string (length + 1, string);
TREE_TYPE (value) = make_array_type (tb->nextOf (), length + 1);
value = build_address (value);

if (tb->ty == Tarray)
Expand Down

0 comments on commit b9d36fc

Please sign in to comment.