system/corennnnn
リビジョン | f1f39cca305f6f3d2d91b88736b7b2b520d59e2e (tree) |
---|---|
日時 | 2009-05-30 10:03:15 |
作者 | Jack Palevich <jackpal@goog...> |
コミッター | Jack Palevich |
Make sure we don't overflow various internal compiler buffers.
We may replace some of these tables with dynamically growing data
structures, but in the meantime we will not trash memory.
@@ -1083,7 +1083,7 @@ class Compiler : public ErrorSink { | ||
1083 | 1083 | int dch; // Macro state: Saves old value of ch during a macro playback. |
1084 | 1084 | char* last_id; |
1085 | 1085 | void* pSymbolBase; |
1086 | - void* pGlobalBase; | |
1086 | + char* pGlobalBase; | |
1087 | 1087 | char* pVarsBase; // Value of variables |
1088 | 1088 | |
1089 | 1089 | InputStream* file; |
@@ -1154,6 +1154,9 @@ class Compiler : public ErrorSink { | ||
1154 | 1154 | static const char operatorLevel[]; |
1155 | 1155 | |
1156 | 1156 | void pdef(int t) { |
1157 | + if (dstk - sym_stk >= ALLOC_SIZE) { | |
1158 | + error("Symbol table exhausted"); | |
1159 | + } | |
1157 | 1160 | *dstk++ = t; |
1158 | 1161 | } |
1159 | 1162 |
@@ -1219,6 +1222,9 @@ class Compiler : public ErrorSink { | ||
1219 | 1222 | tokc = strtol(last_id, 0, 0); |
1220 | 1223 | tok = TOK_NUM; |
1221 | 1224 | } else { |
1225 | + if (dstk - sym_stk + 1 > ALLOC_SIZE) { | |
1226 | + error("symbol stack overflow"); | |
1227 | + } | |
1222 | 1228 | * dstk = TAG_TOK; /* no need to mark end of string (we |
1223 | 1229 | suppose data is initialized to zero by calloc) */ |
1224 | 1230 | tok = (intptr_t) (strstr(sym_stk, (last_id - 1)) |
@@ -1226,6 +1232,9 @@ class Compiler : public ErrorSink { | ||
1226 | 1232 | * dstk = 0; /* mark real end of ident for dlsym() */ |
1227 | 1233 | tok = tok * 8 + TOK_IDENT; |
1228 | 1234 | if (tok > TOK_DEFINE) { |
1235 | + if (tok + 8 > ALLOC_SIZE) { | |
1236 | + error("Variable Table overflow."); | |
1237 | + } | |
1229 | 1238 | tok = (intptr_t) (pVarsBase + tok); |
1230 | 1239 | /* printf("tok=%s %x\n", last_id, tok); */ |
1231 | 1240 | /* define handling */ |
@@ -1350,11 +1359,12 @@ class Compiler : public ErrorSink { | ||
1350 | 1359 | pGen->li((int) glo); |
1351 | 1360 | while (ch != '\"') { |
1352 | 1361 | getq(); |
1353 | - *glo++ = ch; | |
1362 | + *allocGlobalSpace(1) = ch; | |
1354 | 1363 | inp(); |
1355 | 1364 | } |
1356 | 1365 | *glo = 0; |
1357 | - glo = (char*) (((intptr_t) glo + 4) & -4); /* align heap */ | |
1366 | + /* align heap */ | |
1367 | + allocGlobalSpace((char*) (((intptr_t) glo + 4) & -4) - glo); | |
1358 | 1368 | inp(); |
1359 | 1369 | next(); |
1360 | 1370 | } else { |
@@ -1584,8 +1594,7 @@ class Compiler : public ErrorSink { | ||
1584 | 1594 | loc = loc + 4; |
1585 | 1595 | *(int *) tok = -loc; |
1586 | 1596 | } else { |
1587 | - *(int* *) tok = (int*) glo; | |
1588 | - glo = glo + 4; | |
1597 | + *(int* *) tok = (int*) allocGlobalSpace(4); | |
1589 | 1598 | } |
1590 | 1599 | next(); |
1591 | 1600 | if (tok == ',') |
@@ -1621,13 +1630,22 @@ class Compiler : public ErrorSink { | ||
1621 | 1630 | } |
1622 | 1631 | } |
1623 | 1632 | |
1633 | + char* allocGlobalSpace(int bytes) { | |
1634 | + if (glo - pGlobalBase + bytes > ALLOC_SIZE) { | |
1635 | + error("Global space exhausted"); | |
1636 | + } | |
1637 | + char* result = glo; | |
1638 | + glo += bytes; | |
1639 | + return result; | |
1640 | + } | |
1641 | + | |
1624 | 1642 | void cleanup() { |
1625 | 1643 | if (sym_stk != 0) { |
1626 | 1644 | free(sym_stk); |
1627 | 1645 | sym_stk = 0; |
1628 | 1646 | } |
1629 | 1647 | if (pGlobalBase != 0) { |
1630 | - free((void*) pGlobalBase); | |
1648 | + free(pGlobalBase); | |
1631 | 1649 | pGlobalBase = 0; |
1632 | 1650 | } |
1633 | 1651 | if (pVarsBase != 0) { |
@@ -1730,8 +1748,8 @@ public: | ||
1730 | 1748 | dstk = strcpy(sym_stk, |
1731 | 1749 | " int if else while break return for define main ") |
1732 | 1750 | + TOK_STR_SIZE; |
1733 | - pGlobalBase = calloc(1, ALLOC_SIZE); | |
1734 | - glo = (char*) pGlobalBase; | |
1751 | + pGlobalBase = (char*) calloc(1, ALLOC_SIZE); | |
1752 | + glo = pGlobalBase; | |
1735 | 1753 | pVarsBase = (char*) calloc(1, ALLOC_SIZE); |
1736 | 1754 | inp(); |
1737 | 1755 | next(); |