machine/usb: fix truncated USB string descriptor when host requests short maxLen (#5449)

* machine/usb: fix truncated USB string descriptor when host requests short maxLen
* fix binary size
This commit is contained in:
sago35
2026-06-07 20:32:52 +09:00
committed by GitHub
parent e6e7250a47
commit aef70a5839
2 changed files with 7 additions and 6 deletions
+1 -1
View File
@@ -44,7 +44,7 @@ func TestBinarySize(t *testing.T) {
// microcontrollers
{"hifive1b", "examples/echo", 3817, 299, 0, 2252},
{"microbit", "examples/serial", 2820, 356, 8, 2248},
{"wioterminal", "examples/pininterrupt", 7286, 1534, 120, 7248},
{"wioterminal", "examples/pininterrupt", 7330, 1550, 120, 7248},
// TODO: also check wasm. Right now this is difficult, because
// wasm binaries are run through wasm-opt and therefore the
+6 -5
View File
@@ -196,17 +196,18 @@ func sendDescriptorString(data string, maxLen uint16) {
// Clamp the length.
maxEncBytes := min(len(usb_trans_buffer), len(udd_ep_control_cache_buffer), int(maxLen))
data = data[:min(len(data), (maxEncBytes-2)/2)]
// Write the header.
buf := usb_trans_buffer[:2*len(data)+2]
buf := usb_trans_buffer[:min(2*len(data)+2, maxEncBytes)]
hdr, body := buf[:2], buf[2:]
hdr[0] = byte(len(buf))
// hdr[0] (bLength) should convey the "original total string length" before being limited by the host's maxLen.
hdr[0] = byte(2*len(data) + 2)
hdr[1] = descriptor.TypeString
// Convert the string to UTF16.
// NOTE: Using range here would cause the length to disagree when multibyte codepoints are present.
for i := 0; i < len(data); i++ {
limit := min(len(data), len(body)/2)
for i := 0; i < limit; i++ {
body[2*i] = byte(data[i])
body[2*i+1] = 0
}