Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make the build configuration under Bazel more correct. #1600

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 29 additions & 4 deletions BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,7 +1,29 @@
licenses(["unencumbered"]) # Public Domain or MIT

load("@bazel_skylib//rules:common_settings.bzl", "bool_flag")

exports_files(["LICENSE"])

bool_flag(
name = "use_exception",
build_setting_default = False,
)

config_setting(
name = "use_exception_cfg",
flag_values = {":use_exception": "true"},
)

bool_flag(
name = "has_int64",
build_setting_default = True,
)

config_setting(
name = "has_int64_cfg",
flag_values = {":has_int64": "true"},
)

cc_library(
name = "jsoncpp",
srcs = [
Expand All @@ -22,10 +44,13 @@ cc_library(
"include/json/version.h",
"include/json/writer.h",
],
copts = [
"-DJSON_USE_EXCEPTION=0",
"-DJSON_HAS_INT64",
],
defines = select({
":use_exception_cfg": ["JSON_USE_EXCEPTION=1"],
"//conditions:default": ["JSON_USE_EXCEPTION=0"],
}) + select({
":has_int64_cfg": ["JSON_HAS_INT64"],
"//conditions:default": [],
}),
includes = ["include"],
visibility = ["//visibility:public"],
deps = [":private"],
Expand Down
5 changes: 5 additions & 0 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,8 @@ module(
version = "1.9.7",
compatibility_level = 1,
)

bazel_dep(
name = "bazel_skylib",
version = "1.7.1",
)
8 changes: 4 additions & 4 deletions src/test_lib_json/fuzz.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@
#include <memory>
#include <string>

namespace Json {
class Exception;
}

extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
Json::CharReaderBuilder builder;

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

Json::Value root;
const auto data_str = reinterpret_cast<const char*>(data);
#if JSON_USE_EXCEPTION
try {
#endif // JSON_USE_EXCEPTION
reader->parse(data_str, data_str + size, &root, nullptr);
#if JSON_USE_EXCEPTION
} catch (Json::Exception const&) {
}
#endif // JSON_USE_EXCEPTION
// Whether it succeeded or not doesn't matter.
return 0;
}
4 changes: 4 additions & 0 deletions src/test_lib_json/jsontest.h
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,8 @@ TestResult& checkStringEqual(TestResult& result, const Json::String& expected,
JsonTest::ToJsonString(actual), __FILE__, \
__LINE__, #expected " == " #actual)

#if JSON_USE_EXCEPTION

/// \brief Asserts that a given expression throws an exception
#define JSONTEST_ASSERT_THROWS(expr) \
do { \
Expand All @@ -242,6 +244,8 @@ TestResult& checkStringEqual(TestResult& result, const Json::String& expected,
"expected exception thrown: " #expr); \
} while (0)

#endif // JSON_USE_EXCEPTION
Copy link
Contributor Author

@bcsgh bcsgh Mar 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm halfway tempted to try to make a version of this that fork()s and checks that the invocation dies. (e.g. a death test).

if (pid_t child = fork(); child) {
  int status = 0;
  waitpid(child, &status, 0);
  if (status == 0) {
    result_->addFailure(__FILE__, __LINE__, "Expected assertion: ", #expr);
  }
} else {
  expr;
  exit_(0);
}

Something like that might work, but I don't really have time to dig into all the ways that can be subtly wrong.


/// \brief Begin a fixture test case.
#define JSONTEST_FIXTURE(FixtureType, name) \
class Test##FixtureType##name : public FixtureType { \
Expand Down
10 changes: 9 additions & 1 deletion src/test_lib_json/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1888,7 +1888,7 @@ JSONTEST_FIXTURE_LOCAL(ValueTest, typeChecksThrowExceptions) {
JSONTEST_ASSERT_THROWS(objVal.asBool());
JSONTEST_ASSERT_THROWS(arrVal.asBool());

#endif
#endif // JSON_USE_EXCEPTION
}

JSONTEST_FIXTURE_LOCAL(ValueTest, offsetAccessors) {
Expand Down Expand Up @@ -3323,6 +3323,8 @@ JSONTEST_FIXTURE_LOCAL(CharReaderTest, parseWithDetailError) {
}

JSONTEST_FIXTURE_LOCAL(CharReaderTest, parseWithStackLimit) {
#if JSON_USE_EXCEPTION

Json::CharReaderBuilder b;
Json::Value root;
char const doc[] = R"({ "property" : "value" })";
Expand All @@ -3342,6 +3344,8 @@ JSONTEST_FIXTURE_LOCAL(CharReaderTest, parseWithStackLimit) {
JSONTEST_ASSERT_THROWS(
reader->parse(doc, doc + std::strlen(doc), &root, &errs));
}

#endif // JSON_USE_EXCEPTION
}

JSONTEST_FIXTURE_LOCAL(CharReaderTest, testOperator) {
Expand Down Expand Up @@ -3961,6 +3965,8 @@ JSONTEST_FIXTURE_LOCAL(IteratorTest, indexes) {
}

JSONTEST_FIXTURE_LOCAL(IteratorTest, constness) {
#if JSON_USE_EXCEPTION

Json::Value const v;
JSONTEST_ASSERT_THROWS(
Json::Value::iterator it(v.begin())); // Compile, but throw.
Expand All @@ -3982,6 +3988,8 @@ JSONTEST_FIXTURE_LOCAL(IteratorTest, constness) {
}
Json::String expected = R"(" 9","10","11",)";
JSONTEST_ASSERT_STRING_EQUAL(expected, out.str());

#endif // JSON_USE_EXCEPTION
}

struct RValueTest : JsonTest::TestCase {};
Expand Down
Loading