Skip to content

Commit 082d049

Browse files
authored
Merge branch 'master' into clang-tidy-deprecated-arg
2 parents 712d33e + 30e7528 commit 082d049

File tree

10 files changed

+131
-33
lines changed

10 files changed

+131
-33
lines changed

BUILD.bazel

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,29 @@
11
licenses(["unencumbered"]) # Public Domain or MIT
22

3+
load("@bazel_skylib//rules:common_settings.bzl", "bool_flag")
4+
35
exports_files(["LICENSE"])
46

7+
bool_flag(
8+
name = "use_exception",
9+
build_setting_default = False,
10+
)
11+
12+
config_setting(
13+
name = "use_exception_cfg",
14+
flag_values = {":use_exception": "true"},
15+
)
16+
17+
bool_flag(
18+
name = "has_int64",
19+
build_setting_default = True,
20+
)
21+
22+
config_setting(
23+
name = "has_int64_cfg",
24+
flag_values = {":has_int64": "true"},
25+
)
26+
527
cc_library(
628
name = "jsoncpp",
729
srcs = [
@@ -22,10 +44,13 @@ cc_library(
2244
"include/json/version.h",
2345
"include/json/writer.h",
2446
],
25-
copts = [
26-
"-DJSON_USE_EXCEPTION=0",
27-
"-DJSON_HAS_INT64",
28-
],
47+
defines = select({
48+
":use_exception_cfg": ["JSON_USE_EXCEPTION=1"],
49+
"//conditions:default": ["JSON_USE_EXCEPTION=0"],
50+
}) + select({
51+
":has_int64_cfg": ["JSON_HAS_INT64"],
52+
"//conditions:default": [],
53+
}),
2954
includes = ["include"],
3055
visibility = ["//visibility:public"],
3156
deps = [":private"],

MODULE.bazel

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,8 @@ module(
1212
version = "1.9.7",
1313
compatibility_level = 1,
1414
)
15+
16+
bazel_dep(
17+
name = "bazel_skylib",
18+
version = "1.7.1",
19+
)

src/jsontestrunner/BUILD.bazel

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
cc_binary(
2+
name = "jsontestrunner",
3+
srcs = ["main.cpp"],
4+
deps = ["//:jsoncpp"],
5+
visibility = ["//test:__pkg__"],
6+
)

src/lib_json/json_reader.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <cstring>
1717
#include <iostream>
1818
#include <istream>
19+
#include <iterator>
1920
#include <limits>
2021
#include <memory>
2122
#include <set>
@@ -143,7 +144,12 @@ bool Reader::readValue() {
143144
// after calling readValue(). parse() executes one nodes_.push(), so > instead
144145
// of >=.
145146
if (nodes_.size() > stackLimit_g)
147+
#if JSON_USE_EXCEPTION
146148
throwRuntimeError("Exceeded stackLimit in readValue().");
149+
#else
150+
// throwRuntimeError aborts. Don't abort here.
151+
return false;
152+
#endif
147153

148154
Token token;
149155
readTokenSkippingComments(token);

src/test_lib_json/BUILD.bazel

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
cc_test(
2+
name = "jsoncpp_test",
3+
srcs = [
4+
"jsontest.cpp",
5+
"jsontest.h",
6+
"main.cpp",
7+
"fuzz.h",
8+
"fuzz.cpp",
9+
],
10+
deps = ["//:jsoncpp"],
11+
)

src/test_lib_json/fuzz.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,6 @@
1111
#include <memory>
1212
#include <string>
1313

14-
namespace Json {
15-
class Exception;
16-
}
17-
1814
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
1915
Json::CharReaderBuilder builder;
2016

@@ -45,10 +41,14 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
4541

4642
Json::Value root;
4743
const auto data_str = reinterpret_cast<const char*>(data);
44+
#if JSON_USE_EXCEPTION
4845
try {
46+
#endif // JSON_USE_EXCEPTION
4947
reader->parse(data_str, data_str + size, &root, nullptr);
48+
#if JSON_USE_EXCEPTION
5049
} catch (Json::Exception const&) {
5150
}
51+
#endif // JSON_USE_EXCEPTION
5252
// Whether it succeeded or not doesn't matter.
5353
return 0;
5454
}

src/test_lib_json/jsontest.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,8 @@ TestResult& checkStringEqual(TestResult& result, const Json::String& expected,
228228
JsonTest::ToJsonString(actual), __FILE__, \
229229
__LINE__, #expected " == " #actual)
230230

231+
#if JSON_USE_EXCEPTION
232+
231233
/// \brief Asserts that a given expression throws an exception
232234
#define JSONTEST_ASSERT_THROWS(expr) \
233235
do { \
@@ -242,6 +244,8 @@ TestResult& checkStringEqual(TestResult& result, const Json::String& expected,
242244
"expected exception thrown: " #expr); \
243245
} while (0)
244246

247+
#endif // JSON_USE_EXCEPTION
248+
245249
/// \brief Begin a fixture test case.
246250
#define JSONTEST_FIXTURE(FixtureType, name) \
247251
class Test##FixtureType##name : public FixtureType { \

src/test_lib_json/main.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1888,7 +1888,7 @@ JSONTEST_FIXTURE_LOCAL(ValueTest, typeChecksThrowExceptions) {
18881888
JSONTEST_ASSERT_THROWS(objVal.asBool());
18891889
JSONTEST_ASSERT_THROWS(arrVal.asBool());
18901890

1891-
#endif
1891+
#endif // JSON_USE_EXCEPTION
18921892
}
18931893

18941894
JSONTEST_FIXTURE_LOCAL(ValueTest, offsetAccessors) {
@@ -3323,6 +3323,8 @@ JSONTEST_FIXTURE_LOCAL(CharReaderTest, parseWithDetailError) {
33233323
}
33243324

33253325
JSONTEST_FIXTURE_LOCAL(CharReaderTest, parseWithStackLimit) {
3326+
#if JSON_USE_EXCEPTION
3327+
33263328
Json::CharReaderBuilder b;
33273329
Json::Value root;
33283330
char const doc[] = R"({ "property" : "value" })";
@@ -3342,6 +3344,8 @@ JSONTEST_FIXTURE_LOCAL(CharReaderTest, parseWithStackLimit) {
33423344
JSONTEST_ASSERT_THROWS(
33433345
reader->parse(doc, doc + std::strlen(doc), &root, &errs));
33443346
}
3347+
3348+
#endif // JSON_USE_EXCEPTION
33453349
}
33463350

33473351
JSONTEST_FIXTURE_LOCAL(CharReaderTest, testOperator) {
@@ -3961,6 +3965,8 @@ JSONTEST_FIXTURE_LOCAL(IteratorTest, indexes) {
39613965
}
39623966

39633967
JSONTEST_FIXTURE_LOCAL(IteratorTest, constness) {
3968+
#if JSON_USE_EXCEPTION
3969+
39643970
Json::Value const v;
39653971
JSONTEST_ASSERT_THROWS(
39663972
Json::Value::iterator it(v.begin())); // Compile, but throw.
@@ -3982,6 +3988,8 @@ JSONTEST_FIXTURE_LOCAL(IteratorTest, constness) {
39823988
}
39833989
Json::String expected = R"(" 9","10","11",)";
39843990
JSONTEST_ASSERT_STRING_EQUAL(expected, out.str());
3991+
3992+
#endif // JSON_USE_EXCEPTION
39853993
}
39863994

39873995
struct RValueTest : JsonTest::TestCase {};

test/BUILD.bazel

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
filegroup(
2+
name = "expected",
3+
srcs = glob(["data/**", "jsonchecker/**"], exclude=["**/*.json"]),
4+
)
5+
6+
[py_test(
7+
name = "runjson_%s_test" % "_".join(f.split("/")),
8+
srcs = ["runjsontests.py"],
9+
main = "runjsontests.py",
10+
args = [
11+
"--with-json-checker",
12+
"$(location //src/jsontestrunner:jsontestrunner)",
13+
"$(location :%s)" % f,
14+
],
15+
data = [
16+
"//src/jsontestrunner:jsontestrunner",
17+
":expected",
18+
":%s" % f,
19+
],
20+
) for f in glob(["**/*.json"])]

test/runjsontests.py

Lines changed: 37 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -66,38 +66,51 @@ class FailError(Exception):
6666
def __init__(self, msg):
6767
super(Exception, self).__init__(msg)
6868

69-
def runAllTests(jsontest_executable_path, input_dir = None,
69+
def runAllTests(jsontest_executable_path, input_path = None,
7070
use_valgrind=False, with_json_checker=False,
7171
writerClass='StyledWriter'):
72-
if not input_dir:
73-
input_dir = os.path.join(os.getcwd(), 'data')
74-
tests = glob(os.path.join(input_dir, '*.json'))
75-
if with_json_checker:
76-
all_tests = glob(os.path.join(input_dir, '../jsonchecker', '*.json'))
77-
# These tests fail with strict json support, but pass with JsonCPP's
78-
# extra leniency features. When adding a new exclusion to this list,
79-
# remember to add the test's number and reasoning here:
80-
known = ["fail{}.json".format(n) for n in [
81-
4, 9, # fail because we allow trailing commas
82-
7, # fails because we allow commas after close
83-
8, # fails because we allow extra close
84-
10, # fails because we allow extra values after close
85-
13, # fails because we allow leading zeroes in numbers
86-
18, # fails because we allow deeply nested values
87-
25, # fails because we allow tab characters in strings
88-
27, # fails because we allow string line breaks
89-
]]
90-
test_jsonchecker = [ test for test in all_tests
91-
if os.path.basename(test) not in known]
72+
if not input_path:
73+
input_path = os.path.join(os.getcwd(), 'data')
9274

75+
if os.path.isdir(input_path):
76+
tests = [
77+
os.path.normpath(os.path.abspath(test))
78+
for test in glob(os.path.join(input_path, '*.json'))
79+
]
80+
81+
if with_json_checker:
82+
tests += [
83+
os.path.normpath(os.path.abspath(test))
84+
for test in glob(os.path.join(input_path, '../jsonchecker', '*.json'))
85+
]
9386
else:
94-
test_jsonchecker = []
87+
tests = [input_path]
88+
89+
# These tests fail with strict json support, but pass with JsonCPP's
90+
# extra leniency features. When adding a new exclusion to this list,
91+
# remember to add the test's number and reasoning here:
92+
known = ["fail{}.json".format(n) for n in [
93+
4, 9, # fail because we allow trailing commas
94+
7, # fails because we allow commas after close
95+
8, # fails because we allow extra close
96+
10, # fails because we allow extra values after close
97+
13, # fails because we allow leading zeroes in numbers
98+
18, # fails because we allow deeply nested values
99+
25, # fails because we allow tab characters in strings
100+
27, # fails because we allow string line breaks
101+
]]
102+
103+
tests = [
104+
test for test in tests
105+
if os.path.basename(test) not in known or
106+
os.path.basename(os.path.dirname(test)) != "jsonchecker"
107+
]
95108

96109
failed_tests = []
97110
valgrind_path = use_valgrind and VALGRIND_CMD or ''
98-
for input_path in tests + test_jsonchecker:
111+
for input_path in tests:
99112
expect_failure = os.path.basename(input_path).startswith('fail')
100-
is_json_checker_test = input_path in test_jsonchecker
113+
is_json_checker_test = os.path.basename(os.path.dirname(input_path)) == "jsonchecker"
101114
is_parse_only = is_json_checker_test or expect_failure
102115
is_strict_test = ('_strict_' in os.path.basename(input_path)) or is_json_checker_test
103116
print('TESTING:', input_path, end=' ')

0 commit comments

Comments
 (0)