Skip to content

8cc codegen is accessing (potentially) uninitialized struct fields #67

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@
#define INIT_SIZE 8

Buffer *make_buffer() {
Buffer *r = malloc(sizeof(Buffer));
r->body = malloc(INIT_SIZE);
Buffer *r = calloc(1, sizeof(Buffer));
r->body = calloc(1, INIT_SIZE);
r->nalloc = INIT_SIZE;
r->len = 0;
return r;
}

static void realloc_body(Buffer *b) {
int newsize = b->nalloc * 2;
char *body = malloc(newsize);
char *body = calloc(1, newsize);
memcpy(body, b->body, b->len);
b->body = body;
b->nalloc = newsize;
Expand Down
8 changes: 4 additions & 4 deletions cpp.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,14 @@ static Token *read_expand(void);
*/

static CondIncl *make_cond_incl(bool wastrue) {
CondIncl *r = malloc(sizeof(CondIncl));
CondIncl *r = calloc(1, sizeof(CondIncl));
r->ctx = IN_THEN;
r->wastrue = wastrue;
return r;
}

static Macro *make_macro(Macro *tmpl) {
Macro *r = malloc(sizeof(Macro));
Macro *r = calloc(1, sizeof(Macro));
*r = *tmpl;
return r;
}
Expand All @@ -81,7 +81,7 @@ static Macro *make_special_macro(SpecialMacroHandler *fn) {
}

static Token *make_macro_token(int position, bool is_vararg) {
Token *r = malloc(sizeof(Token));
Token *r = calloc(1, sizeof(Token));
r->kind = TMACRO_PARAM;
r->is_vararg = is_vararg;
r->hideset = NULL;
Expand All @@ -92,7 +92,7 @@ static Token *make_macro_token(int position, bool is_vararg) {
}

static Token *copy_token(Token *tok) {
Token *r = malloc(sizeof(Token));
Token *r = calloc(1, sizeof(Token));
*r = *tok;
return r;
}
Expand Down
2 changes: 1 addition & 1 deletion dict.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include "8cc.h"

Dict *make_dict() {
Dict *r = malloc(sizeof(Dict));
Dict *r = calloc(1, sizeof(Dict));
r->map = make_map();
r->key = make_vector();
return r;
Expand Down
6 changes: 3 additions & 3 deletions gen.c
Original file line number Diff line number Diff line change
Expand Up @@ -645,7 +645,7 @@ static void emit_fill_holes(Vector *inits, int off, int totalsize) {
// If at least one of the fields in a variable are initialized,
// unspecified fields has to be initialized with 0.
int len = vec_len(inits);
Node **buf = malloc(len * sizeof(Node *));
Node **buf = calloc(len, sizeof(Node *));
for (int i = 0; i < len; i++)
buf[i] = vec_get(inits, i);
qsort(buf, len, sizeof(Node *), cmpinit);
Expand Down Expand Up @@ -784,7 +784,7 @@ static char **split(char *buf) {
p++;
}
p = buf;
char **r = malloc(sizeof(char *) * len + 1);
char **r = calloc(len + 1, sizeof(char *));
int i = 0;
while (*p) {
if (p[0] == '\r' && p[1] == '\n') {
Expand All @@ -809,7 +809,7 @@ static char **read_source_file(char *file) {
return NULL;
struct stat st;
fstat(fileno(fp), &st);
char *buf = malloc(st.st_size + 1);
char *buf = calloc(1, st.st_size + 1);
if (fread(buf, 1, st.st_size, fp) != st.st_size)
return NULL;
fclose(fp);
Expand Down
2 changes: 1 addition & 1 deletion lex.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ static void mark() {
}

static Token *make_token(Token *tmpl) {
Token *r = malloc(sizeof(Token));
Token *r = calloc(1, sizeof(Token));
*r = *tmpl;
r->hideset = NULL;
File *f = current_file();
Expand Down
2 changes: 1 addition & 1 deletion map.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ static uint32_t hash(char *p) {
}

static Map *do_make_map(Map *parent, int size) {
Map *r = malloc(sizeof(Map));
Map *r = calloc(1, sizeof(Map));
r->parent = parent;
r->key = calloc(size, sizeof(char *));
r->val = calloc(size, sizeof(void *));
Expand Down
14 changes: 7 additions & 7 deletions parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ enum {

static void mark_location() {
Token *tok = peek();
source_loc = malloc(sizeof(SourceLoc));
source_loc = calloc(1, sizeof(SourceLoc));
source_loc->file = tok->file->name;
source_loc->line = tok->line;
}
Expand All @@ -141,7 +141,7 @@ static char *make_static_label(char *name) {
}

static Case *make_case(int beg, int end, char *label) {
Case *r = malloc(sizeof(Case));
Case *r = calloc(1, sizeof(Case));
r->beg = beg;
r->end = end;
r->label = label;
Expand All @@ -153,7 +153,7 @@ static Map *env() {
}

static Node *make_ast(Node *tmpl) {
Node *r = malloc(sizeof(Node));
Node *r = calloc(1, sizeof(Node));
*r = *tmpl;
r->sourceLoc = source_loc;
return r;
Expand Down Expand Up @@ -327,19 +327,19 @@ static Node *ast_label_addr(char *label) {
}

static Type *make_type(Type *tmpl) {
Type *r = malloc(sizeof(Type));
Type *r = calloc(1, sizeof(Type));
*r = *tmpl;
return r;
}

static Type *copy_type(Type *ty) {
Type *r = malloc(sizeof(Type));
Type *r = calloc(1, sizeof(Type));
memcpy(r, ty, sizeof(Type));
return r;
}

static Type *make_numtype(int kind, bool usig) {
Type *r = malloc(sizeof(Type));
Type *r = calloc(1, sizeof(Type));
r->kind = kind;
r->usig = usig;
if (kind == KIND_VOID) r->size = r->align = 0;
Expand Down Expand Up @@ -487,7 +487,7 @@ static bool next_token(int kind) {
}

void *make_pair(void *first, void *second) {
void **r = malloc(sizeof(void *) * 2);
void **r = calloc(2, sizeof(void *));
r[0] = first;
r[1] = second;
return r;
Expand Down
2 changes: 1 addition & 1 deletion set.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#include "8cc.h"

Set *set_add(Set *s, char *v) {
Set *r = malloc(sizeof(Set));
Set *r = calloc(1, sizeof(Set));
r->next = s;
r->v = v;
return r;
Expand Down
6 changes: 3 additions & 3 deletions vector.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ static int roundup(int n) {
}

static Vector *do_make_vector(int size) {
Vector *r = malloc(sizeof(Vector));
Vector *r = calloc(1, sizeof(Vector));
size = roundup(size);
if (size > 0)
r->body = malloc(sizeof(void *) * size);
r->body = calloc(size, sizeof(void *));
r->len = 0;
r->nalloc = size;
return r;
Expand All @@ -41,7 +41,7 @@ static void extend(Vector *vec, int delta) {
if (vec->len + delta <= vec->nalloc)
return;
int nelem = max(roundup(vec->len + delta), MIN_SIZE);
void *newbody = malloc(sizeof(void *) * nelem);
void *newbody = calloc(nelem, sizeof(void *));
memcpy(newbody, vec->body, sizeof(void *) * vec->len);
vec->body = newbody;
vec->nalloc = nelem;
Expand Down