Skip to content

Commit 3289d77

Browse files
authored
Fix buffer overflow error (#4)
1 parent 8055fce commit 3289d77

File tree

3 files changed

+9
-9
lines changed

3 files changed

+9
-9
lines changed

Diff for: aa.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ struct ASTNode *TreeCreate(struct Typetable *type, int nodetype, char *name, uni
66
temp->nodetype = nodetype;
77
if (name != NULL)
88
{
9-
temp->name = (char *)malloc(sizeof(name));
9+
temp->name = (char *)malloc(strlen(name) * sizeof(char));
1010
strcpy(temp->name, name);
1111
}
1212
else
@@ -26,4 +26,4 @@ struct ASTNode *TreeCreate(struct Typetable *type, int nodetype, char *name, uni
2626
temp->ptr2 = ptr2;
2727
temp->ptr3 = ptr3;
2828
return temp;
29-
}
29+
}

Diff for: abstree.l

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
6666
"alloc" {return ALLOC;}
6767
"free" {return FREE;}
6868
[a-zA-Z][a-zA-Z0-9]* {
69-
variable = malloc(sizeof(yytext));
69+
variable = malloc(yyleng * sizeof(char));
7070
strcpy(variable, yytext);
7171
temp = TLookup("integer"); // default type for any word (variable or string) is int
7272
yylval.nptr = TreeCreate(temp, NODE_ID, variable, &yytext, NULL, NULL, NULL, NULL);

Diff for: symboltable.c

+6-6
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ void GInstall(char *name, struct Typetable *type, int size, struct Paramstruct *
2323
}
2424

2525
temp = (struct Gsymbol *)malloc(sizeof(struct Gsymbol));
26-
temp->name = (char *)malloc(sizeof(name));
26+
temp->name = (char *)malloc(strlen(name) * sizeof(char));
2727
strcpy(temp->name, name);
2828
temp->type = type;
2929
temp->size = size;
@@ -67,7 +67,7 @@ void LInstall(char *name, struct Typetable *type)
6767
{
6868
struct Lsymbol *temp;
6969
temp = (struct Lsymbol *)malloc(sizeof(struct Lsymbol));
70-
temp->name = (char *)malloc(sizeof(name));
70+
temp->name = (char *)malloc(strlen(name) * sizeof(char));
7171
strcpy(temp->name, name);
7272
temp->type = type;
7373
temp->next = NULL;
@@ -100,7 +100,7 @@ void PInstall(char *name, struct Typetable *type)
100100
{
101101
struct Paramstruct *temp;
102102
temp = (struct Paramstruct *)malloc(sizeof(struct Paramstruct));
103-
temp->name = (char *)malloc(sizeof(name));
103+
temp->name = (char *)malloc(strlen(name) * sizeof(char));
104104
strcpy(temp->name, name);
105105
temp->type = type;
106106
temp->next = NULL;
@@ -135,7 +135,7 @@ void TInstall(char *name, int size, struct Fieldlist *fields)
135135
int counter = 0;
136136

137137
temp = (struct Typetable *)malloc(sizeof(struct Typetable));
138-
temp->name = (char *)malloc(sizeof(name));
138+
temp->name = (char *)malloc(strlen(name) * sizeof(char));
139139
strcpy(temp->name, name);
140140
temp->next = NULL;
141141

@@ -183,7 +183,7 @@ void FInstall(struct Typetable *type, char *name)
183183
struct Fieldlist *temp;
184184
struct Typetable *temp1;
185185
temp = (struct Fieldlist *)malloc(sizeof(struct Fieldlist));
186-
temp->name = (char *)malloc(sizeof(name));
186+
temp->name = (char *)malloc(strlen(name) * sizeof(char));
187187
strcpy(temp->name, name);
188188
temp->type = type;
189189
temp->next = NULL;
@@ -210,4 +210,4 @@ void printSymbolTable()
210210
printf("%s----%s-----%d\n", temp->name, temp->type->name, temp->binding);
211211
temp = temp->next;
212212
}
213-
}
213+
}

0 commit comments

Comments
 (0)