Skip to content

Commit a38778c

Browse files
committed
Replace calls to malloc with safe_malloc
1 parent c1165fb commit a38778c

File tree

4 files changed

+8
-6
lines changed

4 files changed

+8
-6
lines changed

src/ast.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
#include <stdlib.h>
22

33
#include "ast.h"
4+
#include "func.h"
45

56
struct Node *make_node(int type, struct Node *op1, struct Node *op2, int val)
67
{
7-
struct Node *node = malloc(sizeof(struct Node));
8+
struct Node *node = safe_malloc(sizeof(struct Node));
89
node->type = type;
910
node->val = val;
1011
node->op1 = op1;

src/lex.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ struct Token lex()
3838
default:
3939
// ID
4040
if (isalpha(ch)) {
41-
char *id_name = malloc(MAX_LEN);
41+
char *id_name = safe_malloc(MAX_LEN);
4242
int len = 0;
4343
id_name[len++] = ch;
4444
do {

src/parser.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ static void expect(int type)
4747

4848
static struct Node *factor()
4949
{
50-
struct Node* node = malloc(sizeof(struct Node));
50+
struct Node* node = safe_malloc(sizeof(struct Node));
5151
node->op1 = NULL;
5252
node->op2 = NULL;
5353

@@ -90,7 +90,7 @@ static struct Node *expr()
9090

9191
int tok_attr = tok.attr;
9292
if (accept_two(ID, EQ)) {
93-
node = malloc(sizeof(struct Node));
93+
node = safe_malloc(sizeof(struct Node));
9494
node->type = SET_TYPE;
9595
node->op1 = make_node(VAR_TYPE, 0, 0, tok_attr);
9696
node->op2 = expr();
@@ -110,7 +110,7 @@ static struct Node *expr()
110110

111111
struct Node *produce()
112112
{
113-
struct Node* node = malloc(sizeof(struct Node));
113+
struct Node* node = safe_malloc(sizeof(struct Node));
114114
node->op1 = expr();
115115
node->op2 = NULL;
116116

src/sym.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include <string.h>
33

44
#include "sym.h"
5+
#include "func.h"
56

67
struct Table* symbol_table[MAX_SYMBOL_TABLE_SIZE];
78
int table_size = 0;
@@ -19,7 +20,7 @@ int add_sym(char *name)
1920
return symbol_table[i]->id;
2021
}
2122

22-
struct Table *item = malloc(sizeof(struct Table));
23+
struct Table *item = safe_malloc(sizeof(struct Table));
2324
item->id = table_size;
2425
item->name = name;
2526

0 commit comments

Comments
 (0)