libct/specconv: fix panic in initSystemdProps

There is a chance of panic here -- eliminate it.

Add a test case (which panics before the fix).

Reported-by: Luke Hinds <luke@stacklok.com>
Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
(cherry picked from commit a48a7cef96)
Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
This commit is contained in:
Kir Kolyshkin
2026-02-26 11:28:13 -08:00
parent 168dcab909
commit 190a6c6e37
2 changed files with 6 additions and 1 deletions
+1 -1
View File
@@ -758,7 +758,7 @@ func initSystemdProps(spec *specs.Spec) ([]systemdDbus.Property, error) {
return nil, fmt.Errorf("annotation %s=%s value parse error: %w", k, v, err)
}
// Check for Sec suffix.
if trimName := strings.TrimSuffix(name, "Sec"); len(trimName) < len(name) {
if trimName, ok := strings.CutSuffix(name, "Sec"); ok && len(trimName) > 0 {
// Check for a lowercase ascii a-z just before Sec.
if ch := trimName[len(trimName)-1]; ch >= 'a' && ch <= 'z' {
// Convert from Sec to USec.
+5
View File
@@ -749,6 +749,11 @@ func TestInitSystemdProps(t *testing.T) {
in: inT{"org.systemd.property.FOOSec", "123"},
exp: expT{false, "FOOSec", 123},
},
{
desc: "convert USec to Sec (short name)",
in: inT{"org.systemd.property.Sec", "123"},
exp: expT{false, "Sec", 123},
},
{
desc: "CollectMode",
in: inT{"org.systemd.property.CollectMode", "'inactive-or-failed'"},