Coverage Report

Created: 2024-04-23 06:32

/src/testdir/tests/capi/luaL_gsub_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 <assert.h>
8
#include <stdint.h>
9
#include <stddef.h>
10
#include <string.h>
11
#include <stdlib.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
716
{
30
716
  FuzzedDataProvider fdp(data, size);
31
32
716
  lua_State *L = luaL_newstate();
33
716
  if (L == NULL)
34
0
    return 0;
35
36
716
  auto str1 = fdp.ConsumeRandomLengthString(size);
37
716
  auto str2 = fdp.ConsumeRandomLengthString(size);
38
716
  auto str3 = fdp.ConsumeRandomLengthString(size);
39
716
  const char *c_str1 = str1.c_str();
40
716
  const char *c_str2 = str2.c_str();
41
716
  const char *c_str3 = str3.c_str();
42
716
  if (strlen(c_str1) == 0 ||
43
716
      strlen(c_str2) == 0 ||
44
716
      strlen(c_str3) == 0) {
45
155
    lua_settop(L, 0);
46
155
    lua_close(L);
47
155
    return -1;
48
155
  }
49
561
  int top = lua_gettop(L);
50
561
  luaL_gsub(L, c_str1, c_str2, c_str3);
51
  /* [-0, +1, m] */
52
561
  assert(lua_gettop(L) == top + 1);
53
54
0
  lua_settop(L, 0);
55
561
  lua_close(L);
56
57
561
  return 0;
58
716
}