/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 | 500 | { |
24 | 500 | FuzzedDataProvider fdp(data, size); |
25 | | |
26 | 500 | lua_State *L = luaL_newstate(); |
27 | 500 | if (L == NULL) |
28 | 0 | return 0; |
29 | | |
30 | 500 | auto str = fdp.ConsumeRandomLengthString(size); |
31 | 500 | size_t buf_size = str.length() + 1; |
32 | 500 | uint8_t n = fdp.ConsumeIntegralInRange<uint8_t>(0, buf_size); |
33 | | |
34 | 500 | luaL_Buffer buf; |
35 | 500 | luaL_buffinit(L, &buf); |
36 | 500 | luaL_addlstring(&buf, str.c_str(), buf_size); |
37 | 500 | luaL_buffsub(&buf, n); |
38 | 500 | luaL_pushresult(&buf); |
39 | | |
40 | 500 | assert(luaL_bufflen(&buf) == buf_size - n); |
41 | 0 | lua_settop(L, 0); |
42 | 500 | lua_close(L); |
43 | | |
44 | 500 | return 0; |
45 | 500 | } |