/src/testdir/tests/luaL_buffsub_test.cc
Line | Count | Source (jump to first uncovered line) |
1 | | #include <stdint.h> |
2 | | #include <stddef.h> |
3 | | #include <string.h> |
4 | | #include <stdlib.h> |
5 | | #include <assert.h> |
6 | | |
7 | | #include <fuzzer/FuzzedDataProvider.h> |
8 | | |
9 | | #if defined(__cplusplus) |
10 | | extern "C" { |
11 | | #endif /* defined(__cplusplus) */ |
12 | | |
13 | | #include <lua.h> |
14 | | #include <lualib.h> |
15 | | #include <lauxlib.h> |
16 | | |
17 | | #if defined(__cplusplus) |
18 | | } /* extern "C" */ |
19 | | #endif /* defined(__cplusplus) */ |
20 | | |
21 | | extern "C" int |
22 | | LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) |
23 | 496 | { |
24 | 496 | FuzzedDataProvider fdp(data, size); |
25 | | |
26 | 496 | lua_State *L = luaL_newstate(); |
27 | 496 | if (L == NULL) |
28 | 0 | return 0; |
29 | | |
30 | 496 | auto str = fdp.ConsumeRandomLengthString(size); |
31 | 496 | size_t buf_size = str.length() + 1; |
32 | 496 | uint8_t n = fdp.ConsumeIntegralInRange<uint8_t>(0, buf_size); |
33 | | |
34 | 496 | luaL_Buffer buf; |
35 | 496 | luaL_buffinit(L, &buf); |
36 | 496 | luaL_addlstring(&buf, str.c_str(), buf_size); |
37 | 496 | luaL_buffsub(&buf, n); |
38 | 496 | luaL_pushresult(&buf); |
39 | | |
40 | 496 | assert(luaL_bufflen(&buf) == buf_size - n); |
41 | 0 | lua_settop(L, 0); |
42 | 496 | lua_close(L); |
43 | | |
44 | 496 | return 0; |
45 | 496 | } |