Coverage Report

Created: 2026-01-25 07:04

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