Skip to content

Commit 77240bd

Browse files
committed
luzer: fix argv building
1 parent f76dff6 commit 77240bd

File tree

2 files changed

+19
-20
lines changed

2 files changed

+19
-20
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,4 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2626

2727
- Fix searching Clang RT.
2828
- Stack overflow due to recursive traceback calls.
29+
- Fix a crash due to incorrect `argv` building.

luzer/luzer.c

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -329,36 +329,34 @@ luaL_fuzz(lua_State *L)
329329
char **argv = malloc(1 * sizeof(char*));
330330
if (!argv)
331331
luaL_error(L, "not enough memory");
332+
argv[0] = "<test name>";
333+
argc++;
332334
const char *corpus_path = NULL;
333335
while (lua_next(L, -2) != 0) {
336+
const char *key = lua_tostring(L, -2);
337+
const char *value = lua_tostring(L, -1);
338+
if (strcmp(key, "corpus") == 0) {
339+
corpus_path = strdup(value);
340+
lua_pop(L, 1);
341+
continue;
342+
}
334343
char **argvp = realloc(argv, sizeof(char*) * (argc + 1));
335344
if (argvp == NULL) {
336345
free(argv);
337346
luaL_error(L, "not enough memory");
338347
}
339-
const char *key = lua_tostring(L, -2);
340-
const char *value = lua_tostring(L, -1);
341-
if (strcmp(key, "corpus") != 0) {
342-
size_t arg_len = strlen(key) + strlen(value) + 3;
343-
char *arg = calloc(arg_len, sizeof(char));
344-
if (!arg)
345-
luaL_error(L, "not enough memory");
346-
snprintf(arg, arg_len, "-%s=%s", key, value);
347-
argvp[argc] = arg;
348-
argc++;
349-
} else {
350-
corpus_path = strdup(value);
351-
}
352-
lua_pop(L, 1);
348+
size_t arg_len = strlen(key) + strlen(value) + 3;
349+
char *arg = calloc(arg_len, sizeof(char));
350+
if (!arg)
351+
luaL_error(L, "not enough memory");
352+
snprintf(arg, arg_len, "-%s=%s", key, value);
353+
argvp[argc] = arg;
354+
argc++;
353355
argv = argvp;
356+
lua_pop(L, 1);
354357
}
355358
if (corpus_path) {
356-
argv[argc] = (char*)corpus_path;
357-
argc++;
358-
}
359-
if (argc == 0) {
360-
argv[argc] = "";
361-
argc++;
359+
argv[argc-1] = (char*)corpus_path;
362360
}
363361
argv[argc] = NULL;
364362
lua_pop(L, 1);

0 commit comments

Comments
 (0)