Skip to content

Commit 90fa91f

Browse files
committed
Fixes
Signed-off-by: Juan Cruz Viotti <jv@jviotti.com>
1 parent eea80d5 commit 90fa91f

File tree

4 files changed

+71
-0
lines changed

4 files changed

+71
-0
lines changed

src/command_compile.cc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include <sourcemeta/core/io.h>
22
#include <sourcemeta/core/json.h>
33
#include <sourcemeta/core/jsonschema.h>
4+
#include <sourcemeta/core/regex.h>
45
#include <sourcemeta/core/yaml.h>
56

67
#include <sourcemeta/blaze/compiler.h>
@@ -69,6 +70,14 @@ auto sourcemeta::jsonschema::compile(const sourcemeta::core::Options &options)
6970

7071
if (options.contains("include") && !options.at("include").empty()) {
7172
std::string name{options.at("include").front()};
73+
74+
static const auto IDENTIFIER_PATTERN{
75+
sourcemeta::core::to_regex("^[A-Za-z_][A-Za-z0-9_]*$")};
76+
if (!IDENTIFIER_PATTERN.has_value() ||
77+
!sourcemeta::core::matches(IDENTIFIER_PATTERN.value(), name)) {
78+
throw InvalidIncludeIdentifier{name};
79+
}
80+
7281
std::transform(name.begin(), name.end(), name.begin(),
7382
[](unsigned char character) -> unsigned char {
7483
return static_cast<unsigned char>(std::toupper(character));

src/error.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,21 @@ class InvalidLintRuleError : public std::runtime_error {
8181
std::string rule_;
8282
};
8383

84+
class InvalidIncludeIdentifier : public std::runtime_error {
85+
public:
86+
InvalidIncludeIdentifier(std::string identifier)
87+
: std::runtime_error{"The include identifier is not a valid C/C++ "
88+
"identifier"},
89+
identifier_{std::move(identifier)} {}
90+
91+
[[nodiscard]] auto identifier() const noexcept -> const std::string & {
92+
return this->identifier_;
93+
}
94+
95+
private:
96+
std::string identifier_;
97+
};
98+
8499
class LintAutoFixError : public std::runtime_error {
85100
public:
86101
LintAutoFixError(std::string message, std::filesystem::path path,
@@ -316,6 +331,10 @@ inline auto try_catch(const sourcemeta::core::Options &options,
316331
const auto is_json{options.contains("json")};
317332
print_exception(is_json, error);
318333
return EXIT_FAILURE;
334+
} catch (const InvalidIncludeIdentifier &error) {
335+
const auto is_json{options.contains("json")};
336+
print_exception(is_json, error);
337+
return EXIT_FAILURE;
319338
} catch (const LintAutoFixError &error) {
320339
const auto is_json{options.contains("json")};
321340
print_exception(is_json, error);

test/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,7 @@ add_jsonschema_test_unix(compile/pass_custom_extension_yaml)
362362
add_jsonschema_test_unix(compile/pass_include)
363363
add_jsonschema_test_unix(compile/pass_include_lowercase)
364364
add_jsonschema_test_unix(compile/pass_include_short)
365+
add_jsonschema_test_unix(compile/fail_include_invalid_identifier)
365366

366367
# Canonicalize
367368
add_jsonschema_test_unix(canonicalize/pass_input_unmodified)
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#!/bin/sh
2+
3+
set -o errexit
4+
set -o nounset
5+
6+
TMP="$(mktemp -d)"
7+
clean() { rm -rf "$TMP"; }
8+
trap clean EXIT
9+
10+
cat << 'EOF' > "$TMP/schema.json"
11+
{
12+
"$id": "https://example.com",
13+
"$schema": "https://json-schema.org/draft/2020-12/schema",
14+
"type": "string"
15+
}
16+
EOF
17+
18+
# Test with hyphen (invalid)
19+
"$1" compile "$TMP/schema.json" --include my-schema 2>"$TMP/stderr.txt" \
20+
&& CODE="$?" || CODE="$?"
21+
test "$CODE" = "1" || exit 1
22+
23+
cat << 'EOF' > "$TMP/expected.txt"
24+
error: The include identifier is not a valid C/C++ identifier
25+
at identifier my-schema
26+
EOF
27+
28+
diff "$TMP/stderr.txt" "$TMP/expected.txt"
29+
30+
# JSON error
31+
"$1" compile "$TMP/schema.json" --include my-schema --json > "$TMP/stdout.txt" 2>&1 \
32+
&& CODE="$?" || CODE="$?"
33+
test "$CODE" = "1" || exit 1
34+
35+
cat << 'EOF' > "$TMP/expected.txt"
36+
{
37+
"error": "The include identifier is not a valid C/C++ identifier",
38+
"identifier": "my-schema"
39+
}
40+
EOF
41+
42+
diff "$TMP/stdout.txt" "$TMP/expected.txt"

0 commit comments

Comments
 (0)