Coverage Report

Created: 2025-07-18 07:03

/src/testdir/tests/capi/luaL_addgsub_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
12
#include <fuzzer/FuzzedDataProvider.h>
13
14
#if defined(__cplusplus)
15
extern "C" {
16
#endif /* defined(__cplusplus) */
17
18
#include "lua.h"
19
#include "lualib.h"
20
#include "lauxlib.h"
21
22
#if defined(__cplusplus)
23
} /* extern "C" */
24
#endif /* defined(__cplusplus) */
25
26
extern "C" int
27
LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
28
758
{
29
758
  FuzzedDataProvider fdp(data, size);
30
31
758
  lua_State *L = luaL_newstate();
32
758
  if (L == NULL)
33
0
    return 0;
34
35
758
  luaL_Buffer buf;
36
758
  auto str = fdp.ConsumeRandomLengthString(size);
37
758
  luaL_buffinit(L, &buf);
38
758
  luaL_addstring(&buf, str.c_str());
39
40
758
  auto str1 = fdp.ConsumeRandomLengthString(size);
41
758
  auto str2 = fdp.ConsumeRandomLengthString(size);
42
758
  auto str3 = fdp.ConsumeRandomLengthString(size);
43
758
  const char *c_str1 = str1.c_str();
44
758
  const char *c_str2 = str2.c_str();
45
758
  const char *c_str3 = str3.c_str();
46
758
  if (strlen(c_str1) == 0 ||
47
758
      strlen(c_str2) == 0 ||
48
758
      strlen(c_str3) == 0) {
49
199
    luaL_pushresult(&buf);
50
199
    lua_settop(L, 0);
51
199
    lua_close(L);
52
199
    return -1;
53
199
  }
54
559
  luaL_addgsub(&buf, c_str1, c_str2, c_str3);
55
559
  luaL_pushresult(&buf);
56
57
559
  lua_settop(L, 0);
58
559
  lua_close(L);
59
60
559
  return 0;
61
758
}