From 6aef900af4eeb4a35d3ff1237a41cf3f63c56c24 Mon Sep 17 00:00:00 2001 From: Amol Yadav Date: Wed, 28 Jan 2026 01:14:53 +0000 Subject: runtime/metrics: fix panic in Read with empty slice Calling Read with a nil or empty slice previously caused a panic with "index out of range" because the function unconditionally accessed the first element of the slice (via &m[0]) to pass the pointer to the runtime. This change adds a check for len(m) == 0 to return early, preventing the panic when no samples are provided. Fixes #77231 Change-Id: I442635f5c61de432883c8d0efae9cc6aa1363cc9 GitHub-Last-Rev: 6f24f67b18c77a0b36b92017a3f4ef8aa3aa5229 GitHub-Pull-Request: golang/go#77233 Reviewed-on: https://go-review.googlesource.com/c/go/+/737380 Reviewed-by: Amol Yadav Reviewed-by: Michael Pratt LUCI-TryBot-Result: Go LUCI Auto-Submit: Michael Pratt Commit-Queue: Michael Pratt Reviewed-by: Dmitri Shuralyov --- src/runtime/metrics/sample.go | 3 +++ 1 file changed, 3 insertions(+) (limited to 'src/runtime/metrics/sample.go') diff --git a/src/runtime/metrics/sample.go b/src/runtime/metrics/sample.go index 9efc5c5f06..df1341d48c 100644 --- a/src/runtime/metrics/sample.go +++ b/src/runtime/metrics/sample.go @@ -43,5 +43,8 @@ func runtime_readMetrics(unsafe.Pointer, int, int) // Sample values with names not appearing in [All] will have their Value populated // as KindBad to indicate that the name is unknown. func Read(m []Sample) { + if len(m) == 0 { + return + } runtime_readMetrics(unsafe.Pointer(&m[0]), len(m), cap(m)) } -- cgit v1.3