Coverage Report

Created: 2025-07-11 06:33

/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
751
{
30
751
  FuzzedDataProvider fdp(data, size);
31
32
751
  lua_State *L = luaL_newstate();
33
751
  if (L == NULL)
34
0
    return 0;
35
36
751
  auto str1 = fdp.ConsumeRandomLengthString(size);
37
751
  auto str2 = fdp.ConsumeRandomLengthString(size);
38
751
  auto str3 = fdp.ConsumeRandomLengthString(size);
39
751
  const char *c_str1 = str1.c_str();
40
751
  const char *c_str2 = str2.c_str();
41
751
  const char *c_str3 = str3.c_str();
42
751
  if (strlen(c_str1) == 0 ||
43
751
      strlen(c_str2) == 0 ||
44
751
      strlen(c_str3) == 0) {
45
143
    lua_settop(L, 0);
46
143
    lua_close(L);
47
143
    return -1;
48
143
  }
49
608
  int top = lua_gettop(L);
50
608
  luaL_gsub(L, c_str1, c_str2, c_str3);
51
  /* [-0, +1, m] */
52
608
  assert(lua_gettop(L) == top + 1);
53
54
608
  lua_settop(L, 0);
55
608
  lua_close(L);
56
57
608
  return 0;
58
608
}