Skip to content

Commit 78d1e1e

Browse files
committed
Merge branch 'assert-null'
Checking for NULL pointers all the time can be tedious and slows down many functions. Using asserts for NULL check doesn't slow these functions in release mode because `assert` is a noopt.
2 parents 353726f + 7bc9852 commit 78d1e1e

File tree

8 files changed

+68
-18
lines changed

8 files changed

+68
-18
lines changed

src/commands.c

Lines changed: 18 additions & 12 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

@@ -42,20 +47,21 @@ static const LacoCommand line_commands[] = {
4247
/* External API */
4348

4449
void laco_handle_command(struct LacoState* laco, char* line) {
45-
if(laco != NULL && line != NULL) {
46-
char* command_line = strdup(line + 1);
47-
char** command_words = laco_line_to_words(command_line);
50+
assert(laco != NULL);
51+
assert(line != NULL);
52+
53+
char* command_line = strdup(line + 1);
54+
char** command_words = laco_line_to_words(command_line);
4855

49-
/* Alias for parsed out words within the line */
50-
const char* command = command_words[0];
51-
const char** arguments = (const char**) command_words + 1;
56+
/* Alias for parsed out words within the line */
57+
const char* command = command_words[0];
58+
const char** arguments = (const char**) command_words + 1;
5259

53-
laco_dispatch(line_commands, laco, command, arguments);
60+
laco_dispatch(line_commands, laco, command, arguments);
5461

55-
free(command_line);
56-
free(command_words);
62+
free(command_line);
63+
free(command_words);
5764

58-
/* Make it seems like this was an empty line */
59-
line[0] = '\0';
60-
}
65+
/* Make it seems like this was an empty line */
66+
line[0] = '\0';
6167
}

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/laco.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ int laco_get_laco_status(LacoState* laco) {
7676
}
7777

7878
void laco_set_laco_status(LacoState* laco, int status) {
79-
if(laco != NULL) {
80-
laco->status = status;
81-
}
79+
assert(laco != NULL);
80+
81+
laco->status = status;
8282
}

src/util.c

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,18 @@
77
#include "laco.h"
88

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

12-
while(**string_ptr == chr) {
12+
while(*string_ptr != NULL && **string_ptr == chr) {
1313
*string_ptr += 1;
1414
}
1515
}
1616

1717
/* External API */
1818

1919
void laco_kill(LacoState* laco, int status, const char* message) {
20+
assert(laco != NULL);
21+
2022
laco_destroy_laco_state(laco);
2123

2224
if(message != NULL) {
@@ -27,6 +29,9 @@ void laco_kill(LacoState* laco, int status, const char* message) {
2729
}
2830

2931
bool laco_is_match(const char** matches, const char* test_string) {
32+
assert(matches != NULL);
33+
assert(test_string != NULL);
34+
3035
int i;
3136
const char* match;
3237

@@ -41,7 +46,7 @@ bool laco_is_match(const char** matches, const char* test_string) {
4146

4247
char** laco_split_by(const char split_with, char* string,
4348
int ignore_repeats) {
44-
if(string == NULL) return NULL;
49+
assert(string != NULL);
4550

4651
char** result = calloc(16, sizeof(char*));
4752
size_t i = 0;
@@ -61,6 +66,10 @@ char** laco_split_by(const char split_with, char* string,
6166

6267
void laco_dispatch(const LacoCommand* commands, LacoState* laco,
6368
const char* command_keyword, const char** arguments) {
69+
assert(commands != NULL);
70+
assert(laco != NULL);
71+
assert(command_keyword != NULL);
72+
6473
int i;
6574
const char** matches;
6675

src/util.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#define LACO_UTIL_H
33

44
#include <stdbool.h>
5+
#include <assert.h>
56

67
struct LacoState;
78

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)