mirror of
https://github.com/NixOS/nix
synced 2025-07-05 04:01:47 +02:00
Merge pull request #13112 from NaN-git/fix-json-getInteger
bugfix in getInteger(const nlohmann::json &) and add bounds checks
This commit is contained in:
commit
4548dd1abb
5 changed files with 54 additions and 15 deletions
|
@ -4,6 +4,7 @@
|
|||
#include <nlohmann/json.hpp>
|
||||
#include <list>
|
||||
|
||||
#include "nix/util/error.hh"
|
||||
#include "nix/util/types.hh"
|
||||
|
||||
namespace nix {
|
||||
|
@ -35,7 +36,26 @@ const nlohmann::json * getNullable(const nlohmann::json & value);
|
|||
const nlohmann::json::object_t & getObject(const nlohmann::json & value);
|
||||
const nlohmann::json::array_t & getArray(const nlohmann::json & value);
|
||||
const nlohmann::json::string_t & getString(const nlohmann::json & value);
|
||||
const nlohmann::json::number_integer_t & getInteger(const nlohmann::json & value);
|
||||
const nlohmann::json::number_unsigned_t & getUnsigned(const nlohmann::json & value);
|
||||
|
||||
template<typename T>
|
||||
auto getInteger(const nlohmann::json & value) -> std::enable_if_t<std::is_signed_v<T> && std::is_integral_v<T>, T>
|
||||
{
|
||||
if (auto ptr = value.get_ptr<const nlohmann::json::number_unsigned_t *>()) {
|
||||
if (*ptr <= std::make_unsigned_t<T>(std::numeric_limits<T>::max())) {
|
||||
return *ptr;
|
||||
}
|
||||
} else if (auto ptr = value.get_ptr<const nlohmann::json::number_integer_t *>()) {
|
||||
if (*ptr >= std::numeric_limits<T>::min() && *ptr <= std::numeric_limits<T>::max()) {
|
||||
return *ptr;
|
||||
}
|
||||
} else {
|
||||
auto typeName = value.is_number_float() ? "floating point number" : value.type_name();
|
||||
throw Error("Expected JSON value to be an integral number but it is of type '%s': %s", typeName, value.dump());
|
||||
}
|
||||
throw Error("Out of range: JSON value '%s' cannot be casted to %d-bit integer", value.dump(), 8 * sizeof(T));
|
||||
}
|
||||
|
||||
const nlohmann::json::boolean_t & getBoolean(const nlohmann::json & value);
|
||||
Strings getStringList(const nlohmann::json & value);
|
||||
StringMap getStringMap(const nlohmann::json & value);
|
||||
|
|
|
@ -92,9 +92,18 @@ const nlohmann::json::string_t & getString(const nlohmann::json & value)
|
|||
return ensureType(value, nlohmann::json::value_t::string).get_ref<const nlohmann::json::string_t &>();
|
||||
}
|
||||
|
||||
const nlohmann::json::number_integer_t & getInteger(const nlohmann::json & value)
|
||||
const nlohmann::json::number_unsigned_t & getUnsigned(const nlohmann::json & value)
|
||||
{
|
||||
return ensureType(value, nlohmann::json::value_t::number_integer).get_ref<const nlohmann::json::number_integer_t &>();
|
||||
if (auto ptr = value.get<const nlohmann::json::number_unsigned_t *>()) {
|
||||
return *ptr;
|
||||
}
|
||||
const char * typeName = value.type_name();
|
||||
if (typeName == nlohmann::json(0).type_name()) {
|
||||
typeName = value.is_number_float() ? "floating point number" : "signed integral number";
|
||||
}
|
||||
throw Error(
|
||||
"Expected JSON value to be an unsigned integral number but it is of type '%s': %s",
|
||||
typeName, value.dump());
|
||||
}
|
||||
|
||||
const nlohmann::json::boolean_t & getBoolean(const nlohmann::json & value)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue