mirror of
https://github.com/arkadiyt/protodump.git
synced 2026-10-21 09:46:52 +02:00
Fix infinite loop when trying to consume proto fields
This commit is contained in:
1 parent
16270c3b99
commit
e3ca27f157
2 files changed
+16
-8
No files matched your search
@@ -8,7 +8,7 @@ jobs:
|
|||||||
- name: Set up Go
|
- name: Set up Go
|
||||||
uses: actions/setup-go@v4
|
uses: actions/setup-go@v4
|
||||||
with:
|
with:
|
||||||
go-version: '1.20'
|
go-version: '1.22'
|
||||||
- name: Install Protoc
|
- name: Install Protoc
|
||||||
run: |
|
run: |
|
||||||
sudo apt install -y protobuf-compiler
|
sudo apt install -y protobuf-compiler
|
||||||
|
|||||||
+15
-7
@@ -11,22 +11,21 @@ import (
|
|||||||
const scan = ".proto"
|
const scan = ".proto"
|
||||||
const magicByte = 0xa
|
const magicByte = 0xa
|
||||||
|
|
||||||
func consumeBytes(data []byte, position int) int {
|
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, _, len := protowire.ConsumeField(data[position:])
|
||||||
if len < 0 {
|
if len < 0 {
|
||||||
_ = protowire.ParseError(len)
|
err := protowire.ParseError(len)
|
||||||
return position - start
|
return position - start, fmt.Errorf("couldn't consume proto bytes: %w", err)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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 {
|
||||||
if consumedFieldOne {
|
if consumedFieldOne {
|
||||||
return position - start
|
return position - start, nil
|
||||||
}
|
}
|
||||||
consumedFieldOne = true
|
consumedFieldOne = true
|
||||||
}
|
}
|
||||||
@@ -38,7 +37,7 @@ func consumeBytes(data []byte, position int) int {
|
|||||||
func ScanFile(path string) ([][]byte, error) {
|
func ScanFile(path string) ([][]byte, error) {
|
||||||
data, err := os.ReadFile(path)
|
data, err := os.ReadFile(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("Couldn't open file: %w", err)
|
return nil, fmt.Errorf("couldn't open file: %w", err)
|
||||||
}
|
}
|
||||||
return Scan(data), nil
|
return Scan(data), nil
|
||||||
}
|
}
|
||||||
@@ -68,7 +67,16 @@ func Scan(data []byte) [][]byte {
|
|||||||
start -= 1
|
start -= 1
|
||||||
}
|
}
|
||||||
|
|
||||||
length := consumeBytes(data, start)
|
length, err := consumeBytes(data, start)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("%v\n", err)
|
||||||
|
if len(data) > index {
|
||||||
|
data = data[index+1:]
|
||||||
|
continue
|
||||||
|
} else {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
results = append(results, data[start:start+length])
|
results = append(results, data[start:start+length])
|
||||||
data = data[start+length:]
|
data = data[start+length:]
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user