chore(core): replace `strings.trim_str()` by `utils.truncate_utf8()`
What changed, and why it matters
This is a routine code cleanup: Trezor replaced one internal helper function that shortens text strings with another existing helper that does the same job. No security bug is fixed or introduced in the visible change. The old and new functions appear to behave the same way for normal Unicode text.
No security action required. Treat as normal refactoring.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The commit removes strings.trim_str() and switches the only caller, wire/thp/paired_cache.py, to utils.truncate_utf8(). Both functions truncate a Python str so its UTF-8 byte length does not exceed a given limit. The unit tests are moved from test_trezor.strings.py to test_trezor.utils.py with identical expectations. The diff shows no change in behavior, no bounds-check error, no memory-safety issue, and no security-relevant logic change.
Changed components
core/src/trezor/strings.pycore/src/trezor/wire/thp/paired_cache.pycore/tests/test_trezor.strings.pycore/tests/test_trezor.utils.pyInspect captured patch +23 / −37
diff --git a/core/src/trezor/strings.py b/core/src/trezor/strings.py
index 5fd07686..10cbcb22 100644
--- a/core/src/trezor/strings.py
+++ b/core/src/trezor/strings.py
@@ -147,16 +147,3 @@ def format_autolock_duration(auto_lock_ms: int) -> str:
auto_lock_label = TR.plurals__lock_after_x_seconds
return format_plural("{count} {plural}", auto_lock_num, auto_lock_label)
-
-
-def trim_str(s: str, max_bytes: int) -> str:
- """
- Trim a string, so the result's byte size will be less or equal to `max_bytes`.
- """
- assert max_bytes >= 0
- for i, char in enumerate(s):
- char_size = len(char.encode())
- if max_bytes < char_size:
- return s[:i]
- max_bytes -= char_size
- return s
diff --git a/core/src/trezor/wire/thp/paired_cache.py b/core/src/trezor/wire/thp/paired_cache.py
index 7b8eb3ab..2088f6c8 100644
--- a/core/src/trezor/wire/thp/paired_cache.py
+++ b/core/src/trezor/wire/thp/paired_cache.py
@@ -54,7 +54,7 @@ def cache_host_info(mac_addr: AnyBytes | None, host_name: str, app_name: str) ->
return
from trezor.messages import ThpPairedCacheEntry
- from trezor.strings import trim_str
+ from trezor.utils import truncate_utf8
entries = load()
for e in entries:
@@ -69,8 +69,8 @@ def cache_host_info(mac_addr: AnyBytes | None, host_name: str, app_name: str) ->
# skip writing to flash if this MAC address is already cached
return
- host_name = trim_str(host_name, max_bytes=32)
- app_name = trim_str(app_name, max_bytes=32)
+ host_name = truncate_utf8(host_name, max_bytes=32)
+ app_name = truncate_utf8(app_name, max_bytes=32)
entries.append(
ThpPairedCacheEntry(mac_addr=mac_addr, host_name=host_name, app_name=app_name)
)
diff --git a/core/tests/test_trezor.strings.py b/core/tests/test_trezor.strings.py
index 69d6ab15..721d491b 100644
--- a/core/tests/test_trezor.strings.py
+++ b/core/tests/test_trezor.strings.py
@@ -222,27 +222,6 @@ class TestStrings(unittest.TestCase):
strings.format_timestamp(1616057224)
- def test_trim_str(self):
- # test ASCII
- self.assertEqual(strings.trim_str("123", 4), "123")
- self.assertEqual(strings.trim_str("123", 3), "123")
- self.assertEqual(strings.trim_str("123", 2), "12")
- self.assertEqual(strings.trim_str("123", 1), "1")
- self.assertEqual(strings.trim_str("123", 0), "")
-
- # test non-ASCII
- self.assertEqual(strings.trim_str("➀➁➂", 10), "➀➁➂")
- self.assertEqual(strings.trim_str("➀➁➂", 9), "➀➁➂")
- self.assertEqual(strings.trim_str("➀➁➂", 8), "➀➁")
- self.assertEqual(strings.trim_str("➀➁➂", 7), "➀➁")
- self.assertEqual(strings.trim_str("➀➁➂", 6), "➀➁")
- self.assertEqual(strings.trim_str("➀➁➂", 5), "➀")
- self.assertEqual(strings.trim_str("➀➁➂", 4), "➀")
- self.assertEqual(strings.trim_str("➀➁➂", 3), "➀")
- self.assertEqual(strings.trim_str("➀➁➂", 2), "")
- self.assertEqual(strings.trim_str("➀➁➂", 1), "")
- self.assertEqual(strings.trim_str("➀➁➂", 0), "")
-
if __name__ == "__main__":
unittest.main()
diff --git a/core/tests/test_trezor.utils.py b/core/tests/test_trezor.utils.py
index 8582c691..c52fd99c 100644
--- a/core/tests/test_trezor.utils.py
+++ b/core/tests/test_trezor.utils.py
@@ -51,6 +51,26 @@ class TestUtils(unittest.TestCase):
utils.truncate_utf8("\u1234\u5678", 7), "\u1234\u5678"
) # b'\xe1\x88\xb4\xe5\x99\xb8
+ # test ASCII
+ self.assertEqual(utils.truncate_utf8("123", 4), "123")
+ self.assertEqual(utils.truncate_utf8("123", 3), "123")
+ self.assertEqual(utils.truncate_utf8("123", 2), "12")
+ self.assertEqual(utils.truncate_utf8("123", 1), "1")
+ self.assertEqual(utils.truncate_utf8("123", 0), "")
+
+ # test non-ASCII
+ self.assertEqual(utils.truncate_utf8("➀➁➂", 10), "➀➁➂")
+ self.assertEqual(utils.truncate_utf8("➀➁➂", 9), "➀➁➂")
+ self.assertEqual(utils.truncate_utf8("➀➁➂", 8), "➀➁")
+ self.assertEqual(utils.truncate_utf8("➀➁➂", 7), "➀➁")
+ self.assertEqual(utils.truncate_utf8("➀➁➂", 6), "➀➁")
+ self.assertEqual(utils.truncate_utf8("➀➁➂", 5), "➀")
+ self.assertEqual(utils.truncate_utf8("➀➁➂", 4), "➀")
+ self.assertEqual(utils.truncate_utf8("➀➁➂", 3), "➀")
+ self.assertEqual(utils.truncate_utf8("➀➁➂", 2), "")
+ self.assertEqual(utils.truncate_utf8("➀➁➂", 1), "")
+ self.assertEqual(utils.truncate_utf8("➀➁➂", 0), "")
+
def test_firmware_hash(self):
if utils.INTERNAL_MODEL in ( # pylint: disable=internal-model-tuple-comparison
"D002",
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.