Skip to content
Merged
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
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ Dirk Goetz <dirk.goetz@icinga.com>
Dirk Wening <dirk.wening@netways.de>
Dirk Melchers <dirk@dirk-melchers.de>
Dolf Schimmel <dolf@transip.nl>
Dominik Bay <dominik@bay.sh>
Dominik Riva <driva@protonmail.com>
dominik-r-s <43005480+dominik-r-s@users.noreply.github.com>
Edgar Fuß <ef@math.uni-bonn.de>
Expand Down
12 changes: 11 additions & 1 deletion lib/base/json-script.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,22 @@ static String JsonEncodeShim(const Value& value)
return JsonEncode(value);
}

static Value JsonDecodeShim(const String& data)
{
/* Wrap JsonDecode() so that the DSL function keeps its single-argument
* signature. JsonDecode()'s depthLimit parameter has a default value, but
* function pointers don't carry defaults, so binding it directly would make
* depthLimit a required argument and break Json.decode("..."). See #10913.
*/
return JsonDecode(data);
}

INITIALIZE_ONCE([]() {
Namespace::Ptr jsonNS = new Namespace(true);

/* Methods */
jsonNS->Set("encode", new Function("Json#encode", JsonEncodeShim, { "value" }, true));
jsonNS->Set("decode", new Function("Json#decode", JsonDecode, { "value" }, true));
jsonNS->Set("decode", new Function("Json#decode", JsonDecodeShim, { "value" }, true));

jsonNS->Freeze();

Expand Down
15 changes: 15 additions & 0 deletions test/base-json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "base/io-engine.hpp"
#include "base/objectlock.hpp"
#include "base/json.hpp"
#include "base/scriptglobal.hpp"
#include "test/utils.hpp"
#include <boost/algorithm/string/replace.hpp>
#include <BoostTestTargetConfig.h>
Expand Down Expand Up @@ -147,6 +148,20 @@ BOOST_AUTO_TEST_CASE(decode)
BOOST_CHECK(uint.IsNumber() && uint.Get<double>() == 23.0);
}

BOOST_AUTO_TEST_CASE(decode_dsl_single_argument)
{
/* Regression test for #10913: the Json.decode() DSL function must accept a
* single argument. JsonDecode()'s optional depthLimit parameter must not
* leak into the function binding as a required argument.
*/
Namespace::Ptr systemNS = ScriptGlobal::Get("System");
Namespace::Ptr jsonNS = systemNS->Get("Json");
Function::Ptr decode = jsonNS->Get("decode");

BOOST_REQUIRE(decode);
BOOST_CHECK_EQUAL(decode->Invoke({ "2" }), Value(2));
}

BOOST_AUTO_TEST_CASE(invalid1)
{
BOOST_CHECK_THROW(JsonDecode("\"1.7"), std::exception);
Expand Down
Loading