Skip to content

Commit cd356bb

Browse files
committed
Proper input sanitation for a2l files and removed detailed parsing of if data
remove escaped characters while importing a2l file because some A2L files declare Tab-Verb Conversions like 0 "\\" which would otherwise lead to an error in the tokenizer while trying to detect a text in quotation marks. As it is possible to declare custom datastructres in the A2ML Section which may be used in the if data blocks the parsing for the if data structures is not checking for unexpected keywords anymore as this would lead to potential errors while the A2ML declarations aren't implemented yet.
1 parent 4abff41 commit cd356bb

File tree

6 files changed

+32
-28
lines changed

6 files changed

+32
-28
lines changed

a2l/a2l.go

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@ import (
44
"errors"
55
"fmt"
66
"os"
7+
"regexp"
78
"runtime"
9+
"strings"
810
"time"
11+
"unicode"
912

1013
"github.com/rs/zerolog/log"
1114
)
@@ -18,7 +21,7 @@ var (
1821
//numProc is used to set the amount of goroutines in case useMultithreading is true.
1922
//numProc = runtime.NumCPU() * 2 has proven to be reliably fast for different cpu models.
2023
//factors above 4 will generally lead to severe performance degredation due to channel and locking overhead.
21-
numProc = runtime.NumCPU() * 2
24+
numProc = runtime.NumCPU()
2225
)
2326

2427
// A2L is the main struct returned by the a2l package.
@@ -115,6 +118,19 @@ func readFileToString(filepath string) (string, error) {
115118
if err != nil {
116119
return "", err
117120
}
118-
text := string(bytesString)
119-
return text, nil
121+
cleanedStr := cleanBytesString(bytesString)
122+
return cleanedStr, nil
123+
}
124+
125+
func cleanBytesString(bs []byte) string {
126+
//remove escaped characters
127+
re := regexp.MustCompile(`\\.`)
128+
escapedBytesString := re.ReplaceAll(bs, []byte("$1"))
129+
//convert byte array to string
130+
str := string(escapedBytesString)
131+
//remove unprintable chars at the start and end of the a2l file
132+
str = strings.TrimFunc(str, func(r rune) bool {
133+
return !unicode.IsGraphic(r)
134+
})
135+
return str
120136
}

a2l/if_data.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,6 @@ forLoop:
2727
id.dataSet = true
2828
log.Info().Msg("ifData data successfully parsed")
2929
break forLoop
30-
} else if isKeyword(tok.current()) {
31-
err = errors.New("unexpected token " + tok.current())
32-
log.Err(err).Msg("ifData could not be parsed")
33-
break forLoop
3430
} else if !id.nameSet {
3531
id.name = tok.current()
3632
id.nameSet = true

a2l/testdata/fuzz/FuzzParseA2L/290378f40701d315e4488817302849612f93373fd409f18e9bc7c479f8abcd95

Lines changed: 0 additions & 2 deletions
This file was deleted.

a2l/token_generator.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"strconv"
66
"strings"
77
"sync/atomic"
8-
"unicode"
98

109
"github.com/rs/zerolog/log"
1110
)
@@ -45,11 +44,6 @@ func buildTokenGeneratorFromString(str string) (tokenGenerator, error) {
4544
//initialize moduleCount to zero
4645
moduleCount = 0
4746

48-
//remove unprintable chars at the start and end of the a2l file
49-
str = strings.TrimFunc(str, func(r rune) bool {
50-
return !unicode.IsGraphic(r)
51-
})
52-
5347
//replace escaped quotation marks
5448
str = strings.ReplaceAll(str, `\"`, `'`)
5549

ihex32/hex.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ var (
9797
}
9898
)
9999

100-
//parseHex parses a hex-file given as an slice of strings and return a hex struct containing the data in byte form with all the addresses attached.
100+
// parseHex parses a hex-file given as an slice of strings and return a hex struct containing the data in byte form with all the addresses attached.
101101
func parseHex(lines []string) (map[uint32]byte, error) {
102102
var h map[uint32]byte
103103
var err error
@@ -200,7 +200,7 @@ func parseHex(lines []string) (map[uint32]byte, error) {
200200
return h, nil
201201
}
202202

203-
//readFileToString returns a string by reading a document from a given filepath.
203+
// readFileToString returns a string by reading a document from a given filepath.
204204
func readFileToString(filepath string) (string, error) {
205205
bytesString, err := os.ReadFile(filepath)
206206
if err != nil {
@@ -211,7 +211,7 @@ func readFileToString(filepath string) (string, error) {
211211
return text, err
212212
}
213213

214-
//ParseFromFile parses a hex file from a given filepath and return a hex struct containing all data as bytes with their addresses.
214+
// ParseFromFile parses a hex file from a given filepath and return a hex struct containing all data as bytes with their addresses.
215215
func ParseFromFile(filepath string) (map[uint32]byte, error) {
216216
var h map[uint32]byte
217217
var text string
@@ -236,7 +236,7 @@ func ParseFromFile(filepath string) (map[uint32]byte, error) {
236236
return h, nil
237237
}
238238

239-
//parseRecordRoutine calls the parseRecord method for each line given and return them via a channel.
239+
// parseRecordRoutine calls the parseRecord method for each line given and return them via a channel.
240240
func parseRecordRoutine(c chan []*record, lines []string) {
241241
var recs []*record
242242
forLoop:
@@ -254,7 +254,7 @@ forLoop:
254254
close(c)
255255
}
256256

257-
//hexToByte converts a two character hexString to a single byte. fails if input is too long or not valid hex.
257+
// hexToByte converts a two character hexString to a single byte. fails if input is too long or not valid hex.
258258
func hexToByte(str string) (byte, error) {
259259
var err error
260260
var b byte
@@ -267,7 +267,7 @@ func hexToByte(str string) (byte, error) {
267267
return b, err
268268
}
269269

270-
//hexToByteSlice converts at least a four character hexString to a slice of several bytes. fails if input is too short or not valid hex.
270+
// hexToByteSlice converts at least a four character hexString to a slice of several bytes. fails if input is too short or not valid hex.
271271
func hexToByteSlice(hexVal string) ([]byte, error) {
272272
decoded, err := hex.DecodeString(hexVal)
273273
if err != nil {

srec19/hex.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414

1515
var (
1616
//numProc controls the number of goRoutines that are run for the parallel parts of the programm.
17-
numProc = runtime.NumCPU() * 2
17+
numProc = runtime.NumCPU()
1818
//validateChecksums controls whether checksum validation will be executed or skipped.
1919
//in case checksum validation encounters an incorrect checksum it will stop the parser and throw an error.
2020
validateChecksums = true
@@ -97,7 +97,7 @@ var (
9797
}
9898
)
9999

100-
//parseHex parses a hex-file given as an slice of strings and return a hex struct containing the data in byte form with all the addresses attached.
100+
// parseHex parses a hex-file given as an slice of strings and return a hex struct containing the data in byte form with all the addresses attached.
101101
func parseHex(lines []string) (map[uint32]byte, error) {
102102
var h map[uint32]byte
103103
var err error
@@ -189,7 +189,7 @@ func parseHex(lines []string) (map[uint32]byte, error) {
189189
return h, nil
190190
}
191191

192-
//readFileToString returns a string by reading a document from a given filepath.
192+
// readFileToString returns a string by reading a document from a given filepath.
193193
func readFileToString(filepath string) (string, error) {
194194
bytesString, err := os.ReadFile(filepath)
195195
if err != nil {
@@ -200,7 +200,7 @@ func readFileToString(filepath string) (string, error) {
200200
return text, err
201201
}
202202

203-
//ParseFromFile parses a hex file from a given filepath and return a hex struct containing all data as bytes with their addresses.
203+
// ParseFromFile parses a hex file from a given filepath and return a hex struct containing all data as bytes with their addresses.
204204
func ParseFromFile(filepath string) (map[uint32]byte, error) {
205205
var h map[uint32]byte
206206
var text string
@@ -225,7 +225,7 @@ func ParseFromFile(filepath string) (map[uint32]byte, error) {
225225
return h, nil
226226
}
227227

228-
//parseRecordRoutine calls the parseRecord method for each line given and return them via a channel.
228+
// parseRecordRoutine calls the parseRecord method for each line given and return them via a channel.
229229
func parseRecordRoutine(c chan []*record, lines []string) {
230230
var recs []*record
231231
forLoop:
@@ -243,7 +243,7 @@ forLoop:
243243
close(c)
244244
}
245245

246-
//hexToByte converts a two character hexString to a single byte. fails if input is too long or not valid hex.
246+
// hexToByte converts a two character hexString to a single byte. fails if input is too long or not valid hex.
247247
func hexToByte(str string) (byte, error) {
248248
var err error
249249
var b byte
@@ -256,7 +256,7 @@ func hexToByte(str string) (byte, error) {
256256
return b, err
257257
}
258258

259-
//hexToByteSlice converts at least a four character hexString to a slice of several bytes. fails if input is too short or not valid hex.
259+
// hexToByteSlice converts at least a four character hexString to a slice of several bytes. fails if input is too short or not valid hex.
260260
func hexToByteSlice(hexVal string) ([]byte, error) {
261261
decoded, err := hex.DecodeString(hexVal)
262262
if err != nil {

0 commit comments

Comments
 (0)