Skip to content

Commit 64ad750

Browse files
committed
fix(sys): formatting of escaped bytes when width is specified
fmt::LowerHex was applying the inherited Formatter options, resulting in the following incorrect output: ``` assertion `left == right` failed left: "\\x f0\\x 90\\x 80" right: "\\xf0\\x90\\x80" ```
1 parent 47c4b1d commit 64ad750

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

nginx-sys/src/lib.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,8 +198,7 @@ impl fmt::Display for ngx_str_t {
198198
for chunk in self.as_bytes().utf8_chunks() {
199199
f.write_str(chunk.valid())?;
200200
for byte in chunk.invalid() {
201-
f.write_str("\\x")?;
202-
fmt::LowerHex::fmt(byte, f)?;
201+
write!(f, "\\x{byte:02x}")?;
203202
}
204203
}
205204
Ok(())
@@ -318,6 +317,7 @@ pub unsafe fn add_to_ngx_table(
318317
#[cfg(test)]
319318
mod tests {
320319
extern crate alloc;
320+
use alloc::format;
321321
use alloc::string::ToString;
322322

323323
use super::*;
@@ -340,5 +340,14 @@ mod tests {
340340
};
341341
assert_eq!(str.to_string(), *expected);
342342
}
343+
344+
// Check that the formatter arguments are ignored correctly
345+
for (bytes, expected) in &pairs[2..3] {
346+
let str = ngx_str_t {
347+
data: bytes.as_ptr().cast_mut(),
348+
len: bytes.len(),
349+
};
350+
assert_eq!(format!("{str:12.12}"), *expected);
351+
}
343352
}
344353
}

0 commit comments

Comments
 (0)