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