Skip to content

Commit 32feed3

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 e39cef7 commit 32feed3

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

luzer/init.lua

Lines changed: 5 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))
@@ -58,4 +58,8 @@ return {
5858

5959
_set_custom_mutator = luzer_impl._set_custom_mutator,
6060
_mutate = luzer_impl._mutate,
61+
62+
internal = {
63+
parse_flag = parse_flag,
64+
}
6165
}

luzer/tests/test_unit.lua

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

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

0 commit comments

Comments
 (0)