Skip to content

Commit 5fd9ea7

Browse files
committed
fix: avoid slice out of bounds index when server error body is <1024
Addresses the following panic: 2026/01/21 17:07:16 INFO ping... panic: runtime error: slice bounds out of range [:1024] with capacity 512 goroutine 1 [running]: main.ping(0xc000118e10, {0xc00001c017?, 0x9b69e0?}, {0xc00001e017, 0x4}) github.com/cofide/cofide-demos/workloads/ping-pong-mesh/ping-pong-mesh-client/main.go:72 +0x34c main.run(0xc00007c2e0) github.com/cofide/cofide-demos/workloads/ping-pong-mesh/ping-pong-mesh-client/main.go:47 +0xcd main.main() github.com/cofide/cofide-demos/workloads/ping-pong-mesh/ping-pong-mesh-client/main.go:15 +0x18
1 parent 9c8c0b7 commit 5fd9ea7

File tree

3 files changed

+9
-3
lines changed
  • workloads
    • ping-pong-cofide/ping-pong-cofide-client
    • ping-pong-mesh/ping-pong-mesh-client
    • ping-pong/ping-pong-client

3 files changed

+9
-3
lines changed

workloads/ping-pong-cofide/ping-pong-cofide-client/main.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,9 @@ func ping(client *cofidehttp.Client, serverAddr string, serverPort int) error {
129129
return err
130130
}
131131
if r.StatusCode != http.StatusOK {
132-
return fmt.Errorf("unexpected status code: %d: %s", r.StatusCode, body[:1024])
132+
// Limit how much of the response we include in the error
133+
body = body[:min(len(body), 1024)]
134+
return fmt.Errorf("unexpected status code: %d: %s", r.StatusCode, body)
133135
}
134136
slog.Info(string(body))
135137
return nil

workloads/ping-pong-mesh/ping-pong-mesh-client/main.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,9 @@ func ping(client *http.Client, serverAddr, serverPort string) error {
6969
return err
7070
}
7171
if r.StatusCode != http.StatusOK {
72-
return fmt.Errorf("unexpected status code: %d: %s", r.StatusCode, body[:1024])
72+
// Limit how much of the response we include in the error
73+
body = body[:min(len(body), 1024)]
74+
return fmt.Errorf("unexpected status code: %d: %s", r.StatusCode, body)
7375
}
7476
slog.Info(string(body))
7577
return nil

workloads/ping-pong/ping-pong-client/main.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,9 @@ func ping(client *http.Client, serverAddr string, serverPort int) error {
218218
return err
219219
}
220220
if r.StatusCode != http.StatusOK {
221-
return fmt.Errorf("unexpected status code: %d: %s", r.StatusCode, body[:1024])
221+
// Limit how much of the response we include in the error
222+
body = body[:min(len(body), 1024)]
223+
return fmt.Errorf("unexpected status code: %d: %s", r.StatusCode, body)
222224
}
223225
slog.Info(string(body))
224226
return nil

0 commit comments

Comments
 (0)