Coverage Report

Created: 2025-10-13 06:32

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/testdir/tests/capi/luaL_gsub_test.cc
Line
Count
Source
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
900
{
30
900
  FuzzedDataProvider fdp(data, size);
31
32
900
  lua_State *L = luaL_newstate();
33
900
  if (L == NULL)
34
0
    return 0;
35
36
900
  auto str1 = fdp.ConsumeRandomLengthString(size);
37
900
  auto str2 = fdp.ConsumeRandomLengthString(size);
38
900
  auto str3 = fdp.ConsumeRandomLengthString(size);
39
900
  const char *c_str1 = str1.c_str();
40
900
  const char *c_str2 = str2.c_str();
41
900
  const char *c_str3 = str3.c_str();
42
900
  if (strlen(c_str1) == 0 ||
43
778
      strlen(c_str2) == 0 ||
44
679
      strlen(c_str3) == 0) {
45
249
    lua_settop(L, 0);
46
249
    lua_close(L);
47
249
    return -1;
48
249
  }
49
651
  int top = lua_gettop(L);
50
651
  luaL_gsub(L, c_str1, c_str2, c_str3);
51
  /* [-0, +1, m] */
52
651
  assert(lua_gettop(L) == top + 1);
53
54
651
  lua_settop(L, 0);
55
651
  lua_close(L);
56
57
651
  return 0;
58
651
}