[hackers] [scc] Check integer overflow in symbol id || Roberto E. Vargas Caballero
commit abdf69a520ddaf86c5bef1ce52a64dea1f3b5e77
Author: Roberto E. Vargas Caballero <k0ga_AT_shike2.com>
AuthorDate: Mon Aug 17 14:34:30 2015 +0200
Commit: Roberto E. Vargas Caballero <k0ga_AT_shike2.com>
CommitDate: Mon Aug 17 14:34:30 2015 +0200
Check integer overflow in symbol id
diff --git a/cc1/cc1.h b/cc1/cc1.h
index ea8ad78..ec930ea 100644
--- a/cc1/cc1.h
+++ b/cc1/cc1.h
_AT_@ -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
index ec27c71..01a641b 100644
--- a/cc1/code.c
+++ b/cc1/code.c
_AT_@ -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
_AT_@ -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
index 1088b0d..5738d2e 100644
--- a/cc1/symbol.c
+++ b/cc1/symbol.c
_AT_@ -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];
_AT_@ -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;
}
_AT_@ -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;
}
_AT_@ -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;
}
Received on Tue Aug 18 2015 - 10:02:55 CEST
This archive was generated by hypermail 2.3.0
: Tue Aug 18 2015 - 10:12:15 CEST