mirror of
https://github.com/arkadiyt/protodump.git
synced 2026-10-21 09:46:52 +02:00
Merge pull request #15 from hugoharman/main
Fix overly strict error handling causing valid protos to be skipped
This commit is contained in:
1 file changed
+20
-4
+20
-4
@@ -4,6 +4,7 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"google.golang.org/protobuf/encoding/protowire"
|
"google.golang.org/protobuf/encoding/protowire"
|
||||||
)
|
)
|
||||||
@@ -15,12 +16,22 @@ func consumeBytes(data []byte, position int) (int, error) {
|
|||||||
start := position
|
start := position
|
||||||
consumedFieldOne := false
|
consumedFieldOne := false
|
||||||
for {
|
for {
|
||||||
number, _, len := protowire.ConsumeField(data[position:])
|
number, _, length := protowire.ConsumeField(data[position:])
|
||||||
if len < 0 {
|
if length < 0 {
|
||||||
err := protowire.ParseError(len)
|
err := protowire.ParseError(length)
|
||||||
|
// Treat "invalid field number" as end of data, not an error
|
||||||
|
if strings.Contains(err.Error(), "invalid field number") {
|
||||||
|
return position - start, nil
|
||||||
|
}
|
||||||
|
// Return other parse errors as actual errors
|
||||||
return position - start, fmt.Errorf("couldn't consume proto bytes: %w", err)
|
return position - start, fmt.Errorf("couldn't consume proto bytes: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Prevent infinite loop - if we can't consume any bytes, we're done
|
||||||
|
if length == 0 {
|
||||||
|
return position - start, nil
|
||||||
|
}
|
||||||
|
|
||||||
// Only consume Field 1 once (to handle the case where protobuf definitions are adjacent
|
// Only consume Field 1 once (to handle the case where protobuf definitions are adjacent
|
||||||
// in program memory)
|
// in program memory)
|
||||||
if number == 1 {
|
if number == 1 {
|
||||||
@@ -30,7 +41,12 @@ func consumeBytes(data []byte, position int) (int, error) {
|
|||||||
consumedFieldOne = true
|
consumedFieldOne = true
|
||||||
}
|
}
|
||||||
|
|
||||||
position += len
|
position += length
|
||||||
|
|
||||||
|
// Additional safety check - don't read beyond data bounds
|
||||||
|
if position-start >= len(data[start:]) {
|
||||||
|
return position - start, nil
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user