Skip to content

Commit ac580ef

Browse files
committed
luzer: fix parsing command-line flags
The patch fixes a problem with incorrect parsing command-line flags and adds a tests for the function `parse_flag()`. Fixes #23
1 parent c13b8b3 commit ac580ef

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,4 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2727
- Fix searching Clang RT.
2828
- Stack overflow due to recursive traceback calls.
2929
- Fix a crash due to incorrect `argv` building (#13).
30+
- Fix parsing command-line flags (#23).

luzer/init.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ end
1414
--
1515
-- 1. https://llvm.org/docs/LibFuzzer.html#options
1616
local function parse_flag(str)
17-
local flag_name, flag_val = string.match(str, "-([%l%p]+)=(%w+)")
17+
local flag_name, flag_val = string.match(str, "-([%w_]+)=(.+)")
1818
if not flag_name or
1919
not flag_val then
2020
error(("bad flag: %s"):format(str))
@@ -59,5 +59,6 @@ return {
5959

6060
set_custom_mutator = luzer_impl._set_custom_mutator,
6161
mutate = luzer_impl._mutate,
62+
parse_flag = parse_flag,
6263
}
6364
}

luzer/tests/test_unit.lua

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,4 +199,22 @@ assert(luzer_custom_mutator ~= nil)
199199
-- luzer._internal.mutate(buf, #buf, #buf, math.random(1, 10)) -- TODO
200200
luzer_custom_mutator = nil -- Clean up.
201201

202+
-- luzer._internal.parse_flag()
203+
local flag_testcases = {
204+
{ "dict", "/tmp/lua.dict" },
205+
{ "help", "1" },
206+
{ "rss_limit_mb", "2048" },
207+
{ "runs", "-1" },
208+
}
209+
for _, testcase in ipairs(flag_testcases) do
210+
local name = testcase[1]
211+
local val = testcase[2]
212+
-- libFuzzer flags are strictly in form `-flag=value`.
213+
local flag = ("-%s=%s"):format(name, val)
214+
-- Expected a table with `name` and `value`.
215+
res = { luzer._internal.parse_flag(flag) }
216+
assert(name == res[1], ("expected %s"):format(name))
217+
assert(val == res[2], ("expected %s"):format(val))
218+
end
219+
202220
print("Success!")

0 commit comments

Comments
 (0)