/src/testdir/tests/lua_dump_test.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * SPDX-License-Identifier: ISC |
3 | | * |
4 | | * Copyright 2023, Sergey Bronnikov. |
5 | | */ |
6 | | |
7 | | #include <assert.h> |
8 | | #include <stdint.h> |
9 | | |
10 | | #include <lua.h> |
11 | | #include <lauxlib.h> |
12 | | |
13 | | static int |
14 | | Writer(struct lua_State *L, const void *p, size_t size, void *ud) |
15 | 0 | { |
16 | | /** |
17 | | * We are not interested in produced parts of the binary chunk. |
18 | | * Test does not execute a binary chunk because it is focused |
19 | | * on a Lua runtime frontend. Thereby, high speed of fuzzing is achieved. |
20 | | */ |
21 | |
|
22 | 0 | (void)L; |
23 | 0 | (void)p; |
24 | 0 | (void)ud; |
25 | | |
26 | | /** |
27 | | * The writer returns an error code: 0 means no errors; any other value |
28 | | * means an error and stops lua_dump from calling the writer again. |
29 | | */ |
30 | 0 | return 0; |
31 | 0 | } |
32 | | |
33 | | int |
34 | | LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) |
35 | 234 | { |
36 | 234 | lua_State *L = luaL_newstate(); |
37 | 234 | assert(L != NULL); |
38 | | |
39 | 234 | lua_pushlstring(L, (const char *)data, size); |
40 | | #if LUA_VERSION_NUM < 503 |
41 | | lua_dump(L, Writer, NULL); |
42 | | #else /* Lua 5.3+ */ |
43 | 234 | lua_dump(L, Writer, NULL, 0); |
44 | 234 | #endif /* LUA_VERSION_NUM */ |
45 | | |
46 | 234 | lua_settop(L, 0); |
47 | 234 | lua_close(L); |
48 | | |
49 | 234 | return 0; |
50 | 234 | } |