Commit e7fc5c27 authored by Russ Cox's avatar Russ Cox

gc: test & fix handling of very long string constants

R=ken2
CC=golang-dev
https://golang.org/cl/207106
parent 115066fd
...@@ -420,7 +420,7 @@ struct Loophack { ...@@ -420,7 +420,7 @@ struct Loophack {
static int32 static int32
_yylex(void) _yylex(void)
{ {
int c, c1, clen, escflag; int c, c1, clen, escflag, ncp;
vlong v; vlong v;
char *cp; char *cp;
Rune rune; Rune rune;
...@@ -490,20 +490,22 @@ l0: ...@@ -490,20 +490,22 @@ l0:
case '"': case '"':
/* "..." */ /* "..." */
strcpy(lexbuf, "\"<string>\""); strcpy(lexbuf, "\"<string>\"");
cp = mal(sizeof(int32)); cp = mal(8);
clen = sizeof(int32); clen = sizeof(int32);
ncp = 8;
for(;;) { for(;;) {
if(clen+UTFmax > ncp) {
cp = remal(cp, ncp, ncp);
ncp += ncp;
}
if(escchar('"', &escflag, &v)) if(escchar('"', &escflag, &v))
break; break;
if(v < Runeself || escflag) { if(v < Runeself || escflag) {
cp = remal(cp, clen, 1);
cp[clen++] = v; cp[clen++] = v;
} else { } else {
// botch - this limits size of runes
rune = v; rune = v;
c = runelen(rune); c = runelen(rune);
cp = remal(cp, clen, c);
runetochar(cp+clen, &rune); runetochar(cp+clen, &rune);
clen += c; clen += c;
} }
...@@ -513,10 +515,15 @@ l0: ...@@ -513,10 +515,15 @@ l0:
case '`': case '`':
/* `...` */ /* `...` */
strcpy(lexbuf, "`<string>`"); strcpy(lexbuf, "`<string>`");
cp = mal(sizeof(int32)); cp = mal(8);
clen = sizeof(int32); clen = sizeof(int32);
ncp = 8;
for(;;) { for(;;) {
if(clen == ncp) {
cp = remal(cp, clen, ncp);
ncp += ncp;
}
c = getc(); c = getc();
if(c == EOF) { if(c == EOF) {
yyerror("eof in string"); yyerror("eof in string");
...@@ -524,14 +531,12 @@ l0: ...@@ -524,14 +531,12 @@ l0:
} }
if(c == '`') if(c == '`')
break; break;
cp = remal(cp, clen, 1);
cp[clen++] = c; cp[clen++] = c;
} }
strlit: strlit:
*(int32*)cp = clen-sizeof(int32); // length *(int32*)cp = clen-sizeof(int32); // length
do { do {
cp = remal(cp, clen, 1);
cp[clen++] = 0; cp[clen++] = 0;
} while(clen & MAXALIGN); } while(clen & MAXALIGN);
yylval.val.u.sval = (Strlit*)cp; yylval.val.u.sval = (Strlit*)cp;
......
...@@ -364,7 +364,7 @@ importdot(Pkg *opkg, Node *pack) ...@@ -364,7 +364,7 @@ importdot(Pkg *opkg, Node *pack)
} }
} }
void static void
gethunk(void) gethunk(void)
{ {
char *h; char *h;
...@@ -374,7 +374,7 @@ gethunk(void) ...@@ -374,7 +374,7 @@ gethunk(void)
if(thunk >= 10L*NHUNK) if(thunk >= 10L*NHUNK)
nh = 10L*NHUNK; nh = 10L*NHUNK;
h = (char*)malloc(nh); h = (char*)malloc(nh);
if(h == (char*)-1) { if(h == nil) {
flusherrors(); flusherrors();
yyerror("out of memory"); yyerror("out of memory");
errorexit(); errorexit();
...@@ -389,11 +389,22 @@ mal(int32 n) ...@@ -389,11 +389,22 @@ mal(int32 n)
{ {
void *p; void *p;
if(n >= NHUNK) {
p = malloc(n);
if(p == nil) {
flusherrors();
yyerror("out of memory");
errorexit();
}
memset(p, 0, n);
return p;
}
while((uintptr)hunk & MAXALIGN) { while((uintptr)hunk & MAXALIGN) {
hunk++; hunk++;
nhunk--; nhunk--;
} }
while(nhunk < n) if(nhunk < n)
gethunk(); gethunk();
p = hunk; p = hunk;
...@@ -410,7 +421,12 @@ remal(void *p, int32 on, int32 n) ...@@ -410,7 +421,12 @@ remal(void *p, int32 on, int32 n)
q = (uchar*)p + on; q = (uchar*)p + on;
if(q != hunk || nhunk < n) { if(q != hunk || nhunk < n) {
while(nhunk < on+n) if(on+n >= NHUNK) {
q = mal(on+n);
memmove(q, p, on);
return q;
}
if(nhunk < on+n)
gethunk(); gethunk();
memmove(hunk, p, on); memmove(hunk, p, on);
p = hunk; p = hunk;
......
This diff is collapsed.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment