Skip to content

Commit 14aae27

Browse files
Merge remote-tracking branch 'origin' into feat/telemetry-span-kind
2 parents 1361a8a + 046187b commit 14aae27

10 files changed

Lines changed: 193 additions & 26 deletions

go.mod

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,11 @@ require (
5050
go.uber.org/atomic v1.11.0
5151
go.uber.org/goleak v1.3.0
5252
go.uber.org/zap v1.27.0
53-
golang.org/x/crypto v0.49.0
54-
golang.org/x/net v0.52.0
53+
golang.org/x/crypto v0.51.0
54+
golang.org/x/net v0.55.0
5555
golang.org/x/sync v0.20.0
56-
golang.org/x/sys v0.42.0
57-
golang.org/x/term v0.41.0
56+
golang.org/x/sys v0.45.0
57+
golang.org/x/term v0.43.0
5858
golang.org/x/time v0.12.0
5959
google.golang.org/grpc v1.80.0
6060
gopkg.in/yaml.v2 v2.4.0
@@ -189,10 +189,10 @@ require (
189189
go.uber.org/multierr v1.11.0 // indirect
190190
go.uber.org/zap/exp v0.3.0 // indirect
191191
golang.org/x/exp v0.0.0-20250606033433-dcc06ee1d476 // indirect
192-
golang.org/x/mod v0.33.0 // indirect
193-
golang.org/x/telemetry v0.0.0-20260209163413-e7419c687ee4 // indirect
194-
golang.org/x/text v0.35.0 // indirect
195-
golang.org/x/tools v0.42.0 // indirect
192+
golang.org/x/mod v0.35.0 // indirect
193+
golang.org/x/telemetry v0.0.0-20260409153401-be6f6cb8b1fa // indirect
194+
golang.org/x/text v0.37.0 // indirect
195+
golang.org/x/tools v0.44.0 // indirect
196196
google.golang.org/genproto/googleapis/api v0.0.0-20260504160031-60b97b32f348 // indirect
197197
google.golang.org/genproto/googleapis/rpc v0.0.0-20260504160031-60b97b32f348 // indirect
198198
google.golang.org/protobuf v1.36.11 // indirect

go.sum

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1080,8 +1080,8 @@ golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPh
10801080
golang.org/x/crypto v0.0.0-20210220033148-5ea612d1eb83/go.mod h1:jdWPYTVW3xRLrWPugEBEK3UY2ZEsg3UU495nc5E+M+I=
10811081
golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4=
10821082
golang.org/x/crypto v0.0.0-20210616213533-5ff15b29337e/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
1083-
golang.org/x/crypto v0.49.0 h1:+Ng2ULVvLHnJ/ZFEq4KdcDd/cfjrrjjNSXNzxg0Y4U4=
1084-
golang.org/x/crypto v0.49.0/go.mod h1:ErX4dUh2UM+CFYiXZRTcMpEcN8b/1gxEuv3nODoYtCA=
1083+
golang.org/x/crypto v0.51.0 h1:IBPXwPfKxY7cWQZ38ZCIRPI50YLeevDLlLnyC5wRGTI=
1084+
golang.org/x/crypto v0.51.0/go.mod h1:8AdwkbraGNABw2kOX6YFPs3WM22XqI4EXEd8g+x7Oc8=
10851085
golang.org/x/exp v0.0.0-20180321215751-8460e604b9de/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
10861086
golang.org/x/exp v0.0.0-20180807140117-3d87b88a115f/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
10871087
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
@@ -1120,8 +1120,8 @@ golang.org/x/mod v0.1.1-0.20191107180719-034126e5016b/go.mod h1:QqPTAvyqsEbceGzB
11201120
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
11211121
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
11221122
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
1123-
golang.org/x/mod v0.33.0 h1:tHFzIWbBifEmbwtGz65eaWyGiGZatSrT9prnU8DbVL8=
1124-
golang.org/x/mod v0.33.0/go.mod h1:swjeQEj+6r7fODbD2cqrnje9PnziFuw4bmLbBZFrQ5w=
1123+
golang.org/x/mod v0.35.0 h1:Ww1D637e6Pg+Zb2KrWfHQUnH2dQRLBQyAtpr/haaJeM=
1124+
golang.org/x/mod v0.35.0/go.mod h1:+GwiRhIInF8wPm+4AoT6L0FA1QWAad3OMdTRx4tFYlU=
11251125
golang.org/x/net v0.0.0-20180719180050-a680a1efc54d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
11261126
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
11271127
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
@@ -1172,8 +1172,8 @@ golang.org/x/net v0.0.0-20210525063256-abc453219eb5/go.mod h1:9nx3DQGgdP8bBQD5qx
11721172
golang.org/x/net v0.0.0-20210614182718-04defd469f4e/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
11731173
golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk=
11741174
golang.org/x/net v0.0.0-20220225172249-27dd8689420f/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk=
1175-
golang.org/x/net v0.52.0 h1:He/TN1l0e4mmR3QqHMT2Xab3Aj3L9qjbhRm78/6jrW0=
1176-
golang.org/x/net v0.52.0/go.mod h1:R1MAz7uMZxVMualyPXb+VaqGSa3LIaUqk0eEt3w36Sw=
1175+
golang.org/x/net v0.55.0 h1:bcvxaJn3e1U6InsFWt1JUq1aSjnRxLzT2rtD2KfkDF8=
1176+
golang.org/x/net v0.55.0/go.mod h1:L5U2KuzuOe1lY7Z+aWVIKK6qEeJXnXV9yzGA+WCHJww=
11771177
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
11781178
golang.org/x/oauth2 v0.0.0-20181017192945-9dcd33a902f4/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
11791179
golang.org/x/oauth2 v0.0.0-20181203162652-d668ce993890/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
@@ -1270,15 +1270,15 @@ golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
12701270
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
12711271
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
12721272
golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
1273-
golang.org/x/sys v0.42.0 h1:omrd2nAlyT5ESRdCLYdm3+fMfNFE/+Rf4bDIQImRJeo=
1274-
golang.org/x/sys v0.42.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw=
1275-
golang.org/x/telemetry v0.0.0-20260209163413-e7419c687ee4 h1:bTLqdHv7xrGlFbvf5/TXNxy/iUwwdkjhqQTJDjW7aj0=
1276-
golang.org/x/telemetry v0.0.0-20260209163413-e7419c687ee4/go.mod h1:g5NllXBEermZrmR51cJDQxmJUHUOfRAaNyWBM+R+548=
1273+
golang.org/x/sys v0.45.0 h1:dO4czNzziLiiXplLQgBCEpCvXQ3dnkn0SdaZSYdQ+FY=
1274+
golang.org/x/sys v0.45.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw=
1275+
golang.org/x/telemetry v0.0.0-20260409153401-be6f6cb8b1fa h1:efT73AJZfAAUV7SOip6pWGkwJDzIGiKBZGVzHYa+ve4=
1276+
golang.org/x/telemetry v0.0.0-20260409153401-be6f6cb8b1fa/go.mod h1:kHjTxDEnAu6/Nl9lDkzjWpR+bmKfxeiRuSDlsMb70gE=
12771277
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
12781278
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
12791279
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
1280-
golang.org/x/term v0.41.0 h1:QCgPso/Q3RTJx2Th4bDLqML4W6iJiaXFq2/ftQF13YU=
1281-
golang.org/x/term v0.41.0/go.mod h1:3pfBgksrReYfZ5lvYM0kSO0LIkAl4Yl2bXOkKP7Ec2A=
1280+
golang.org/x/term v0.43.0 h1:S4RLU2sB31O/NCl+zFN9Aru9A/Cq2aqKpTZJ6B+DwT4=
1281+
golang.org/x/term v0.43.0/go.mod h1:lrhlHNdQJHO+1qVYiHfFKVuVioJIheAc3fBSMFYEIsk=
12821282
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
12831283
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
12841284
golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
@@ -1287,8 +1287,8 @@ golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
12871287
golang.org/x/text v0.3.4/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
12881288
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
12891289
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
1290-
golang.org/x/text v0.35.0 h1:JOVx6vVDFokkpaq1AEptVzLTpDe9KGpj5tR4/X+ybL8=
1291-
golang.org/x/text v0.35.0/go.mod h1:khi/HExzZJ2pGnjenulevKNX1W67CUy0AsXcNubPGCA=
1290+
golang.org/x/text v0.37.0 h1:Cqjiwd9eSg8e0QAkyCaQTNHFIIzWtidPahFWR83rTrc=
1291+
golang.org/x/text v0.37.0/go.mod h1:a5sjxXGs9hsn/AJVwuElvCAo9v8QYLzvavO5z2PiM38=
12921292
golang.org/x/time v0.0.0-20180412165947-fbb02b2291d2/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
12931293
golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
12941294
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
@@ -1348,8 +1348,8 @@ golang.org/x/tools v0.0.0-20200804011535-6c149bb5ef0d/go.mod h1:njjCfa9FT2d7l9Bc
13481348
golang.org/x/tools v0.0.0-20200825202427-b303f430e36d/go.mod h1:njjCfa9FT2d7l9Bc6FUM5FLjQPp3cFF28FI3qnDFljA=
13491349
golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
13501350
golang.org/x/tools v0.1.0/go.mod h1:xkSsbof2nBLbhDlRMhhhyNLN/zl3eTqcnHD5viDpcZ0=
1351-
golang.org/x/tools v0.42.0 h1:uNgphsn75Tdz5Ji2q36v/nsFSfR/9BRFvqhGBaJGd5k=
1352-
golang.org/x/tools v0.42.0/go.mod h1:Ma6lCIwGZvHK6XtgbswSoWroEkhugApmsXyrUmBhfr0=
1351+
golang.org/x/tools v0.44.0 h1:UP4ajHPIcuMjT1GqzDWRlalUEoY+uzoZKnhOjbIPD2c=
1352+
golang.org/x/tools v0.44.0/go.mod h1:KA0AfVErSdxRZIsOVipbv3rQhVXTnlU6UhKxHd1seDI=
13531353
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
13541354
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
13551355
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=

pkg/file/joiner/bounds_test.go

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
// Copyright 2026 The Swarm Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package joiner_test
6+
7+
import (
8+
"context"
9+
"encoding/base64"
10+
"encoding/hex"
11+
"errors"
12+
"io"
13+
"os"
14+
"path/filepath"
15+
"testing"
16+
"time"
17+
18+
"github.com/ethersphere/bee/v2/pkg/file/joiner"
19+
"github.com/ethersphere/bee/v2/pkg/file/redundancy"
20+
"github.com/ethersphere/bee/v2/pkg/storage/inmemchunkstore"
21+
"github.com/ethersphere/bee/v2/pkg/swarm"
22+
"github.com/ethersphere/langos"
23+
)
24+
25+
// TestJoinerBounds reproduces the joiner panic and timeout against the
26+
// actual malformed sub-reference of the tree, read the way the /bzz
27+
// file-serving path reads it (through langos look-ahead) rather than by
28+
// sequentially reading the root.
29+
//
30+
// Chunk1 is the tree root. TestJoinerCrashTest joins Chunk1 directly, but the
31+
// root advertises a span (352) smaller than its own chunk data, so the joiner
32+
// treats it as a single leaf, copies 352 bytes and stops without ever
33+
// descending into the tree - hence no panic there.
34+
//
35+
// The /bzz endpoint instead resolves Chunk1 as a manifest and ends up serving
36+
// the child reference Chunk2, whose header advertises a span (524288 bytes) far
37+
// larger than the data actually present in the tree. Serving it wraps the
38+
// joiner in langos, which reads from offset 0 while peeking the next buffer at
39+
// offset smallFileBufferSize.
40+
//
41+
// Root Cause Details:
42+
// 1. In TestJoinerBounds/langos crash (with langos), the peek's ReadAt at offset 262144
43+
// descended into the first child chunk Chunk1. The parent's layout expected Chunk1's
44+
// subtree to cover a section size of 438272 bytes, but Chunk1 is actually a leaf
45+
// chunk of only 4096 bytes. Because the joiner did not validate that the child's
46+
// actual span matches the parent's layout expectation, it descended into Chunk1
47+
// with off=262144, cur=0. Since Chunk1's span (4096) <= len(data) (4096), it was
48+
// treated as a leaf, calculating dataOffsetStart = off - cur = 262144, and
49+
// sliced the data out of bounds, crashing the node.
50+
// 2. In TestJoinerBounds/timeout (without langos), reading sequentially from offset 0
51+
// successfully read the first 4096 bytes from Chunk1. The next read at offset 4096
52+
// descended into Chunk1 again (expecting it to cover up to 438272 bytes). This
53+
// calculated dataOffsetStart = 4096, which resulted in copying 0 bytes and
54+
// returning 0, nil. io.Copy kept retrying indefinitely, causing the hang.
55+
// 3. Note that langos itself functions correctly. Since the root chunk Chunk2
56+
// advertises a span of 524288 bytes, langos is entirely justified in issuing a
57+
// read/peek at offset 262144 (which is less than the advertised size). The joiner,
58+
// as an io.Reader/io.ReaderAt implementation, must return an error when queried
59+
// past the actual tree boundaries rather than panicking or hanging.
60+
//
61+
// The joiner enforces bounds check limits on leaf chunk reads, returning
62+
// ErrMalformedTrie immediately if the offset is invalid.
63+
func TestJoinerBounds(t *testing.T) {
64+
t.Run("langos crash", func(t *testing.T) {
65+
t.Parallel()
66+
testJoinerBug(t, true)
67+
})
68+
69+
t.Run("timeout", func(t *testing.T) {
70+
t.Parallel()
71+
testJoinerBug(t, false)
72+
})
73+
}
74+
75+
func testJoinerBug(t *testing.T, useLangos bool) {
76+
t.Helper()
77+
78+
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
79+
defer cancel()
80+
81+
store := inmemchunkstore.New()
82+
addrs := []string{
83+
"324881a5e0980e21eec6ab4348767a322260b4d477198aeb2500d9d492a99518", // Chunk1
84+
"7c512937c1aac8d772cff0d071cbdcda15748c9d422904007342a30dd58ef05d", // Chunk2
85+
"147a4a13002cfe9033b04bbf64b4de8091645930fa58f170fa146b0a7e0d7695", // Chunk3
86+
"6650507776a544842c632ed1dde92f8a456a88495308349f147472de3c1620f8", // Chunk4
87+
"8504f2a107ca940beafc4ce2f6c9a9f0968c62a5b5893ff0e4e1e2983048d276", // Chunk5
88+
"95378adf59567d5db28e970fc60d2dcf0568f11804fd8aa27d2ee7c9eac24100", // Chunk6
89+
}
90+
91+
for _, addrStr := range addrs {
92+
b64Data, err := os.ReadFile(filepath.Join("testdata/bounds", addrStr+".b64"))
93+
if err != nil {
94+
t.Fatal(err)
95+
}
96+
data, err := base64.StdEncoding.DecodeString(string(b64Data))
97+
if err != nil {
98+
t.Fatal(err)
99+
}
100+
addr, err := hex.DecodeString(addrStr)
101+
if err != nil {
102+
t.Fatal(err)
103+
}
104+
if err := store.Put(ctx, swarm.NewChunk(swarm.NewAddress(addr), data)); err != nil {
105+
t.Fatal(err)
106+
}
107+
}
108+
109+
// Join the malformed child reference directly, as /bzz does after resolving
110+
// the manifest at Chunk1.
111+
ref := swarm.MustParseHexAddress("7c512937c1aac8d772cff0d071cbdcda15748c9d422904007342a30dd58ef05d")
112+
reader, _, err := joiner.New(ctx, store, store, ref, redundancy.DefaultDownloadLevel)
113+
if err != nil {
114+
t.Fatal(err)
115+
}
116+
117+
if useLangos {
118+
// smallFileBufferSize mirrors the lookahead buffer size that pkg/api uses when
119+
// serving sub-10MB files through langos (see lookaheadBufferSize in bzz.go).
120+
// langos peeks one buffer ahead, i.e. while serving from offset 0 it also issues
121+
// a ReadAt at this offset.
122+
const smallFileBufferSize = 8 * 32 * 1024 // 262144
123+
124+
// Serve it exactly like the API does: wrap the joiner in langos and read it
125+
// from the start. langos' look-ahead peek issues the panicking ReadAt at
126+
// offset smallFileBufferSize. Once the joiner rejects such malformed trees
127+
// this should instead return an error here.
128+
lr := langos.NewBufferedLangos(reader, smallFileBufferSize)
129+
if _, err := io.Copy(io.Discard, lr); err == nil {
130+
t.Fatal("expected error, got nil")
131+
} else if !errors.Is(err, joiner.ErrMalformedTrie) {
132+
t.Fatalf("expected ErrMalformedTrie, got: %v", err)
133+
}
134+
} else {
135+
// Sequentially read the entire file. Without the leaf bounds check,
136+
// this path would hang indefinitely (reproducing the timeout) because
137+
// it kept retrying to read 0 bytes from the leaf chunk.
138+
if _, err := io.Copy(io.Discard, reader); err == nil {
139+
t.Fatal("expected error, got nil")
140+
} else if !errors.Is(err, joiner.ErrMalformedTrie) {
141+
t.Fatalf("expected ErrMalformedTrie, got: %v", err)
142+
}
143+
}
144+
}

pkg/file/joiner/joiner.go

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,15 +231,32 @@ func (j *joiner) readAtOffset(
231231
parity int,
232232
eg *errgroup.Group,
233233
) {
234+
dataLen := int64(len(data))
234235
// we are at a leaf data chunk
235-
if subTrieSize <= int64(len(data)) {
236+
if subTrieSize <= dataLen {
236237
dataOffsetStart := off - cur
238+
// Ensure that the read offset is within the bounds of this leaf chunk.
239+
// A malformed tree might advertise a larger span, leading to an out-of-bounds start offset.
240+
if dataOffsetStart < 0 || dataOffsetStart >= dataLen {
241+
eg.Go(func() error {
242+
return ErrMalformedTrie
243+
})
244+
return
245+
}
237246
dataOffsetEnd := dataOffsetStart + bytesToRead
238247

239-
if lenDataToCopy := int64(len(data)) - dataOffsetStart; bytesToRead > lenDataToCopy {
248+
if lenDataToCopy := dataLen - dataOffsetStart; bytesToRead > lenDataToCopy {
240249
dataOffsetEnd = dataOffsetStart + lenDataToCopy
241250
}
242251

252+
// Guard against slicing out-of-bounds if the computed end offset is invalid.
253+
if dataOffsetEnd < dataOffsetStart || dataOffsetEnd > dataLen {
254+
eg.Go(func() error {
255+
return ErrMalformedTrie
256+
})
257+
return
258+
}
259+
243260
bs := data[dataOffsetStart:dataOffsetEnd]
244261
n := copy(b[bufferOffset:bufferOffset+int64(len(bs))], bs)
245262
atomic.AddInt64(bytesRead, int64(n))

0 commit comments

Comments
 (0)