|
| 1 | +--[[ |
| 2 | +SPDX-License-Identifier: ISC |
| 3 | +Copyright (c) 2023-2025, Sergey Bronnikov. |
| 4 | +
|
| 5 | +5.3 – Modules |
| 6 | +https://www.lua.org/manual/5.1/manual.html#5.3 |
| 7 | +
|
| 8 | +PIL: 15 – Packages |
| 9 | +https://www.lua.org/pil/15.html |
| 10 | +
|
| 11 | +Synopsis: require(modname) |
| 12 | +]] |
| 13 | + |
| 14 | +local luzer = require("luzer") |
| 15 | +local test_lib = require("lib") |
| 16 | + |
| 17 | +local MAX_STR_LEN = test_lib.MAX_STR_LEN |
| 18 | +local MAX_PATH_NUM = 10 |
| 19 | + |
| 20 | +local function build_path(fdp) |
| 21 | + local count = fdp:consume_integer(0, MAX_PATH_NUM) |
| 22 | + local paths = fdp:consume_strings(MAX_STR_LEN, count) |
| 23 | + local path_str = table.concat(paths, ";") |
| 24 | + local enable_def_path = fdp:consume_boolean() |
| 25 | + return enable_def_path and path_str or path_str .. ";;" |
| 26 | +end |
| 27 | + |
| 28 | +local function TestOneInput(buf) |
| 29 | + -- Save paths used by `require` to search for a Lua loader. |
| 30 | + local old_path = package.path |
| 31 | + local old_cpath = package.cpath |
| 32 | + |
| 33 | + local fdp = luzer.FuzzedDataProvider(buf) |
| 34 | + local path = build_path(fdp) |
| 35 | + package.path = path |
| 36 | + local cpath = build_path(fdp) |
| 37 | + package.cpath = cpath |
| 38 | + |
| 39 | + local module_name = fdp:consume_string(MAX_STR_LEN) |
| 40 | + -- If there is any error loading or running the module, or if |
| 41 | + -- it cannot find any loader for the module, then require |
| 42 | + -- signals an error, |
| 43 | + -- https://www.lua.org/manual/5.1/manual.html#pdf-require. |
| 44 | + pcall(require, module_name) |
| 45 | + |
| 46 | + -- Teardown. |
| 47 | + package.path = old_path |
| 48 | + package.cpath = old_cpath |
| 49 | +end |
| 50 | + |
| 51 | +local args = { |
| 52 | + artifact_prefix = "package_require_", |
| 53 | +} |
| 54 | +luzer.Fuzz(TestOneInput, nil, args) |
0 commit comments