[format_loader] catch invalid format names

Fixes #1122
This commit is contained in:
Tim Stack 2023-04-05 22:52:55 -07:00
parent a2f076a967
commit 44b564681b
7 changed files with 52 additions and 11 deletions

View File

@ -95,9 +95,10 @@ like so:
}
Each format to be defined in the file should be a separate field in the top-level
object. The field name should be the symbolic name of the format. This value
will also be used as the SQL table name for the log. The value for each field
should be another object with the following fields:
object. The field name should be the symbolic name of the format and consist
only of alphanumeric characters and underscores. This value will also be used
as the SQL table name for the log. The value for each field should be another
object with the following fields:
:title: The short and human-readable name for the format.
:description: A longer description of the format.

View File

@ -529,12 +529,11 @@ static const json_path_handler_base::enum_value_t SCALE_OP_ENUM[] = {
};
static const struct json_path_container scaling_factor_handlers = {
yajlpp::pattern_property_handler("op")
yajlpp::property_handler("op")
.with_enum_values(SCALE_OP_ENUM)
.for_field(&scaling_factor::sf_op),
yajlpp::pattern_property_handler("value").for_field(
&scaling_factor::sf_value),
yajlpp::property_handler("value").for_field(&scaling_factor::sf_value),
};
static const struct json_path_container scale_handlers = {

View File

@ -240,7 +240,21 @@ TEST_CASE("get_captures-namedq")
{
auto re = lnav::pcre2pp::code::from_const("(?'named'b)");
assert(re.get_captures().size() == 1);
assert(re.get_captures()[0].sf_begin == 0);
assert(re.get_captures()[0].sf_end == 11);
CHECK(re.get_captures().size() == 1);
CHECK(re.get_captures()[0].sf_begin == 0);
CHECK(re.get_captures()[0].sf_end == 11);
}
TEST_CASE("anchored")
{
auto re = lnav::pcre2pp::code::from_const(
"abc", PCRE2_ANCHORED | PCRE2_ENDANCHORED);
const auto sub1 = string_fragment::from_const("abc");
const auto sub2 = string_fragment::from_const("abcd");
const auto sub3 = string_fragment::from_const("0abc");
CHECK(re.find_in(sub1).ignore_error().has_value());
CHECK_FALSE(re.find_in(sub2).ignore_error().has_value());
CHECK_FALSE(re.find_in(sub3).ignore_error().has_value());
}

View File

@ -716,7 +716,8 @@ yajlpp_parse_context::update_callbacks(const json_path_container* orig_handlers,
if (jph.jph_regex->capture_from(path_frag)
.into(md)
.matches()
.ignore_error())
.ignore_error()
&& (md.remaining().empty() || md.remaining().startswith("/")))
{
auto cap = md[0].value();

View File

@ -1380,7 +1380,7 @@ template<typename T, std::size_t N>
inline json_path_handler
pattern_property_handler(const T (&path)[N])
{
return {lnav::pcre2pp::code::from_const(path).to_shared()};
return {lnav::pcre2pp::code::from_const(path, PCRE2_ANCHORED).to_shared()};
}
} // namespace yajlpp

View File

@ -0,0 +1,8 @@
{
"$schema": "https://lnav.org/schemas/format-v1.schema.json",
"bad-name-log": {
"title": "bad-format",
"description": "Log format with a name that has invalid characters",
"json": true
}
}

View File

@ -6,6 +6,24 @@
/bad_json_log/line-format#/timestamp-format <string>
Description
The strftime(3) format for this field
⚠ warning: unexpected value for property “/bad-name-log/title”
 --> {test_dir}/bad-config/formats/invalid-name/format.json:4
 |  "title": "bad-format", 
 = help: Available Properties
$schema The URI of the schema for this file
(\w+)/
⚠ warning: unexpected value for property “/bad-name-log/description”
 --> {test_dir}/bad-config/formats/invalid-name/format.json:5
 |  "description": "Log format with a name that has invalid characters",
 = help: Available Properties
$schema The URI of the schema for this file
(\w+)/
⚠ warning: unexpected value for property “/bad-name-log/json”
 --> {test_dir}/bad-config/formats/invalid-name/format.json:6
 |  "json": true 
 = help: Available Properties
$schema The URI of the schema for this file
(\w+)/
✘ error: “invalid(abc” is not a valid regular expression
reason: missing closing parenthesis
 --> /invalid_props_log/tags/badtag3/pattern