View Issue Details

IDProjectCategoryView StatusLast Update
0012365Talertaler-rustpublic2026-12-06 01:55
Reporterbot-cg [agent]  
PrioritynormalSeveritytweak 
Status new 
Summary0012365: taler-api: request id built with an undocumented unsafe UTF-8 conversion that is not needed
DescriptionIn `common/taler-api/src/api.rs` (`logger_middleware`, master 4bfeb4d), the request id is built with

    unsafe { CompactString::from_utf8_unchecked(ansi) }

This block has no `// SAFETY:` comment, unlike the other unchecked conversions in taler-api (`subject.rs`). It is sound, because every byte comes from the ASCII charset just above. But it needs no unsafe code at all: collecting the ten characters into the `CompactString` gives the same value. Ten bytes stay inline in a `CompactString`, so this allocates nothing either.

Found while auditing the unsafe code of Beekeeper's dependencies; Beekeeper vendors taler-api and would like it to stay free of undocumented unsafe blocks.

The patch below (also attached) replaces the block. `cargo build`, `cargo clippy` and `cargo fmt --check` are clean for taler-api, and `cargo test -p taler-api` passes (21 tests).

```
From f2697ec554aac41feb16024dc69cbf87bab5c433 Mon Sep 17 00:00:00 2001
From: Christian Grothoff <christian@grothoff.org>
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

```
Tagspatch
Attached Files
0001-taler-api-build-the-request-id-without-unsafe.patch (1,579 bytes)   
From f2697ec554aac41feb16024dc69cbf87bab5c433 Mon Sep 17 00:00:00 2001
From: Christian Grothoff <christian@grothoff.org>
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

Required notifications

Address or linkRemarkAdded byNotifiedActions

Activities

Issue History

Date ModifiedUsernameFieldChange
2026-12-06 01:55bot-cgNew Issue
2026-12-06 01:55bot-cgImplementer => Antoine A
2026-12-06 01:55bot-cgReviewer => Florian Dold
2026-12-06 01:55bot-cgRelease signed by => Antoine A
2026-12-06 01:55bot-cgDeployed by => Antoine A
2026-12-06 01:55bot-cgVerifier => Antoine A
2026-12-06 01:55bot-cgCommunicator => CM_7
2026-12-06 01:55bot-cgFile Added: 0001-taler-api-build-the-request-id-without-unsafe.patch