Skip to content

fix(enumerator): treat an empty sysfs attribute as empty, not an error#220

Merged
cmaglie merged 1 commit into
bugst:masterfrom
ultramcu:fix/enumerator-empty-sysfs-attr
May 25, 2026
Merged

fix(enumerator): treat an empty sysfs attribute as empty, not an error#220
cmaglie merged 1 commit into
bugst:masterfrom
ultramcu:fix/enumerator-empty-sysfs-attr

Conversation

@ultramcu
Copy link
Copy Markdown
Contributor

On Linux, enumerator.readLine (enumerator/usb_linux.go) returns io.EOF when a USB sysfs attribute file exists but is empty (zero bytes). bufio.Reader.ReadLine() returns ("", io.EOF) for an empty file, and readLine passes that EOF through.

parseUSBSysFS reads idVendor, idProduct, serial, manufacturer and product as required fields and propagates the error, so a single USB device that exposes one of these attributes but leaves it blank (some devices ship with an empty serial/manufacturer/product string descriptor) makes the entire GetDetailedPortsList() call fail — no ports are returned at all.

An absent attribute is already handled gracefully (os.IsNotExist"", nil); an empty one was not.

Fix

Return ("", nil) when ReadLine reports io.EOF, treating an empty attribute the same as a missing one.

line, _, err := reader.ReadLine()
if err == io.EOF {
    return "", nil
}
return string(line), err

Tests

Added enumerator/usb_linux_test.go — all hardware-free (temp files, no real device/sysfs):

  • TestReadLineEmptyFile — empty file → "", not an error.
  • TestParseUSBSysFSEmptySerial — a device dir with a present-but-empty serial/manufacturer parses successfully (empty SerialNumber).
  • TestReadLineMissingFile / TestReadLineContent — regression guards (absent file, normal content with/without trailing newline).

Both new tests fail before the change (return/propagate io.EOF) and pass after. go test -race ./... stays green (verified on Linux).

readLine returned io.EOF for a present-but-empty USB sysfs attribute
file (serial/manufacturer/product). parseUSBSysFS reads those as
required fields, so a single device with a blank attribute made the
whole GetDetailedPortsList() fail. Return an empty string for an empty
file, mirroring how an absent attribute is already handled via
os.IsNotExist.
@cmaglie cmaglie merged commit 616b986 into bugst:master May 25, 2026
8 checks passed
@cmaglie
Copy link
Copy Markdown
Member

cmaglie commented May 25, 2026

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants