scc

Simple C99 Compiler
Log | Files | Refs | README | LICENSE

commit abdf69a520ddaf86c5bef1ce52a64dea1f3b5e77
parent f20cb974e12e176500a0f44c4ebf3332e26dc463
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Mon, 17 Aug 2015 14:34:30 +0200

Check integer overflow in symbol id

Diffstat:
cc1/cc1.h | 2+-
cc1/code.c | 4++--
cc1/symbol.c | 23++++++++++++++++++-----
3 files changed, 21 insertions(+), 8 deletions(-)

diff --git a/cc1/cc1.h b/cc1/cc1.h @@ -46,7 +46,7 @@ struct type { struct symbol { char *name; Type *type; - short id; + unsigned short id; unsigned char ctx; unsigned char ns; unsigned char token; diff --git a/cc1/code.c b/cc1/code.c @@ -172,7 +172,7 @@ emitvar(Symbol *sym) c = L_EXTERN; else c = L_AUTO; - printf("%c%d", c, sym->id); + printf("%c%u", c, sym->id); } static void @@ -217,7 +217,7 @@ emitletter(Type *tp) case FTN: case STRUCT: case UNION: - printf("%d", tp->id); + printf("%u", tp->id); } } diff --git a/cc1/symbol.c b/cc1/symbol.c @@ -12,8 +12,8 @@ #define NR_SYM_HASH 64 unsigned curctx; -static short localcnt; -static short globalcnt; +static unsigned short localcnt; +static unsigned short globalcnt; static Symbol *head, *labels; static Symbol *htab[NR_SYM_HASH]; @@ -138,13 +138,26 @@ popctx(void) head = sym; } +static unsigned short +newid(void) +{ + unsigned id; + + id = (curctx) ? ++localcnt : ++globalcnt; + if (id == 0) { + die("Overflow in %s identifiers", + (curctx) ? "internal" : "external"); + } + return id; +} + Type * duptype(Type *base) { Type *tp = xmalloc(sizeof(*tp)); *tp = *base; - tp->id = (curctx) ? ++localcnt : ++globalcnt; + tp->id = newid(); return tp; } @@ -167,7 +180,7 @@ newsym(unsigned ns) return sym; if (ns == NS_LABEL) { sym->next = labels; - sym->id = ++localcnt; + sym->id = newid(); return labels = sym; } @@ -263,7 +276,7 @@ install(unsigned ns, Symbol *sym) assign_id: if (sym->ns != NS_CPP || sym->ns != NS_LABEL) - sym->id = (curctx) ? ++localcnt : ++globalcnt; + sym->id = newid(); return sym; }