Skip to content

Commit 6c6ba05

Browse files
committed
[TO BE SQUASHED] Address issues
* MustCompile becomes Compile, regex can fail and an error will be reported (Compilation WON'T fail) * Atoi are checked and compilation WILL fail in case of errors
1 parent 975a84a commit 6c6ba05

File tree

1 file changed

+32
-11
lines changed

1 file changed

+32
-11
lines changed

src/arduino.cc/builder/phases/sizer.go

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -69,22 +69,29 @@ func checkSize(buildProperties properties.Map, verbose bool, warningsLevel strin
6969
return nil
7070
}
7171

72-
maxTextSize, _ := strconv.Atoi(maxTextSizeString)
72+
maxTextSize, err := strconv.Atoi(maxTextSizeString)
73+
if err != nil {
74+
return err
75+
}
7376
maxDataSize := -1
7477

7578
if maxDataSizeString != "" {
76-
maxDataSize, _ = strconv.Atoi(maxDataSizeString)
79+
maxDataSize, err = strconv.Atoi(maxDataSizeString)
80+
if err != nil {
81+
return err
82+
}
7783
}
7884

79-
var textSize, dataSize, eepromSize int
85+
textSize, dataSize, eepromSize := -1, -1, -1
8086

81-
err := execSizeReceipe(properties, logger, &textSize, &dataSize, &eepromSize)
87+
err = execSizeReceipe(properties, logger, &textSize, &dataSize, &eepromSize)
8288
if err != nil {
83-
return err
89+
logger.Println(constants.LOG_LEVEL_WARN, constants.MSG_SIZER_ERROR_NO_RULE)
90+
return nil
8491
}
8592

8693
logger.Println(constants.LOG_LEVEL_INFO, constants.MSG_SIZER_TEXT_FULL, strconv.Itoa(textSize), strconv.Itoa(maxTextSize), strconv.Itoa(textSize*100/maxTextSize))
87-
if dataSize > 0 {
94+
if dataSize >= 0 {
8895
if maxDataSize > 0 {
8996
logger.Println(constants.LOG_LEVEL_INFO, constants.MSG_SIZER_DATA_FULL, strconv.Itoa(dataSize), strconv.Itoa(maxDataSize), strconv.Itoa(dataSize*100/maxDataSize), strconv.Itoa(maxDataSize-dataSize))
9097
} else {
@@ -103,7 +110,10 @@ func checkSize(buildProperties properties.Map, verbose bool, warningsLevel strin
103110
}
104111

105112
if properties[constants.PROPERTY_WARN_DATA_PERCENT] != "" {
106-
warnDataPercentage, _ := strconv.Atoi(properties[constants.PROPERTY_WARN_DATA_PERCENT])
113+
warnDataPercentage, err := strconv.Atoi(properties[constants.PROPERTY_WARN_DATA_PERCENT])
114+
if err != nil {
115+
return err
116+
}
107117
if maxDataSize > 0 && dataSize > maxDataSize*warnDataPercentage/100 {
108118
logger.Println(constants.LOG_LEVEL_WARN, constants.MSG_SIZER_LOW_MEMORY)
109119
}
@@ -117,25 +127,33 @@ func execSizeReceipe(properties properties.Map, logger i18n.Logger, textSize *in
117127
out, err := builder_utils.ExecRecipe(properties, constants.RECIPE_SIZE_PATTERN, false, false, false, logger)
118128

119129
if err != nil {
120-
i18n.ErrorfWithLogger(logger, constants.MSG_SIZER_ERROR_NO_RULE)
121130
return errors.New("")
122131
}
123132

124133
// force multiline match prepending "(?m)" to the actual regexp
134+
// return an error if RECIPE_SIZE_REGEXP doesn't exist
125135

126136
if len(properties[constants.RECIPE_SIZE_REGEXP]) > 0 {
127-
textRegexp := regexp.MustCompile("(?m)" + properties[constants.RECIPE_SIZE_REGEXP])
137+
textRegexp, err := regexp.Compile("(?m)" + properties[constants.RECIPE_SIZE_REGEXP])
138+
if err != nil {
139+
return errors.New("")
140+
}
128141
result := textRegexp.FindAllSubmatch(out, -1)
129142
for _, b := range result {
130143
for _, c := range b {
131144
res, _ := strconv.Atoi(string(c))
132145
*textSize += res
133146
}
134147
}
148+
} else {
149+
return errors.New("")
135150
}
136151

137152
if len(properties[constants.RECIPE_SIZE_REGEXP_DATA]) > 0 {
138-
dataRegexp := regexp.MustCompile("(?m)" + properties[constants.RECIPE_SIZE_REGEXP_DATA])
153+
dataRegexp, err := regexp.Compile("(?m)" + properties[constants.RECIPE_SIZE_REGEXP_DATA])
154+
if err != nil {
155+
return errors.New("")
156+
}
139157
result := dataRegexp.FindAllSubmatch(out, -1)
140158
for _, b := range result {
141159
for _, c := range b {
@@ -146,7 +164,10 @@ func execSizeReceipe(properties properties.Map, logger i18n.Logger, textSize *in
146164
}
147165

148166
if len(properties[constants.RECIPE_SIZE_REGEXP_EEPROM]) > 0 {
149-
eepromRegexp := regexp.MustCompile("(?m)" + properties[constants.RECIPE_SIZE_REGEXP_EEPROM])
167+
eepromRegexp, err := regexp.Compile("(?m)" + properties[constants.RECIPE_SIZE_REGEXP_EEPROM])
168+
if err != nil {
169+
return errors.New("")
170+
}
150171
result := eepromRegexp.FindAllSubmatch(out, -1)
151172
for _, b := range result {
152173
for _, c := range b {

0 commit comments

Comments
 (0)