cache strings to solve stack-use-after-scope

Signed-off-by: Pratik Mankawde <3397372+pratikmankawde@users.noreply.github.com>
This commit is contained in:
Pratik Mankawde
2026-02-06 12:30:17 +00:00
parent 14e3e098d5
commit 65ba439117
2 changed files with 22 additions and 9 deletions

View File

@@ -78,7 +78,8 @@ static inline Json::Value
not_an_object(std::string const& object, std::string const& field)
{
auto const fieldName = make_name(object, field);
return RPC::make_error(rpcINVALID_PARAMS, "Field '" + fieldName + "' is not a JSON object.");
std::string const msg = "Field '" + fieldName + "' is not a JSON object.";
return RPC::make_error(rpcINVALID_PARAMS, msg);
}
static inline Json::Value
@@ -97,28 +98,32 @@ static inline Json::Value
unknown_field(std::string const& object, std::string const& field)
{
auto const fieldName = make_name(object, field);
return RPC::make_error(rpcINVALID_PARAMS, "Field '" + fieldName + "' is unknown.");
std::string const msg = "Field '" + fieldName + "' is unknown.";
return RPC::make_error(rpcINVALID_PARAMS, msg);
}
static inline Json::Value
out_of_range(std::string const& object, std::string const& field)
{
auto const fieldName = make_name(object, field);
return RPC::make_error(rpcINVALID_PARAMS, "Field '" + fieldName + "' is out of range.");
std::string const msg = "Field '" + fieldName + "' is out of range.";
return RPC::make_error(rpcINVALID_PARAMS, msg);
}
static inline Json::Value
bad_type(std::string const& object, std::string const& field)
{
auto const fieldName = make_name(object, field);
return RPC::make_error(rpcINVALID_PARAMS, "Field '" + fieldName + "' has bad type.");
std::string const msg = "Field '" + fieldName + "' has bad type.";
return RPC::make_error(rpcINVALID_PARAMS, msg);
}
static inline Json::Value
invalid_data(std::string const& object, std::string const& field)
{
auto const fieldName = make_name(object, field);
return RPC::make_error(rpcINVALID_PARAMS, "Field '" + fieldName + "' has invalid data.");
std::string const msg = "Field '" + fieldName + "' has invalid data.";
return RPC::make_error(rpcINVALID_PARAMS, msg);
}
static inline Json::Value
@@ -131,14 +136,16 @@ static inline Json::Value
array_expected(std::string const& object, std::string const& field)
{
auto const fieldName = make_name(object, field);
return RPC::make_error(rpcINVALID_PARAMS, "Field '" + fieldName + "' must be a JSON array.");
std::string const msg = "Field '" + fieldName + "' must be a JSON array.";
return RPC::make_error(rpcINVALID_PARAMS, msg);
}
static inline Json::Value
string_expected(std::string const& object, std::string const& field)
{
auto const fieldName = make_name(object, field);
return RPC::make_error(rpcINVALID_PARAMS, "Field '" + fieldName + "' must be a string.");
std::string const msg = "Field '" + fieldName + "' must be a string.";
return RPC::make_error(rpcINVALID_PARAMS, msg);
}
static inline Json::Value

View File

@@ -179,7 +179,10 @@ AMMLiquidity<TIn, TOut>::getOffer(ReadView const& view, std::optional<Quality> c
}
catch (std::overflow_error const& e)
{
JLOG(j_.error()) << "AMMLiquidity::getOffer overflow " << e.what();
// Store e.what() in a local variable to prevent stack-use-after-scope
// when the exception's internal storage becomes invalid in coroutine context
std::string const errorMsg = e.what();
JLOG(j_.error()) << "AMMLiquidity::getOffer overflow " << errorMsg;
if (!view.rules().enabled(fixAMMOverflowOffer))
return maxOffer(balances, view.rules());
else
@@ -187,7 +190,10 @@ AMMLiquidity<TIn, TOut>::getOffer(ReadView const& view, std::optional<Quality> c
}
catch (std::exception const& e)
{
JLOG(j_.error()) << "AMMLiquidity::getOffer exception " << e.what();
// Store e.what() in a local variable to prevent stack-use-after-scope
// when the exception's internal storage becomes invalid in coroutine context
std::string const errorMsg = e.what();
JLOG(j_.error()) << "AMMLiquidity::getOffer exception " << errorMsg;
}
return std::nullopt;
}();