@@ -18,13 +18,26 @@ const (
18
18
19
19
func ReadInt (reader * bufio.Reader ) (int32 , error ) {
20
20
var int32Value int32
21
- err := binary .Read (reader , binary . BigEndian , & int32Value )
21
+ err := binary .Read (reader , currentByteOrder () , & int32Value )
22
22
return int32Value , err
23
23
}
24
24
25
+ func currentByteOrder () binary.ByteOrder {
26
+ var order binary.ByteOrder = binary .BigEndian
27
+ //if runtime.GOOS == "darwin" && runtime.GOARCH == "arm64" {
28
+ // order = binary.LittleEndian
29
+ //}
30
+ //l.Debugf("Using byte order: %v for OS: %s, ARCH: %s",
31
+ // order == binary.LittleEndian,
32
+ // runtime.GOOS,
33
+ // runtime.GOARCH,
34
+ //)
35
+ return order
36
+ }
37
+
25
38
func ReadLong (reader * bufio.Reader ) (int64 , error ) {
26
39
var int64Value int64
27
- err := binary .Read (reader , binary . BigEndian , & int64Value )
40
+ err := binary .Read (reader , currentByteOrder () , & int64Value )
28
41
return int64Value , err
29
42
}
30
43
@@ -46,6 +59,10 @@ func ReadString(reader *bufio.Reader) (string, error) {
46
59
return "" , fmt .Errorf ("invalid string length: %d" , strLength )
47
60
}
48
61
62
+ if strLength > 2000 {
63
+ return "" , fmt .Errorf ("invalid string length: %d" , strLength )
64
+ }
65
+
49
66
strBytes := make ([]byte , strLength )
50
67
_ , err = reader .Read (strBytes )
51
68
if err != nil {
@@ -191,16 +208,24 @@ func ReadHeader(reader *bufio.Reader) (RunMessage, []string, [][]byte, error) {
191
208
}
192
209
193
210
func ReadGroup (reader * bufio.Reader ) (* Group , error ) {
211
+ const maxHierarchyLength = 2000 // adjust to needs
212
+
194
213
hierarchyLength , err := ReadInt (reader )
195
214
if err != nil {
196
- return nil , err
215
+ return nil , fmt .Errorf ("failed to read hierarchy length: %w" , err )
216
+ }
217
+
218
+ // Validate the length before allocating slice
219
+ if hierarchyLength < 0 || hierarchyLength > maxHierarchyLength {
220
+ return nil , fmt .Errorf ("invalid hierarchy length: %d (must be between 0 and %d)" ,
221
+ hierarchyLength , maxHierarchyLength )
197
222
}
198
223
199
224
hierarchy := make ([]string , hierarchyLength )
200
225
for i := int32 (0 ); i < hierarchyLength ; i ++ {
201
226
hierarchy [i ], err = ReadCachedSanitizedString (reader )
202
227
if err != nil {
203
- return nil , err
228
+ return nil , fmt . Errorf ( "failed to read hierarchy element %d: %w" , i , err )
204
229
}
205
230
}
206
231
0 commit comments