Skip to content

Commit 1e4b440

Browse files
centralize endian encoding, limit debug string/hex output
1 parent e00b9d5 commit 1e4b440

File tree

2 files changed

+38
-8
lines changed

2 files changed

+38
-8
lines changed

gatlingparser/decoders.go

+29-4
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,26 @@ const (
1818

1919
func ReadInt(reader *bufio.Reader) (int32, error) {
2020
var int32Value int32
21-
err := binary.Read(reader, binary.BigEndian, &int32Value)
21+
err := binary.Read(reader, currentByteOrder(), &int32Value)
2222
return int32Value, err
2323
}
2424

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+
2538
func ReadLong(reader *bufio.Reader) (int64, error) {
2639
var int64Value int64
27-
err := binary.Read(reader, binary.BigEndian, &int64Value)
40+
err := binary.Read(reader, currentByteOrder(), &int64Value)
2841
return int64Value, err
2942
}
3043

@@ -46,6 +59,10 @@ func ReadString(reader *bufio.Reader) (string, error) {
4659
return "", fmt.Errorf("invalid string length: %d", strLength)
4760
}
4861

62+
if strLength > 2000 {
63+
return "", fmt.Errorf("invalid string length: %d", strLength)
64+
}
65+
4966
strBytes := make([]byte, strLength)
5067
_, err = reader.Read(strBytes)
5168
if err != nil {
@@ -191,16 +208,24 @@ func ReadHeader(reader *bufio.Reader) (RunMessage, []string, [][]byte, error) {
191208
}
192209

193210
func ReadGroup(reader *bufio.Reader) (*Group, error) {
211+
const maxHierarchyLength = 2000 // adjust to needs
212+
194213
hierarchyLength, err := ReadInt(reader)
195214
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)
197222
}
198223

199224
hierarchy := make([]string, hierarchyLength)
200225
for i := int32(0); i < hierarchyLength; i++ {
201226
hierarchy[i], err = ReadCachedSanitizedString(reader)
202227
if err != nil {
203-
return nil, err
228+
return nil, fmt.Errorf("failed to read hierarchy element %d: %w", i, err)
204229
}
205230
}
206231

gatlingparser/gatlingparser.go

+9-4
Original file line numberDiff line numberDiff line change
@@ -458,8 +458,13 @@ func stringProcessor(lineBuffer []byte) error {
458458
return err
459459
default:
460460
// If the line buffer contains unknown characters, convert bytes to hex string for better debugging
461-
hexLineBuffer := fmt.Sprintf("%X", lineBuffer)
462-
return fmt.Errorf("Unknown line type encountered: %s (hex: %s)", lineBuffer, hexLineBuffer)
461+
// hexLineBuffer := fmt.Sprintf("%X", lineBuffer)
462+
//return fmt.Errorf("Unknown line type encountered: %s (hex: %s)", lineBuffer, hexLineBuffer)
463+
// If string is longer than 24 chars, truncate it
464+
if len(lineBuffer) > 24 {
465+
lineBuffer = lineBuffer[:24]
466+
}
467+
return fmt.Errorf("Unknown line type encountered: %s", lineBuffer)
463468
}
464469
}
465470

@@ -471,7 +476,7 @@ func detectGatlingLogVersion(file *os.File) (string, error) {
471476
}
472477
}()
473478
var firstByte byte
474-
if err := binary.Read(file, binary.BigEndian, &firstByte); err != nil {
479+
if err := binary.Read(file, currentByteOrder(), &firstByte); err != nil {
475480
if err == io.EOF {
476481
return "", fmt.Errorf("file is empty")
477482
}
@@ -561,7 +566,7 @@ ParseLoop:
561566

562567
func processLogHeader(reader *bufio.Reader) (*RunMessage, []string, error) {
563568
var recordType byte
564-
err := binary.Read(reader, binary.BigEndian, &recordType)
569+
err := binary.Read(reader, currentByteOrder(), &recordType)
565570
if err != nil {
566571
return nil, nil, err
567572
}

0 commit comments

Comments
 (0)