mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-01 19:58:15 +00:00
fix(go): add bounds checking to ByteVector (#8776)
Add missing bounds checking to ByteVector before slice operations in the Go FlatBuffers implementation. Relative offset and vector length are now checked against the buffer size. Instead of panicking, the code now returns nil. Regression test added. Signed-off-by: Ville Vesilehto <ville@vesilehto.fi> Co-authored-by: Justin Davis <jtdavis777@gmail.com>
This commit is contained in:
15
go/table.go
15
go/table.go
@@ -31,10 +31,25 @@ func (t *Table) String(off UOffsetT) string {
|
||||
}
|
||||
|
||||
// ByteVector gets a byte slice from data stored inside the flatbuffer.
|
||||
// If the offset is invalid or out of bounds, returns nil to prevent crashes.
|
||||
func (t *Table) ByteVector(off UOffsetT) []byte {
|
||||
n := UOffsetT(len(t.Bytes))
|
||||
// Need at least SizeUOffsetT bytes to read the relative vector offset.
|
||||
u := UOffsetT(SizeUOffsetT)
|
||||
if n < u || off > n-u {
|
||||
return nil
|
||||
}
|
||||
off += GetUOffsetT(t.Bytes[off:])
|
||||
// Need at least SizeUOffsetT bytes to read the vector length.
|
||||
if n < u || off > n-u {
|
||||
return nil
|
||||
}
|
||||
start := off + UOffsetT(SizeUOffsetT)
|
||||
length := GetUOffsetT(t.Bytes[off:])
|
||||
// Avoid overflow by checking the length against the remaining buffer space.
|
||||
if length > n-start {
|
||||
return nil
|
||||
}
|
||||
return t.Bytes[start : start+length]
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user