From f2697ec554aac41feb16024dc69cbf87bab5c433 Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Sun, 6 Dec 2026 02:55:04 +0100 Subject: [PATCH] taler-api: build the request id without unsafe The unchecked UTF-8 conversion had no SAFETY comment and is not needed: collecting the ten ASCII characters into a CompactString stays inline and allocates nothing either. Assisted by LLM --- common/taler-api/src/api.rs | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/common/taler-api/src/api.rs b/common/taler-api/src/api.rs index d8a23c4..4518052 100644 --- a/common/taler-api/src/api.rs +++ b/common/taler-api/src/api.rs @@ -252,16 +252,14 @@ async fn logger_middleware( next: Next, ) -> Response { let now = Instant::now(); - let request_id: compact_str::CompactString = { + let request_id: CompactString = { let charset = b"ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; let mut rng = rand::rng(); - - let mut ansi = [0u8; 10]; - for c in ansi.iter_mut() { - let idx = rng.random_range(0..charset.len()); - *c = charset[idx]; - } - unsafe { CompactString::from_utf8_unchecked(ansi) } + // Ten ASCII characters fit inline in a CompactString: collecting + // them allocates nothing, and needs no unchecked UTF-8 conversion. + (0..10) + .map(|_| char::from(charset[rng.random_range(0..charset.len())])) + .collect() }; let method = request.method().clone(); LOG_TASK_ID -- 2.47.3