move DEVICE_MAX_LEN/MULTISIG_NAME_MAX_LEN defs from C to Rust
What changed, and why it matters
This commit is a routine code reorganization. It moves two constant definitions—how long device names and multisig account names can be—from the C code into Rust, so both the BitBox02 and future BitBox03 products can share the same values. The actual length limits (63 characters plus a null terminator for device names, 30 plus null for multisig names) are unchanged, and the commit adds compile-time checks to prove they stay the same. There is no security bug being fixed here.
No security action required. Treat as normal refactoring; standard review and CI testing are sufficient.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The change relocates DEVICE_NAME_MAX_LEN and MULTISIG_NAME_MAX_LEN from bitbox02_sys C bindings into the bitbox-hal Rust crate. C code now imports MEMORY_DEVICE_MAX_LEN_WITH_NULL (64) and MEMORY_MULTISIG_NAME_MAX_LEN_WITH_NULL (31) via cbindgen from bitbox02-rust-c. Static assertions in memory.c and const-array assertions in lib.rs enforce that the exported values match the Rust HAL constants and the existing persistent memory layout. All call sites are updated consistently; no functional length limits change.
Changed components
src/memory/memory.csrc/memory/memory.hsrc/memory/memory_shared.csrc/memory/memory_shared.hsrc/rust/bitbox-hal/src/memory.rssrc/rust/bitbox02-rust-c/src/lib.rssrc/rust/bitbox02-rust/src/hww/api/bitcoin/registration.rssrc/rust/bitbox02-rust/src/hww/api/set_device_name.rssrc/rust/bitbox02/src/memory.rssrc/ui/components/lockscreen.csrc/bootloader/bootloader.csrc/bootloader/startup.csrc/da14531/da14531_handler.ctest/unit-test/test_memory.csrc/rust/bitbox02-sys/build.rsInspect captured patch +79 / −53
diff --git a/src/bootloader/bootloader.c b/src/bootloader/bootloader.c
index 82eb4bd..5e89684 100644
--- a/src/bootloader/bootloader.c
+++ b/src/bootloader/bootloader.c
@@ -330,7 +330,7 @@ void bootloader_render_default_screen(void)
UG_PutString(0, SCREEN_HEIGHT - 9 * 2 - 5, "See the BitBoxApp", false);
if (rust_communication_mode_ble_enabled() &&
da14531_connected_state < DA14531_CONNECTED_CONNECTED_SECURED) {
- char buf[MEMORY_DEVICE_NAME_MAX_LEN] = {0};
+ char buf[MEMORY_DEVICE_MAX_LEN_WITH_NULL] = {0};
memory_random_name(buf);
UG_PutString(0, SCREEN_HEIGHT - 9, buf, false);
} else if (_is_app_flash_empty) {
diff --git a/src/bootloader/startup.c b/src/bootloader/startup.c
index 4bb9178..5826506 100644
--- a/src/bootloader/startup.c
+++ b/src/bootloader/startup.c
@@ -99,7 +99,7 @@ int main(void)
// Set device name, the MCU and BLE chip will probably not have the same name after a reset of
// only the MCU.
- char buf[MEMORY_DEVICE_NAME_MAX_LEN] = {0};
+ char buf[MEMORY_DEVICE_MAX_LEN_WITH_NULL] = {0};
memory_random_name(buf);
da14531_set_name(buf, &uart_write_queue);
diff --git a/src/da14531/da14531_handler.c b/src/da14531/da14531_handler.c
index 76460c2..bacf1eb 100644
--- a/src/da14531/da14531_handler.c
+++ b/src/da14531/da14531_handler.c
@@ -11,6 +11,7 @@
#include "usb/usb_frame.h"
#include "usb/usb_packet.h"
#include "utils_ringbuffer.h"
+#include <rust/rust.h>
#include <ui/components/confirm.h>
#include <ui/components/ui_images.h>
#include <ui/fonts/monogram_5X9.h>
@@ -79,7 +80,7 @@ static void _ctrl_handler(const struct da14531_ctrl_frame* frame, struct ringbuf
// util_log("da14531: get device name");
// 1 byte cmd
// rest device name
- uint8_t response[1 + MEMORY_DEVICE_NAME_MAX_LEN] = {0}; // +1 for cmd
+ uint8_t response[1 + MEMORY_DEVICE_MAX_LEN_WITH_NULL] = {0}; // +1 for cmd
response[0] = CTRL_CMD_DEVICE_NAME;
#if defined(BOOTLOADER)
memory_random_name((char*)&response[1]);
diff --git a/src/memory/memory.c b/src/memory/memory.c
index cef2812..da4ddb7 100644
--- a/src/memory/memory.c
+++ b/src/memory/memory.c
@@ -26,6 +26,14 @@
const char* MEMORY_DEFAULT_DEVICE_NAME = "My BitBox";
+// These values are part of the persistent memory layout and must never change without a migration.
+static_assert(
+ MEMORY_DEVICE_MAX_LEN_WITH_NULL == 64,
+ "MEMORY_DEVICE_MAX_LEN_WITH_NULL must remain 64.");
+static_assert(
+ MEMORY_MULTISIG_NAME_MAX_LEN_WITH_NULL == 31,
+ "MEMORY_MULTISIG_NAME_MAX_LEN_WITH_NULL must remain 31.");
+
// Documentation of all appData chunks and their contents. A chunk is defined as
// 16 pages, which is the erase granularity: changing any byte in the page
// involves erases and writing all 16 pages. One page is 512 bytes. The MCU has
@@ -82,8 +90,8 @@ typedef union {
uint8_t noise_static_private_key[32]; // CURVE25519
uint8_t noise_remote_static_pubkeys[5][NOISE_PUBKEY_SIZE]; // 5 pubkey slots
uint8_t salt_root[32];
- uint8_t
- device_name[MEMORY_DEVICE_NAME_MAX_LEN]; // utf8 encoded device name. 0xFF if not set.
+ uint8_t device_name[MEMORY_DEVICE_MAX_LEN_WITH_NULL]; // utf8 encoded device name. 0xFF if
+ // not set.
uint8_t encrypted_seed_and_hmac_len;
uint8_t encrypted_seed_and_hmac[96];
uint32_t seed_birthdate; // unix timestamp.
@@ -98,7 +106,7 @@ typedef struct __attribute__((__packed__)) {
// support other types of data.
// The multisig entry is considered empty/unset if the hash is filled with 0xFF.
uint8_t version;
- char name[MEMORY_MULTISIG_NAME_MAX_LEN]; // user-given name for this multisig setup.
+ char name[MEMORY_MULTISIG_NAME_MAX_LEN_WITH_NULL]; // user-given name for this multisig setup.
uint8_t hash[32]; // hash comitting to the multisig setup.
} multisig_configuration_t;
@@ -240,9 +248,9 @@ bool memory_set_device_name(const char* name)
CLEANUP_CHUNK(chunk);
_read_chunk(CHUNK_1, chunk_bytes);
util_zero(chunk.fields.device_name, sizeof(chunk.fields.device_name));
- snprintf((char*)&chunk.fields.device_name, MEMORY_DEVICE_NAME_MAX_LEN, "%s", name);
+ snprintf((char*)&chunk.fields.device_name, MEMORY_DEVICE_MAX_LEN_WITH_NULL, "%s", name);
- if (!rust_util_is_name_valid(chunk.fields.device_name, MEMORY_DEVICE_NAME_MAX_LEN)) {
+ if (!rust_util_is_name_valid(chunk.fields.device_name, MEMORY_DEVICE_MAX_LEN_WITH_NULL)) {
return false;
}
return _write_chunk(CHUNK_1, chunk.bytes);
@@ -254,16 +262,16 @@ void memory_get_device_name(char* name_out)
CLEANUP_CHUNK(chunk);
_read_chunk(CHUNK_1, chunk_bytes);
if (chunk.fields.device_name[0] == 0xFF ||
- !rust_util_is_name_valid(chunk.fields.device_name, MEMORY_DEVICE_NAME_MAX_LEN)) {
+ !rust_util_is_name_valid(chunk.fields.device_name, MEMORY_DEVICE_MAX_LEN_WITH_NULL)) {
if (memory_get_platform() == MEMORY_PLATFORM_BITBOX02_PLUS) {
// For Bluetooth, we want to use an unambiguous default name so this BitBox can be
// identified if multiple BitBoxes are advertising at the same time.
memory_random_name(name_out);
} else {
- snprintf(name_out, MEMORY_DEVICE_NAME_MAX_LEN, "%s", MEMORY_DEFAULT_DEVICE_NAME);
+ snprintf(name_out, MEMORY_DEVICE_MAX_LEN_WITH_NULL, "%s", MEMORY_DEFAULT_DEVICE_NAME);
}
} else {
- snprintf(name_out, MEMORY_DEVICE_NAME_MAX_LEN, "%s", chunk.fields.device_name);
+ snprintf(name_out, MEMORY_DEVICE_MAX_LEN_WITH_NULL, "%s", chunk.fields.device_name);
}
}
diff --git a/src/memory/memory.h b/src/memory/memory.h
index 31ab8a1..89ffaf6 100644
--- a/src/memory/memory.h
+++ b/src/memory/memory.h
@@ -11,9 +11,6 @@
#define NOISE_PUBKEY_SIZE 32
-// Including null terminator.
-#define MEMORY_MULTISIG_NAME_MAX_LEN (31)
-
// How many multisig configurations (accounts) can be registered.
#define MEMORY_MULTISIG_NUM_ENTRIES 25
@@ -66,18 +63,15 @@ USE_RESULT bool memory_cleanup_smarteeprom(void);
// Default device name if no name was set by the user.
extern const char* MEMORY_DEFAULT_DEVICE_NAME;
-// Don't change this without proper memory layout migration! (see chunk_1_t in
-// memory.c)
-#define MEMORY_DEVICE_NAME_MAX_LEN (64)
// set device name. name is null terminated. The name must be smaller or equal to
-// MEMORY_DEVICE_NAME_MAX_LEN (including the null terminator) and larger than 0 in size, consist of
-// printable ASCII characters only (and space), not start or end with whitespace, and contain no
-// whitespace other than space.
+// MEMORY_DEVICE_MAX_LEN_WITH_NULL (including the null terminator) and larger than 0 in size,
+// consist of printable ASCII characters only (and space), not start or end with whitespace, and
+// contain no whitespace other than space.
USE_RESULT bool memory_set_device_name(const char* name);
-// name_out must have MEMORY_DEVICE_NAME_MAX_LEN bytes in size. If no device name is set, or if it
-// is invalid, we return:
+// name_out must have MEMORY_DEVICE_MAX_LEN_WITH_NULL bytes in size. If no device name is set, or if
+// it is invalid, we return:
// - `MEMORY_DEFAULT_DEVICE_NAME` for non-bluetooth enabled BitBoxes
// - "BitBox ABCD" for Bluetooth-enabled BitBoxes, where ABCD are four random uppercase letters.
// The name is cached in RAM, so the same random name is returned until reboot.
@@ -259,7 +253,7 @@ USE_RESULT bool memory_add_noise_remote_static_pubkey(const uint8_t* pubkey);
* If a name is already stored with this hash, the old name will be overwritten.
* It's the callers responsibility to validate the name (beyond that it must be non-empty).
* @param[in] hash hash identifying the multisig config. Can't be 0xfffff....
- * @param[in] human readable name. Must be at most MEMORY_MULTISIG_NAME_MAX_LEN bytes,
+ * @param[in] human readable name. Must be at most MEMORY_MULTISIG_NAME_MAX_LEN_WITH_NULL bytes,
* including the null terminator (otherwise the name will be truncated), and non-empty.
* @return see memory_result_t, can return MEMORY_OK, MEMORY_ERR_INVALID_INPUT, MEMORY_ERR_FULL,
* MEMORY_ERR_DUPLICATE_NAME, MEMORY_ERR_UNKNOWN.
@@ -269,7 +263,8 @@ USE_RESULT memory_result_t memory_multisig_set_by_hash(const uint8_t* hash, cons
/**
* Retrieves the name of a previously stored multisig config identified by `hash`.
* @param[in] hash hash identifying the multisig config.
- * @param[out] name_out will contain the name. Must have at least `MEMORY_MULTISIG_NAME_MAX_LEN`
+ * @param[out] name_out will contain the name. Must have at least
+ * `MEMORY_MULTISIG_NAME_MAX_LEN_WITH_NULL`
* bytes. Can be NULL.
* @return true if the multisig config was found, false otherwise.
*/
diff --git a/src/memory/memory_shared.c b/src/memory/memory_shared.c
index c5aa24b..7545759 100644
--- a/src/memory/memory_shared.c
+++ b/src/memory/memory_shared.c
@@ -7,6 +7,7 @@
#include <driver_init.h>
#include <flags.h>
+#include <rust/rust.h>
#include <util.h>
#include <utils_assert.h>
@@ -237,7 +238,7 @@ void memory_get_ble_metadata(memory_ble_metadata_t* metadata_out)
void memory_random_name(char* name_out)
{
- static char cached_name[MEMORY_DEVICE_NAME_MAX_LEN] = {0};
+ static char cached_name[MEMORY_DEVICE_MAX_LEN_WITH_NULL] = {0};
if (cached_name[0] == 0x00) {
// Generate 4 random uppercase letters
@@ -251,7 +252,7 @@ void memory_random_name(char* name_out)
// Format into cached name
snprintf(
cached_name,
- MEMORY_DEVICE_NAME_MAX_LEN,
+ MEMORY_DEVICE_MAX_LEN_WITH_NULL,
"BitBox %c%c%c%c",
letters[0],
letters[1],
@@ -260,5 +261,5 @@ void memory_random_name(char* name_out)
}
// Copy cached result to output
- snprintf(name_out, MEMORY_DEVICE_NAME_MAX_LEN, "%s", cached_name);
+ snprintf(name_out, MEMORY_DEVICE_MAX_LEN_WITH_NULL, "%s", cached_name);
}
diff --git a/src/memory/memory_shared.h b/src/memory/memory_shared.h
index 68f81e8..44f6cae 100644
--- a/src/memory/memory_shared.h
+++ b/src/memory/memory_shared.h
@@ -156,7 +156,7 @@ typedef struct {
USE_RESULT bool memory_set_ble_metadata(const memory_ble_metadata_t* metadata);
void memory_get_ble_metadata(memory_ble_metadata_t* metadata_out);
-// name_out must have MEMORY_DEVICE_NAME_MAX_LEN bytes in size.
+// name_out must have MEMORY_DEVICE_MAX_LEN_WITH_NULL bytes in size.
void memory_random_name(char* name_out);
#endif
diff --git a/src/rust/bitbox-hal/src/memory.rs b/src/rust/bitbox-hal/src/memory.rs
index 232de91..7eefa2b 100644
--- a/src/rust/bitbox-hal/src/memory.rs
+++ b/src/rust/bitbox-hal/src/memory.rs
@@ -3,6 +3,12 @@
use alloc::string::String;
use alloc::vec::Vec;
+/// Maximum device name length in bytes, excluding the null terminator used in C strings.
+pub const DEVICE_NAME_MAX_LEN: usize = 63;
+/// Maximum multisig account name length in bytes, excluding the null terminator used in C
+/// strings.
+pub const MULTISIG_NAME_MAX_LEN: usize = 30;
+
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum PasswordStretchAlgo {
V0,
@@ -35,6 +41,7 @@ pub trait Memory {
fn get_securechip_type(&mut self) -> Result<SecurechipType, ()>;
fn get_platform(&mut self) -> Result<Platform, ()>;
fn get_device_name(&mut self) -> String;
+ /// `name` must be non-empty and at most [`DEVICE_NAME_MAX_LEN`] bytes long.
fn set_device_name(&mut self, name: &str) -> Result<(), Error>;
fn is_mnemonic_passphrase_enabled(&mut self) -> bool;
fn set_mnemonic_passphrase_enabled(&mut self, enabled: bool) -> Result<(), ()>;
@@ -61,6 +68,7 @@ pub trait Memory {
root_pubkey_identifier_out: &mut [u8; 32],
) -> Result<(), ()>;
fn get_attestation_bootloader_hash(&mut self) -> [u8; 32];
+ /// `name` must be non-empty and at most [`MULTISIG_NAME_MAX_LEN`] bytes long.
fn multisig_set_by_hash(&mut self, hash: &[u8; 32], name: &str) -> Result<(), Error>;
fn multisig_get_by_hash(&self, hash: &[u8; 32]) -> Option<String>;
}
diff --git a/src/rust/bitbox02-rust-c/src/lib.rs b/src/rust/bitbox02-rust-c/src/lib.rs
index 7879519..6992c3c 100644
--- a/src/rust/bitbox02-rust-c/src/lib.rs
+++ b/src/rust/bitbox02-rust-c/src/lib.rs
@@ -58,6 +58,20 @@ extern crate util;
))]
type HalImpl = bitbox02::hal::BitBox02Hal;
+// Keep this as a numeric literal so cbindgen reliably exports it to C.
+// The static assert below enforces consistency with the Rust HAL constant.
+/// Maximum device name length in bytes, including null terminator.
+pub const MEMORY_DEVICE_MAX_LEN_WITH_NULL: u8 = 64;
+// Keep this as a numeric literal so cbindgen reliably exports it to C.
+// The static assert below enforces consistency with the Rust HAL constant.
+/// Maximum multisig account name length in bytes, including null terminator.
+pub const MEMORY_MULTISIG_NAME_MAX_LEN_WITH_NULL: u8 = 31;
+
+const _: [(); bitbox_hal::memory::DEVICE_NAME_MAX_LEN + 1] =
+ [(); MEMORY_DEVICE_MAX_LEN_WITH_NULL as usize];
+const _: [(); bitbox_hal::memory::MULTISIG_NAME_MAX_LEN + 1] =
+ [(); MEMORY_MULTISIG_NAME_MAX_LEN_WITH_NULL as usize];
+
// Whenever execution reaches somewhere it isn't supposed to rust code will "panic". Our panic
// handler will print the available information on the screen and over RTT. If we compile with
// `panic=abort` this code will never get executed.
diff --git a/src/rust/bitbox02-rust/src/hww/api/bitcoin/registration.rs b/src/rust/bitbox02-rust/src/hww/api/bitcoin/registration.rs
index 37f54fc..4c5d0bf 100644
--- a/src/rust/bitbox02-rust/src/hww/api/bitcoin/registration.rs
+++ b/src/rust/bitbox02-rust/src/hww/api/bitcoin/registration.rs
@@ -87,11 +87,11 @@ async fn get_name(
// We truncate the user input string to fit into the maximum allowed multisig
// account name length. This is not very nice, but it has to do until we have some
// sort of indication in the input component.
- util::strings::truncate_str(name.as_str(), bitbox02::memory::MULTISIG_NAME_MAX_LEN).into()
+ util::strings::truncate_str(name.as_str(), bitbox_hal::memory::MULTISIG_NAME_MAX_LEN).into()
} else {
request.name.clone()
};
- if !util::name::validate(&name, bitbox02::memory::MULTISIG_NAME_MAX_LEN) {
+ if !util::name::validate(&name, bitbox_hal::memory::MULTISIG_NAME_MAX_LEN) {
return Err(Error::InvalidInput);
}
Ok(name)
diff --git a/src/rust/bitbox02-rust/src/hww/api/set_device_name.rs b/src/rust/bitbox02-rust/src/hww/api/set_device_name.rs
index 1e0bc89..ec00d0a 100644
--- a/src/rust/bitbox02-rust/src/hww/api/set_device_name.rs
+++ b/src/rust/bitbox02-rust/src/hww/api/set_device_name.rs
@@ -12,7 +12,7 @@ pub async fn process(
hal: &mut impl crate::hal::Hal,
pb::SetDeviceNameRequest { name }: &pb::SetDeviceNameRequest,
) -> Result<Response, Error> {
- if !util::name::validate(name, bitbox02::memory::DEVICE_NAME_MAX_LEN) {
+ if !util::name::validate(name, bitbox_hal::memory::DEVICE_NAME_MAX_LEN) {
return Err(Error::InvalidInput);
}
diff --git a/src/rust/bitbox02-sys/build.rs b/src/rust/bitbox02-sys/build.rs
index d3b949a..c618531 100644
--- a/src/rust/bitbox02-sys/build.rs
+++ b/src/rust/bitbox02-sys/build.rs
@@ -19,9 +19,7 @@ const ALLOWLIST_VARS: &[&str] = &[
"MAX_LABEL_SIZE",
"MAX_PK_SCRIPT_SIZE",
"MAX_VARINT_SIZE",
- "MEMORY_DEVICE_NAME_MAX_LEN",
"MEMORY_MULTISIG_NUM_ENTRIES",
- "MEMORY_MULTISIG_NAME_MAX_LEN",
"MEMORY_PLATFORM_BITBOX02_PLUS",
"MEMORY_PLATFORM_BITBOX02",
"MEMORY_SECURECHIP_TYPE_ATECC",
diff --git a/src/rust/bitbox02/src/memory.rs b/src/rust/bitbox02/src/memory.rs
index 764503a..5ec8358 100644
--- a/src/rust/bitbox02/src/memory.rs
+++ b/src/rust/bitbox02/src/memory.rs
@@ -3,12 +3,7 @@
extern crate alloc;
use alloc::string::String;
use alloc::vec::Vec;
-
-// deduct one for the null terminator.
-pub const DEVICE_NAME_MAX_LEN: usize = bitbox02_sys::MEMORY_DEVICE_NAME_MAX_LEN as usize - 1;
-
-// deduct one for the null terminator.
-pub const MULTISIG_NAME_MAX_LEN: usize = bitbox02_sys::MEMORY_MULTISIG_NAME_MAX_LEN as usize - 1;
+use bitbox_hal::memory::{DEVICE_NAME_MAX_LEN, MULTISIG_NAME_MAX_LEN};
pub use bitbox02_sys::memory_ble_metadata_t as BleMetadata;
diff --git a/src/ui/components/lockscreen.c b/src/ui/components/lockscreen.c
index 521dc9a..bb11b50 100644
--- a/src/ui/components/lockscreen.c
+++ b/src/ui/components/lockscreen.c
@@ -6,6 +6,7 @@
#include <hardfault.h>
#include <memory/memory.h>
+#include <rust/rust.h>
#include <screen.h>
#include <string.h>
#include <touch/gestures.h>
@@ -47,7 +48,7 @@ static void _truncate_to_fit(
// Name fits without truncation.
if (width <= max_width) {
- snprintf(out, MEMORY_DEVICE_NAME_MAX_LEN, "%s", in);
+ snprintf(out, MEMORY_DEVICE_MAX_LEN_WITH_NULL, "%s", in);
return;
}
@@ -75,14 +76,14 @@ component_t* lockscreen_create(void)
const UG_FONT* device_name_font = &font_font_a_9X9;
- char device_name[MEMORY_DEVICE_NAME_MAX_LEN] = {0};
+ char device_name[MEMORY_DEVICE_MAX_LEN_WITH_NULL] = {0};
memory_get_device_name(device_name);
// Show nothing if the name is the default name.
if (STREQ(device_name, MEMORY_DEFAULT_DEVICE_NAME)) {
device_name[0] = 0;
}
- char display_name[MEMORY_DEVICE_NAME_MAX_LEN + 3] = {0};
+ char display_name[MEMORY_DEVICE_MAX_LEN_WITH_NULL + 3] = {0};
_truncate_to_fit(
device_name,
display_name,
diff --git a/test/unit-test/test_memory.c b/test/unit-test/test_memory.c
index 3e59bd1..d947e13 100644
--- a/test/unit-test/test_memory.c
+++ b/test/unit-test/test_memory.c
@@ -7,6 +7,7 @@
#include <memory/memory.h>
#include <memory/memory_shared.h>
+#include <rust/rust.h>
#include <stdint.h>
#include <stdio.h>
@@ -31,7 +32,8 @@ static const int _addr_noise_static_private_key = 4;
static const int _addr_noise_remote_static_pubkeys = _addr_noise_static_private_key + 32;
static const int _addr_salt_root = _addr_noise_remote_static_pubkeys + 5 * NOISE_PUBKEY_SIZE;
static const int _addr_device_name = _addr_salt_root + 32;
-static const int _addr_seed_birthdate = _addr_device_name + MEMORY_DEVICE_NAME_MAX_LEN + 1 + 96;
+static const int _addr_seed_birthdate =
+ _addr_device_name + MEMORY_DEVICE_MAX_LEN_WITH_NULL + 1 + 96;
static const uint8_t _bitmask_seeded = (1 << 0);
static const uint8_t _bitmask_initialized = (1 << 1);
static const uint8_t _bitmask_mnemonic_passphrase_enabled = (1 << 2);
@@ -430,7 +432,7 @@ static void _test_memory_reset_hww_ble(void** state)
static void _test_memory_get_device_name_default(void** state)
{
- char name_out[MEMORY_DEVICE_NAME_MAX_LEN] = {0};
+ char name_out[MEMORY_DEVICE_MAX_LEN_WITH_NULL] = {0};
EMPTYCHUNK(empty_chunk);
expect_value(__wrap_memory_read_chunk_fake, chunk_num, 1);
will_return(__wrap_memory_read_chunk_fake, empty_chunk);
@@ -450,7 +452,7 @@ static void _test_memory_get_device_name_default_bluetooth(void** state)
const uint8_t entropy_prefix[] = {0x00, 0x19, 0xFE, 0xFF};
memcpy(entropy, entropy_prefix, sizeof(entropy_prefix));
- char name_out[MEMORY_DEVICE_NAME_MAX_LEN] = {0};
+ char name_out[MEMORY_DEVICE_MAX_LEN_WITH_NULL] = {0};
EMPTYCHUNK(empty_chunk);
expect_value(__wrap_memory_read_chunk_fake, chunk_num, 1);
will_return(__wrap_memory_read_chunk_fake, empty_chunk);
@@ -476,11 +478,11 @@ static void _test_memory_get_device_name_default_bluetooth(void** state)
static void _test_memory_get_device_name_invalid(void** state)
{
- char name_out[MEMORY_DEVICE_NAME_MAX_LEN] = {0};
+ char name_out[MEMORY_DEVICE_MAX_LEN_WITH_NULL] = {0};
EMPTYCHUNK(chunk);
- memset(chunk + _addr_device_name, 0, MEMORY_DEVICE_NAME_MAX_LEN);
+ memset(chunk + _addr_device_name, 0, MEMORY_DEVICE_MAX_LEN_WITH_NULL);
const char* device_name = "Äxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxx 漢字xxxxxxxxxxxxxxxxx";
- snprintf((char*)chunk + _addr_device_name, MEMORY_DEVICE_NAME_MAX_LEN, "%s", device_name);
+ snprintf((char*)chunk + _addr_device_name, MEMORY_DEVICE_MAX_LEN_WITH_NULL, "%s", device_name);
EMPTYCHUNK(empty_shared_chunk);
will_return(__wrap_memory_read_shared_bootdata_fake, empty_shared_chunk);
@@ -493,11 +495,11 @@ static void _test_memory_get_device_name_invalid(void** state)
static void _test_memory_get_device_name(void** state)
{
- char name_out[MEMORY_DEVICE_NAME_MAX_LEN] = {0};
+ char name_out[MEMORY_DEVICE_MAX_LEN_WITH_NULL] = {0};
EMPTYCHUNK(chunk);
- memset(chunk + _addr_device_name, 0, MEMORY_DEVICE_NAME_MAX_LEN);
+ memset(chunk + _addr_device_name, 0, MEMORY_DEVICE_MAX_LEN_WITH_NULL);
const char* device_name = "foo bar";
- snprintf((char*)chunk + _addr_device_name, MEMORY_DEVICE_NAME_MAX_LEN, "%s", device_name);
+ snprintf((char*)chunk + _addr_device_name, MEMORY_DEVICE_MAX_LEN_WITH_NULL, "%s", device_name);
expect_value(__wrap_memory_read_chunk_fake, chunk_num, 1);
will_return(__wrap_memory_read_chunk_fake, chunk);
@@ -512,9 +514,12 @@ static void _set_device_name(const char* device_name)
will_return(__wrap_memory_read_chunk_fake, empty_chunk);
EMPTYCHUNK(expected_chunk);
- memset(expected_chunk + _addr_device_name, 0, MEMORY_DEVICE_NAME_MAX_LEN);
+ memset(expected_chunk + _addr_device_name, 0, MEMORY_DEVICE_MAX_LEN_WITH_NULL);
snprintf(
- (char*)expected_chunk + _addr_device_name, MEMORY_DEVICE_NAME_MAX_LEN, "%s", device_name);
+ (char*)expected_chunk + _addr_device_name,
+ MEMORY_DEVICE_MAX_LEN_WITH_NULL,
+ "%s",
+ device_name);
expect_value(__wrap_memory_write_chunk_fake, chunk_num, 1);
expect_memory(__wrap_memory_write_chunk_fake, chunk, expected_chunk, CHUNK_SIZE);
will_return(__wrap_memory_write_chunk_fake, true);
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.