Skip to content

Commit

Permalink
osrelease: handle completely empty keys
Browse files Browse the repository at this point in the history
We hadn't run into this before. This seems like the sensible action to
take when there's no value.

Signed-off-by: Hank Donnay <[email protected]>
  • Loading branch information
hdonnay committed Jan 2, 2025
1 parent e03213f commit 6bbe356
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 7 deletions.
15 changes: 8 additions & 7 deletions osrelease/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,13 +178,13 @@ func Parse(ctx context.Context, r io.Reader) (map[string]string, error) {
case b[0] == '#':
continue
}
eq := bytes.IndexRune(b, '=')
if eq == -1 {
if !bytes.ContainsRune(b, '=') {
return nil, fmt.Errorf("osrelease: malformed line %q", s.Text())
}
// Also handling spaces here matches what systemd seems to do.
key := strings.TrimSpace(string(b[:eq]))
value := strings.TrimSpace(string(b[eq+1:]))
key, value, _ := strings.Cut(string(b), "=")
key = strings.TrimSpace(key)
value = strings.TrimSpace(value)

// The value side is defined to follow shell-like quoting rules, which I
// take to mean:
Expand All @@ -201,11 +201,12 @@ func Parse(ctx context.Context, r io.Reader) (map[string]string, error) {
//
// With these in mind, the arms of the switch below implement the first
// case and a limited version of the second.
switch value[0] {
case '\'':
switch {
case len(value) == 0:
case value[0] == '\'':
value = strings.TrimFunc(value, func(r rune) bool { return r == '\'' })
value = strings.ReplaceAll(value, `'\''`, `'`)
case '"':
case value[0] == '"':
// This only implements the metacharacters that are called out in
// the os-release documentation.
value = strings.TrimFunc(value, func(r rune) bool { return r == '"' })
Expand Down
35 changes: 35 additions & 0 deletions osrelease/testdata/EmptyKey.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
-- os-release --
NAME=Fedora
VERSION="30 (Container Image)"
ID=fedora
VERSION_ID=30
VERSION_CODENAME=
PLATFORM_ID="platform:f30"
PRETTY_NAME="Fedora 30 (Container Image)"
LOGO=fedora-logo-icon
CPE_NAME="cpe:/o:fedoraproject:fedora:30"
VARIANT="Container Image"
VARIANT_ID=container
-- Parsed --
{
"CPE_NAME": "cpe:/o:fedoraproject:fedora:30",
"ID": "fedora",
"LOGO": "fedora-logo-icon",
"NAME": "Fedora",
"PLATFORM_ID": "platform:f30",
"PRETTY_NAME": "Fedora 30 (Container Image)",
"VARIANT_ID": "container",
"VARIANT": "Container Image",
"VERSION": "30 (Container Image)",
"VERSION_CODENAME": "",
"VERSION_ID": "30"
}
-- Distribution --
{
"cpe": "cpe:/o:fedoraproject:fedora:30",
"did": "fedora",
"name": "Fedora",
"pretty_name": "Fedora 30 (Container Image)",
"version": "30 (Container Image)",
"version_id": "30"
}

0 comments on commit 6bbe356

Please sign in to comment.