Skip to content

Commit e332a02

Browse files
committed
Add assets for functions not checking for NULLs
1 parent d739cd5 commit e332a02

File tree

6 files changed

+49
-0
lines changed

6 files changed

+49
-0
lines changed

src/commands.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ static const char* help_matches[] = {"help", "?", NULL};
1212
static const char* debug_info_matches[] = {"info", NULL};
1313

1414
static void handle_quit(struct LacoState* laco, const char** arguments) {
15+
assert(laco != NULL);
16+
1517
laco_kill(laco, 0, "Exiting laco...");
1618
}
1719

@@ -26,6 +28,9 @@ static void handle_help(struct LacoState* laco, const char** arguments) {
2628

2729
static void handle_debug_info(struct LacoState* laco,
2830
const char** arguments) {
31+
assert(laco != NULL);
32+
assert(arguments != NULL);
33+
2934
laco_print_debug_info(laco, arguments[0]);
3035
}
3136

src/commands/debugger.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
#include "util.h"
1111

1212
void laco_print_debug_info(LacoState* laco, const char* function_name) {
13+
assert(laco != NULL);
14+
assert(function_name != NULL);
15+
1316
int i;
1417
char* namespace;
1518
lua_State* L = laco_get_laco_lua_state(laco);

src/flags.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ static const char* help_matches[] = {"-h", "--help", NULL};
1010

1111
/* Print off the current version of laco */
1212
static void handle_version(LacoState* laco, const char** arguments) {
13+
assert(laco != NULL);
14+
1315
const char* version = laco_get_laco_version(laco);
1416

1517
printf("laco version %s\n", version);
@@ -18,6 +20,8 @@ static void handle_version(LacoState* laco, const char** arguments) {
1820

1921
/* Print off the help screen */
2022
static void handle_help(LacoState* laco, const char** arguments) {
23+
assert(laco != NULL);
24+
2125
puts(
2226
"A better REPL for Lua.\n\n"
2327
"Usage: laco [options]\n\n"
@@ -37,6 +41,8 @@ static const LacoCommand flag_commands[] = {
3741
/* External API */
3842

3943
void laco_handle_flag(LacoState* laco) {
44+
assert(laco != NULL);
45+
4046
const char* command = laco_get_laco_args(laco)[1];
4147

4248
laco_dispatch(flag_commands, laco, command, NULL);

src/util.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "laco.h"
88

99
static inline void ignore_extra(const char chr, char** string_ptr) {
10+
assert(string_ptr != NULL);
1011
if(*string_ptr == NULL) return;
1112

1213
while(**string_ptr == chr) {
@@ -17,6 +18,8 @@ static inline void ignore_extra(const char chr, char** string_ptr) {
1718
/* External API */
1819

1920
void laco_kill(LacoState* laco, int status, const char* message) {
21+
assert(laco != NULL);
22+
2023
laco_destroy_laco_state(laco);
2124

2225
if(message != NULL) {
@@ -27,6 +30,9 @@ void laco_kill(LacoState* laco, int status, const char* message) {
2730
}
2831

2932
bool laco_is_match(const char** matches, const char* test_string) {
33+
assert(matches != NULL);
34+
assert(test_string != NULL);
35+
3036
int i;
3137
const char* match;
3238

@@ -61,6 +67,10 @@ char** laco_split_by(const char split_with, char* string,
6167

6268
void laco_dispatch(const LacoCommand* commands, LacoState* laco,
6369
const char* command_keyword, const char** arguments) {
70+
assert(commands != NULL);
71+
assert(laco != NULL);
72+
assert(command_keyword != NULL);
73+
6474
int i;
6575
const char** matches;
6676

src/util/line.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
/* Check if line is incomplete */
1515
static bool incomplete(lua_State* L, int status) {
16+
assert(L != NULL);
17+
1618
size_t lmess;
1719
size_t eof_size;
1820
const char* mess;
@@ -38,6 +40,8 @@ static bool incomplete(lua_State* L, int status) {
3840

3941
/* Check if line can be printed */
4042
static bool is_printable(lua_State* L, int status) {
43+
assert(L != NULL);
44+
4145
const char* mess;
4246
const char* literal;
4347
const char* func;
@@ -82,6 +86,9 @@ static bool is_printable(lua_State* L, int status) {
8286
}
8387

8488
static char* get_line(LacoState* laco, const char* prompt) {
89+
assert(laco != NULL);
90+
assert(prompt != NULL);
91+
8592
char* line = linenoise(prompt);
8693

8794
if(line != NULL) {
@@ -97,6 +104,8 @@ static char* get_line(LacoState* laco, const char* prompt) {
97104

98105
/* Push a line to the stack and store in history */
99106
static bool pushline(LacoState* laco, bool isFirstLine) {
107+
assert(laco != NULL);
108+
100109
const char* prompt = (isFirstLine) ? "> " : "... ";
101110
char* line = get_line(laco, prompt);
102111
lua_State* L = laco_get_laco_lua_state(laco);
@@ -115,6 +124,8 @@ static bool pushline(LacoState* laco, bool isFirstLine) {
115124
/* External API */
116125

117126
bool laco_load_line(LacoState* laco) {
127+
assert(laco != NULL);
128+
118129
int status = laco_get_laco_status(laco);
119130
lua_State* L = laco_get_laco_lua_state(laco);
120131

@@ -145,6 +156,8 @@ bool laco_load_line(LacoState* laco) {
145156
}
146157

147158
void laco_handle_line(LacoState* laco) {
159+
assert(laco != NULL);
160+
148161
int status = laco_get_laco_status(laco);
149162
lua_State* L = laco_get_laco_lua_state(laco);
150163

src/util/print.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
/* C Wrapper for lua's tostring function */
1010
inline static const char* to_lua_string(lua_State* L) {
11+
assert(L != NULL);
12+
1113
const char* result = NULL;
1214

1315
lua_getglobal(L, "tostring");
@@ -24,13 +26,17 @@ inline static const char* to_lua_string(lua_State* L) {
2426

2527
/* Format printing of a single key value pair */
2628
static void print_key_value(lua_State* L, char start_char) {
29+
assert(L != NULL);
30+
2731
printf("%c %s = %s\n",
2832
start_char, lua_tostring(L, -2),
2933
to_lua_string(L));
3034
}
3135

3236
/* Format printing of a single list element */
3337
static void print_list(lua_State* L, char start_char) {
38+
assert(L != NULL);
39+
3440
const char* format = NULL;
3541
if(start_char == '{') {
3642
format = "%c%s";
@@ -43,6 +49,8 @@ static void print_list(lua_State* L, char start_char) {
4349

4450
/* Print both types of tables */
4551
static void print_table(lua_State* L) {
52+
assert(L != NULL);
53+
4654
bool first_elem = true;
4755
int luatype;
4856

@@ -80,6 +88,8 @@ static void print_table(lua_State* L) {
8088
/* External API */
8189

8290
int laco_print_type(LacoState* laco) {
91+
assert(laco != NULL);
92+
8393
lua_State* L = laco_get_laco_lua_state(laco);
8494
int status = laco_get_laco_status(laco);
8595
int luatype = lua_type(L, -1);
@@ -99,6 +109,8 @@ int laco_print_type(LacoState* laco) {
99109
}
100110

101111
void laco_report_error(LacoState* laco, int status) {
112+
assert(laco != NULL);
113+
102114
lua_State* L = laco_get_laco_lua_state(laco);
103115

104116
if(status != 0 && lua_isstring(L, -1)) {

0 commit comments

Comments
 (0)