Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reduce internal dependency on 0 terminated strings #5220

Merged
merged 1 commit into from Oct 23, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/attrib.d
Expand Up @@ -413,7 +413,7 @@ public:
char* depmsg = null;
StringExp se = msg.toStringExp();
if (se)
depmsg = cast(char*)se.string;
depmsg = se.toStringz();
else
msg.error("string expected, not '%s'", msg.toChars());
Scope* scx = sc.push();
Expand Down Expand Up @@ -1266,7 +1266,8 @@ public:
{
se = se.toUTF8(sc);
uint errors = global.errors;
scope Parser p = new Parser(loc, sc._module, cast(char*)se.string, se.len, 0);
auto cstr = se.toStringz();
scope Parser p = new Parser(loc, sc._module, cstr, se.len, 0);
p.nextToken();
decl = p.parseDeclDefs(0);
if (p.token.value != TOKeof)
Expand Down
12 changes: 7 additions & 5 deletions src/backend/dt.c
Expand Up @@ -175,23 +175,25 @@ dt_t ** dtnbytes(dt_t **pdtend,unsigned size,const char *ptr)
* Construct a DTabytes record, and return it.
*/

dt_t **dtabytes(dt_t **pdtend, unsigned offset, unsigned size, const char *ptr)
dt_t **dtabytes(dt_t **pdtend, unsigned offset, unsigned size, const char *ptr, unsigned nbytes)
{
return dtabytes(pdtend, TYnptr, offset, size, ptr);
return dtabytes(pdtend, TYnptr, offset, size, ptr, nbytes);
}

dt_t **dtabytes(dt_t **pdtend,tym_t ty, unsigned offset, unsigned size, const char *ptr)
dt_t **dtabytes(dt_t **pdtend,tym_t ty, unsigned offset, unsigned size, const char *ptr, unsigned nbytes)
{ dt_t *dt;

while (*pdtend)
pdtend = &((*pdtend)->DTnext);

dt = dt_calloc(DT_abytes);
dt->DTnbytes = size;
dt->DTpbytes = (char *) MEM_PH_MALLOC(size);
dt->DTnbytes = size + nbytes;
dt->DTpbytes = (char *) MEM_PH_MALLOC(size + nbytes);
dt->Dty = ty;
dt->DTabytes = offset;
memcpy(dt->DTpbytes,ptr,size);
if (size)
memset(dt->DTpbytes + size, 0, nbytes);

*pdtend = dt;
pdtend = &dt->DTnext;
Expand Down
4 changes: 2 additions & 2 deletions src/backend/dt.h
Expand Up @@ -11,8 +11,8 @@ void dt_free(dt_t *);
void dt_term();

dt_t **dtnbytes(dt_t **,unsigned,const char *);
dt_t **dtabytes(dt_t **pdtend,tym_t ty, unsigned offset, unsigned size, const char *ptr);
dt_t **dtabytes(dt_t **pdtend, unsigned offset, unsigned size, const char *ptr);
dt_t **dtabytes(dt_t **pdtend,tym_t ty, unsigned offset, unsigned size, const char *ptr, unsigned nzeros);
dt_t **dtabytes(dt_t **pdtend, unsigned offset, unsigned size, const char *ptr, unsigned nzeros);
dt_t **dtdword(dt_t **, int value);
dt_t **dtsize_t(dt_t **, unsigned long long value);
dt_t **dtnzeros(dt_t **pdtend,unsigned size);
Expand Down
11 changes: 7 additions & 4 deletions src/e2ir.c
Expand Up @@ -1391,9 +1391,11 @@ elem *toElem(Expression *e, IRState *irs)
e = el_calloc();
e->Eoper = OPstring;
// freed in el_free
e->EV.ss.Vstring = (char *)mem_malloc((se->len + 1) * se->sz);
memcpy(e->EV.ss.Vstring, se->string, (se->len + 1) * se->sz);
e->EV.ss.Vstrlen = (se->len + 1) * se->sz;
unsigned len = se->len * se->sz;
e->EV.ss.Vstring = (char *)mem_malloc(len + se->sz);
memcpy(e->EV.ss.Vstring, se->string, len);
memset(e->EV.ss.Vstring + len, 0, se->sz);
e->EV.ss.Vstrlen = len + se->sz;
e->Ety = TYnptr;
}
else
Expand Down Expand Up @@ -5593,7 +5595,8 @@ Symbol *toStringSymbol(const char *str, size_t len, size_t sz)
Symbol *si = symbol_generate(SCstatic,type_static_array(len * sz, tschar));
si->Salignment = 1;
si->Sdt = NULL;
dtnbytes(&si->Sdt, (len + 1) * sz, str);
dt_t **pdt = dtnbytes(&si->Sdt, len * sz, str);
dtnzeros(pdt, sz);
si->Sfl = FLdata;
out_readonly(si);
outdata(si);
Expand Down
29 changes: 22 additions & 7 deletions src/expression.d
Expand Up @@ -4428,6 +4428,19 @@ public:
return value;
}

/********************************
* Convert string contents to a 0 terminated string,
* allocated by mem.xmalloc().
*/
final char* toStringz()
{
auto nbytes = len * sz;
char* s = cast(char*)mem.xmalloc(nbytes + sz);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

auto ...

memcpy(s, string, nbytes);
memset(s + nbytes, 0, sz);
return s;
}

override void accept(Visitor v)
{
v.visit(this);
Expand Down Expand Up @@ -7602,12 +7615,14 @@ public:

override Expression semantic(Scope* sc)
{
const(char)* name;
StringExp se;
static if (LOGSEMANTIC)
{
printf("FileExp::semantic('%s')\n", toChars());
}
const(char)* name;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

urgh again with the pascal convention of putting all variables at the beginning...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That was necessary because of the goto Lerror; statements.

char* namez;
StringExp se;

sc = sc.startCTFE();
e1 = e1.semantic(sc);
e1 = resolveProperties(sc, e1);
Expand All @@ -7620,24 +7635,24 @@ public:
}
se = cast(StringExp)e1;
se = se.toUTF8(sc);
name = cast(char*)se.string;
namez = se.toStringz();
if (!global.params.fileImppath)
{
error("need -Jpath switch to import text file %s", name);
error("need -Jpath switch to import text file %s", namez);
goto Lerror;
}
/* Be wary of CWE-22: Improper Limitation of a Pathname to a Restricted Directory
* ('Path Traversal') attacks.
* http://cwe.mitre.org/data/definitions/22.html
*/
name = FileName.safeSearchPath(global.filePath, name);
name = FileName.safeSearchPath(global.filePath, namez);
if (!name)
{
error("file %s cannot be found or not in a path specified with -J", se.toChars());
goto Lerror;
}
if (global.params.verbose)
fprintf(global.stdmsg, "file %s\t(%s)\n", cast(char*)se.string, name);
fprintf(global.stdmsg, "file %.*s\t(%s)\n", cast(int)se.len, se.string, name);
if (global.params.moduleDeps !is null)
{
OutBuffer* ob = global.params.moduleDeps;
Expand All @@ -7650,7 +7665,7 @@ public:
ob.writestring(") : ");
if (global.params.moduleDepsFile)
ob.writestring("string : ");
ob.writestring(cast(char*)se.string);
ob.write(se.string, se.len);
ob.writestring(" (");
escapePath(ob, name);
ob.writestring(")");
Expand Down
1 change: 1 addition & 0 deletions src/expression.h
Expand Up @@ -380,6 +380,7 @@ class StringExp : public Expression
Expression *modifiableLvalue(Scope *sc, Expression *e);
unsigned charAt(uinteger_t i);
void accept(Visitor *v) { v->visit(this); }
char *toStringz();
};

// Tuple
Expand Down
6 changes: 2 additions & 4 deletions src/todt.c
Expand Up @@ -381,7 +381,8 @@ dt_t **Expression_toDt(Expression *e, dt_t **pdt)
{
case Tarray:
pdt = dtsize_t(pdt, e->len);
pdt = dtabytes(pdt, 0, (e->len + 1) * e->sz, (char *)e->string);
case Tpointer:
pdt = dtabytes(pdt, 0, e->len * e->sz, (const char *)e->string, (unsigned)e->sz);
break;

case Tsarray:
Expand All @@ -400,9 +401,6 @@ dt_t **Expression_toDt(Expression *e, dt_t **pdt)
}
break;
}
case Tpointer:
pdt = dtabytes(pdt, 0, (e->len + 1) * e->sz, (char *)e->string);
break;

default:
printf("StringExp::toDt(type = %s)\n", e->type->toChars());
Expand Down