Coverage Report

Created: 2024-04-23 06:32

/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
752
{
29
752
  FuzzedDataProvider fdp(data, size);
30
31
752
  lua_State *L = luaL_newstate();
32
752
  if (L == NULL)
33
0
    return 0;
34
35
752
  luaL_Buffer buf;
36
752
  auto str = fdp.ConsumeRandomLengthString(size);
37
752
  luaL_buffinit(L, &buf);
38
752
  luaL_addstring(&buf, str.c_str());
39
40
752
  auto str1 = fdp.ConsumeRandomLengthString(size);
41
752
  auto str2 = fdp.ConsumeRandomLengthString(size);
42
752
  auto str3 = fdp.ConsumeRandomLengthString(size);
43
752
  const char *c_str1 = str1.c_str();
44
752
  const char *c_str2 = str2.c_str();
45
752
  const char *c_str3 = str3.c_str();
46
752
  if (strlen(c_str1) == 0 ||
47
752
      strlen(c_str2) == 0 ||
48
752
      strlen(c_str3) == 0) {
49
217
    luaL_pushresult(&buf);
50
217
    lua_settop(L, 0);
51
217
    lua_close(L);
52
217
    return -1;
53
217
  }
54
535
  luaL_addgsub(&buf, c_str1, c_str2, c_str3);
55
535
  luaL_pushresult(&buf);
56
57
535
  lua_settop(L, 0);
58
535
  lua_close(L);
59
60
535
  return 0;
61
752
}