Skip to content

Commit 01f24da

Browse files
authored
fix: configure jsonconst double parser to use std::from_chars (#4360)
The problem: apparently, jsoncons uses strtod by default when parsing doubles. On some platforms (alpine/musl) this function uses lots of stack, which potentially can lead to stack corruption. This PR configures jsoncons to use std::from_chars that is more efficient and less stack hungry. The single include point to consume jsoncons/json.hpp should be "core/json_object.h" Signed-off-by: Roman Gershman <roman@dragonflydb.io>
1 parent 95cd9df commit 01f24da

File tree

8 files changed

+19
-17
lines changed

8 files changed

+19
-17
lines changed

src/core/compact_object.cc

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ extern "C" {
2020
#include <absl/strings/str_cat.h>
2121
#include <absl/strings/strip.h>
2222

23-
#include <jsoncons/json.hpp>
24-
2523
#include "base/flags.h"
2624
#include "base/logging.h"
2725
#include "base/pod_array.h"

src/core/compact_object_test.cc

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
#include <mimalloc.h>
88
#include <xxhash.h>
99

10-
#include <jsoncons/json.hpp>
11-
#include <jsoncons_ext/jsonpath/jsonpath.hpp>
1210
#include <random>
1311

1412
#include "base/gtest.h"

src/core/json/json_object.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@
44

55
#pragma once
66

7+
#include <version> // for __cpp_lib_to_chars macro.
8+
9+
// std::from_chars is available in C++17 if __cpp_lib_to_chars is defined.
10+
#if __cpp_lib_to_chars >= 201611L
11+
#define JSONCONS_HAS_STD_FROM_CHARS 1
12+
#endif
13+
714
#include <jsoncons/json.hpp>
815
#include <jsoncons_ext/jsonpath/jsonpath.hpp>
916
#include <memory>

src/server/cluster/cluster_config.cc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
#include <absl/container/flat_hash_set.h>
44

5-
#include <jsoncons/json.hpp>
65
#include <optional>
76
#include <string_view>
87

src/server/json_family.cc

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,6 @@
99
#include <absl/strings/str_join.h>
1010
#include <absl/strings/str_split.h>
1111

12-
#include <jsoncons/json.hpp>
13-
#include <jsoncons_ext/jsonpatch/jsonpatch.hpp>
14-
#include <jsoncons_ext/jsonpath/jsonpath.hpp>
15-
#include <jsoncons_ext/jsonpointer/jsonpointer.hpp>
16-
#include <jsoncons_ext/mergepatch/mergepatch.hpp>
17-
1812
#include "absl/cleanup/cleanup.h"
1913
#include "base/flags.h"
2014
#include "base/logging.h"
@@ -36,6 +30,12 @@
3630
#include "server/tiered_storage.h"
3731
#include "server/transaction.h"
3832

33+
// clang-format off
34+
#include <jsoncons_ext/jsonpatch/jsonpatch.hpp>
35+
#include <jsoncons_ext/jsonpointer/jsonpointer.hpp>
36+
#include <jsoncons_ext/mergepatch/mergepatch.hpp>
37+
// clang-format on
38+
3939
ABSL_FLAG(bool, jsonpathv2, true,
4040
"If true uses Dragonfly jsonpath implementation, "
4141
"otherwise uses legacy jsoncons implementation.");

src/server/json_family_test.cc

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66

77
#include <absl/strings/str_replace.h>
88

9-
#include <jsoncons/json.hpp>
10-
119
#include "base/gtest.h"
1210
#include "base/logging.h"
1311
#include "facade/facade_test.h"

src/server/search/doc_accessors.cc

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
#include <absl/strings/str_cat.h>
1313
#include <absl/strings/str_join.h>
1414

15-
#include <jsoncons/json.hpp>
16-
1715
#include "base/flags.h"
1816
#include "core/json/path.h"
1917
#include "core/overloaded.h"
@@ -86,7 +84,7 @@ SearchDocData BaseAccessor::Serialize(const search::Schema& schema,
8684
for (const auto& field : fields) {
8785
const auto& fident = field.GetIdentifier(schema, false);
8886
const auto& fname = field.GetShortName(schema);
89-
87+
9088
auto field_value =
9189
ExtractSortableValue(schema, fident, absl::StrJoin(GetStrings(fident).value(), ","));
9290
if (field_value) {

src/server/test_utils.cc

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,7 @@ void BaseFamilyTest::ResetService() {
243243
absl::SetFlag(&FLAGS_alsologtostderr, true);
244244
fb2::Mutex m;
245245
shard_set->pool()->AwaitFiberOnAll([&m](unsigned index, ProactorBase* base) {
246+
ThisFiber::SetName("Watchdog");
246247
std::unique_lock lk(m);
247248
LOG(ERROR) << "Proactor " << index << ":\n";
248249
fb2::detail::FiberInterface::PrintAllFiberStackTraces();
@@ -359,7 +360,10 @@ bool BaseFamilyTest::WaitUntilCondition(std::function<bool()> condition_cb,
359360

360361
RespExpr BaseFamilyTest::Run(ArgSlice list) {
361362
if (!ProactorBase::IsProactorThread()) {
362-
return pp_->at(0)->Await([&] { return this->Run(list); });
363+
return pp_->at(0)->Await([&] {
364+
ThisFiber::SetName("Test::Run");
365+
return this->Run(list);
366+
});
363367
}
364368

365369
return Run(GetId(), list);

0 commit comments

Comments
 (0)