Skip to content

Commit 4772288

Browse files
committed
fix(dataexchange): prevent integer overflow in WriteFrame allocation
Use int64 arithmetic for size computation before allocating the file frame payload buffer. This prevents a potential integer overflow when len(name) + len(Payload) exceeds math.MaxInt on 32-bit platforms. Fixes CodeQL high-severity alert: 'Size computation for allocation may overflow'
1 parent 54f0edc commit 4772288

1 file changed

Lines changed: 6 additions & 1 deletion

File tree

pkg/dataexchange/dataexchange.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"encoding/binary"
55
"fmt"
66
"io"
7+
"math"
78
)
89

910
// Frame types for data exchange on port 1001.
@@ -29,7 +30,11 @@ func WriteFrame(w io.Writer, f *Frame) error {
2930
if f.Type == TypeFile {
3031
// Prepend filename
3132
name := []byte(f.Filename)
32-
payload = make([]byte, 2+len(name)+len(f.Payload))
33+
totalLen := int64(2) + int64(len(name)) + int64(len(f.Payload))
34+
if totalLen > math.MaxInt || totalLen < 0 {
35+
return fmt.Errorf("file frame too large: %d bytes", totalLen)
36+
}
37+
payload = make([]byte, int(totalLen))
3338
binary.BigEndian.PutUint16(payload[0:2], uint16(len(name)))
3439
copy(payload[2:], name)
3540
copy(payload[2+len(name):], f.Payload)

0 commit comments

Comments
 (0)