From b92a441b5cf57670a25ff9127ca71ee89fb0f0a3 Mon Sep 17 00:00:00 2001 From: Kacper Sawicki Date: Fri, 27 Feb 2026 14:01:37 +0000 Subject: [PATCH] fix: handle extra colons in /proc/self/cgroup entries (#34) Use strings.SplitN with limit of 3 instead of strings.Split so that extra colons in the cgroup path (e.g. from Kubernetes cri-containerd) are preserved in the path field rather than causing a parse error. Also fix nil error wrapping in the parse error message. --- cgroup.go | 4 ++-- cgroup_internal_test.go | 16 ++++++++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/cgroup.go b/cgroup.go index 6e4cc84..de06a05 100644 --- a/cgroup.go +++ b/cgroup.go @@ -128,9 +128,9 @@ func currentProcCgroup(fs afero.Fs) (string, error) { entries := strings.Split(strings.TrimSpace(string(data)), "\n") for _, entry := range entries { - parts := strings.Split(strings.TrimSpace(entry), ":") + parts := strings.SplitN(strings.TrimSpace(entry), ":", 3) if len(parts) != 3 { - return "", xerrors.Errorf("parse entry %v: %w", procSelfCgroup, err) + return "", xerrors.Errorf("parse entry %v: expected at least 3 colon-separated fields in %q", procSelfCgroup, entry) } if parts[0] == "0" { diff --git a/cgroup_internal_test.go b/cgroup_internal_test.go index ff3c0fd..8214c3d 100644 --- a/cgroup_internal_test.go +++ b/cgroup_internal_test.go @@ -54,6 +54,22 @@ func TestCurrentProcCgroup(t *testing.T) { 1:net_cls:/`, expectPath: "/init.slice", }, + { + name: "ExtraColonsInPath", + procFile: `0::/system.slice/kubepods-burstable.slice:cri-containerd:d24f9cc`, + expectPath: "/system.slice/kubepods-burstable.slice:cri-containerd:d24f9cc", + }, + { + name: "ExtraColonsInPathWithMixedHierarchy", + procFile: `1:net_cls:/init.slice +0::/system.slice/kubepods.slice:cri-containerd:abc123`, + expectPath: "/system.slice/kubepods.slice:cri-containerd:abc123", + }, + { + name: "MalformedEntryTooFewFields", + procFile: `0`, + expectError: "parse entry", + }, { name: "MissingHierarchy0", procFile: `1:net_cls:/`,