rpc: add alternative typed getters with default values
What changed, and why it matters
This commit adds convenience functions that read settings from CBOR messages and return a default value if the setting is missing or invalid. It is a straightforward feature addition with no visible security bug.
No security action required. Treat as a normal feature/refactoring commit.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The patch introduces _or() variants of existing typed CBOR-RPC getters (rpc_get_boolean_or, rpc_get_uint64_t_or, rpc_get_sizet_or). These wrappers call the original getters, ignore the success/failure result, and return either the parsed value or a caller-supplied default. The header is updated with declarations and a comment explaining the new behavior. No existing callers are changed, and no security-sensitive logic is modified.
Changed components
main/utils/cbor_rpc.cmain/utils/cbor_rpc.hInspect captured patch +27 / −1
diff --git a/main/utils/cbor_rpc.c b/main/utils/cbor_rpc.c
index 8dcf6b1..a6bceca 100644
--- a/main/utils/cbor_rpc.c
+++ b/main/utils/cbor_rpc.c
@@ -296,6 +296,13 @@ bool rpc_get_boolean(const char* field, const CborValue* value, bool* res)
&& cbor_value_get_boolean(&result, res) == CborNoError;
}
+bool rpc_get_boolean_or(const char* field, const CborValue* value, const bool default_value)
+{
+ bool res = default_value;
+ IGNORE_RESULT(rpc_get_boolean(field, value, &res));
+ return res;
+}
+
bool rpc_get_uint64_t(const char* field, const CborValue* value, uint64_t* res)
{
JADE_ASSERT(value);
@@ -311,6 +318,13 @@ bool rpc_get_uint64_t(const char* field, const CborValue* value, uint64_t* res)
return true;
}
+uint64_t rpc_get_uint64_t_or(const char* field, const CborValue* value, const uint64_t default_value)
+{
+ uint64_t res = default_value;
+ IGNORE_RESULT(rpc_get_uint64_t(field, value, &res));
+ return res;
+}
+
bool rpc_get_sizet(const char* field, const CborValue* value, size_t* res)
{
JADE_ASSERT(value);
@@ -331,6 +345,13 @@ bool rpc_get_sizet(const char* field, const CborValue* value, size_t* res)
return true;
}
+size_t rpc_get_sizet_or(const char* field, const CborValue* value, const size_t default_value)
+{
+ size_t res = default_value;
+ IGNORE_RESULT(rpc_get_sizet(field, value, &res));
+ return res;
+}
+
void rpc_get_method(const CborValue* value, const char** data, size_t* written)
{
JADE_ASSERT(value);
diff --git a/main/utils/cbor_rpc.h b/main/utils/cbor_rpc.h
index 9bb67f3..e546104 100644
--- a/main/utils/cbor_rpc.h
+++ b/main/utils/cbor_rpc.h
@@ -1,6 +1,7 @@
#ifndef UTILS_CBOR_RPC_H_
#define UTILS_CBOR_RPC_H_
+#include "../jade_assert.h"
#include <cbor.h>
#include <stdbool.h>
#include <stddef.h>
@@ -39,7 +40,8 @@ void rpc_get_id_ptr(const CborValue* value, const char** data, size_t* written);
void rpc_get_method(const CborValue* value, const char** data, size_t* written);
bool rpc_is_method(const CborValue* value, const char* method);
-// Some typed/checked getters for various nodes/data-types
+// Typed/checked getters for various nodes/data-types.
+// _or() variants accept and return a default value if the field is not present.
bool rpc_has_field_data(const char* field, const CborValue* value);
void rpc_get_raw_string_ptr(const CborValue* value, const char** data, size_t* size);
void rpc_get_string_ptr(const char* field, const CborValue* value, const char** data, size_t* size);
@@ -49,8 +51,11 @@ void rpc_get_bytes_ptr(const char* field, const CborValue* value, const uint8_t*
void rpc_get_bytes(const char* field, size_t max, const CborValue* value, uint8_t* data, size_t* written);
bool rpc_get_n_bytes(const char* field, const CborValue* value, size_t expected_size, uint8_t* data);
bool rpc_get_sizet(const char* field, const CborValue* value, size_t* res);
+size_t rpc_get_sizet_or(const char* field, const CborValue* value, size_t default_value);
bool rpc_get_uint64_t(const char* field, const CborValue* value, uint64_t* res);
+uint64_t rpc_get_uint64_t_or(const char* field, const CborValue* value, uint64_t default_value);
bool rpc_get_boolean(const char* field, const CborValue* value, bool* res);
+bool rpc_get_boolean_or(const char* field, const CborValue* value, bool default_value);
bool rpc_get_bip32_path(
const char* field, const CborValue* value, uint32_t* path_ptr, size_t max_path_len, size_t* written);
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.