refactor(core): use mp_obj_new_str_from_cstr
What changed, and why it matters
This commit is a simple code cleanup that replaces a common MicroPython string-creation pattern with a new helper function. It does not change what the code does, only how it is written. There is no security issue visible in the change.
No action required. Treat as routine refactoring.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The commit refactors three call sites to use the new MicroPython helper mp_obj_new_str_from_cstr(s) instead of mp_obj_new_str(s, strlen(s)). The helper is functionally equivalent: it creates a MicroPython string object from a NUL-terminated C string. The change is purely stylistic and reduces boilerplate. No behavior, length handling, memory allocation, or trust boundary changes.
Changed components
core/embed/upymod/modtrezorcrypto/modtrezorcrypto-bech32.hcore/embed/upymod/modtrezorcrypto/modtrezorcrypto-nem.hcore/embed/upymod/modtrezorio/modtrezorio-fatfs.hInspect captured patch +3 / −3
diff --git a/core/embed/upymod/modtrezorcrypto/modtrezorcrypto-bech32.h b/core/embed/upymod/modtrezorcrypto/modtrezorcrypto-bech32.h
index a57e2a78..ab4d569d 100644
--- a/core/embed/upymod/modtrezorcrypto/modtrezorcrypto-bech32.h
+++ b/core/embed/upymod/modtrezorcrypto/modtrezorcrypto-bech32.h
@@ -63,7 +63,7 @@ STATIC mp_obj_t mod_trezorcrypto_bech32_decode(size_t n_args,
}
mp_obj_tuple_t *tuple = MP_OBJ_TO_PTR(mp_obj_new_tuple(3, NULL));
- tuple->items[0] = mp_obj_new_str(hrp, strlen(hrp));
+ tuple->items[0] = mp_obj_new_str_from_cstr(hrp);
tuple->items[1] = data_list;
tuple->items[2] = MP_OBJ_NEW_SMALL_INT(enc);
return MP_OBJ_FROM_PTR(tuple);
diff --git a/core/embed/upymod/modtrezorcrypto/modtrezorcrypto-nem.h b/core/embed/upymod/modtrezorcrypto/modtrezorcrypto-nem.h
index 128c89fa..7e7c4265 100644
--- a/core/embed/upymod/modtrezorcrypto/modtrezorcrypto-nem.h
+++ b/core/embed/upymod/modtrezorcrypto/modtrezorcrypto-nem.h
@@ -56,7 +56,7 @@ STATIC mp_obj_t mod_trezorcrypto_nem_compute_address(mp_obj_t public_key,
mp_raise_ValueError(MP_ERROR_TEXT(
"Failed to compute a NEM address from provided public key"));
}
- return mp_obj_new_str(address, strlen(address));
+ return mp_obj_new_str_from_cstr(address);
}
STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_trezorcrypto_nem_compute_address_obj,
mod_trezorcrypto_nem_compute_address);
diff --git a/core/embed/upymod/modtrezorio/modtrezorio-fatfs.h b/core/embed/upymod/modtrezorio/modtrezorio-fatfs.h
index 3efa1b33..a75b8e2d 100644
--- a/core/embed/upymod/modtrezorio/modtrezorio-fatfs.h
+++ b/core/embed/upymod/modtrezorio/modtrezorio-fatfs.h
@@ -149,7 +149,7 @@ STATIC mp_obj_t filinfo_to_tuple(const FILINFO *info) {
attrs[4] = 'a';
}
tuple->items[1] = mp_obj_new_str(attrs, 5);
- tuple->items[2] = mp_obj_new_str(info->fname, strlen(info->fname));
+ tuple->items[2] = mp_obj_new_str_from_cstr(info->fname);
return MP_OBJ_FROM_PTR(tuple);
}
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.