Skip to content

Commit 07eb867

Browse files
author
Dave Wichers
committed
Some minor error handling improvements.
1 parent f0e5b35 commit 07eb867

File tree

4 files changed

+44
-9
lines changed

4 files changed

+44
-9
lines changed

plugin/src/main/java/org/owasp/benchmarkutils/score/CategoryMetrics.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,11 @@ public CategoryMetrics(
4444
"ERROR: precision for category: " + category + " is NaN");
4545
}
4646
if (Double.isNaN(tpr)) {
47-
throw new IllegalArgumentException(
48-
"ERROR: true positive rate for category: " + category + " is NaN");
47+
System.out.println(
48+
"WARNING: true positive rate for category: "
49+
+ category
50+
+ " is NaN. Setting it to 0.");
51+
tpr = 0.0;
4952
}
5053
if (Double.isNaN(fpr)) {
5154
throw new IllegalArgumentException(

plugin/src/main/java/org/owasp/benchmarkutils/score/parsers/sarif/SarifReader.java

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import java.util.regex.Matcher;
3030
import java.util.regex.Pattern;
3131
import org.json.JSONArray;
32+
import org.json.JSONException;
3233
import org.json.JSONObject;
3334
import org.owasp.benchmarkutils.score.ResultFile;
3435
import org.owasp.benchmarkutils.score.TestCaseResult;
@@ -159,15 +160,24 @@ private Map<String, Integer> ruleCweMappingsByTag(JSONObject tool) {
159160
Map<String, Integer> mappings = new HashMap<>();
160161

161162
for (JSONObject rule : extractRulesFrom(tool)) {
162-
JSONArray tags = rule.getJSONObject("properties").getJSONArray("tags");
163+
try {
164+
JSONArray tags = rule.getJSONObject("properties").getJSONArray("tags");
163165

164-
for (int j = 0; j < tags.length(); j++) {
165-
String tag = tags.getString(j).toLowerCase();
166+
for (int j = 0; j < tags.length(); j++) {
167+
String tag = tags.getString(j).toLowerCase();
166168

167-
// only take first CWE id for rule
168-
if (tag.contains("cwe") && !mappings.containsKey(rule.getString("id"))) {
169-
mappings.put(rule.getString("id"), mapCwe(extractCwe(tag)));
169+
// only take first CWE id for rule
170+
if (tag.contains("cwe") && !mappings.containsKey(rule.getString("id"))) {
171+
mappings.put(rule.getString("id"), mapCwe(extractCwe(tag)));
172+
}
170173
}
174+
} catch (JSONException e) {
175+
System.err.println(
176+
"WARNING: "
177+
+ e.getMessage()
178+
+ " for rule: "
179+
+ rule.toString()
180+
+ ". Parser for this tool type needs to be fixed to handle this properly.");
171181
}
172182
}
173183

@@ -214,7 +224,7 @@ private Map<String, Integer> ruleCweMappingsByField(JSONObject tool) {
214224
return mappings;
215225
}
216226

217-
public Map<String, Integer> customRuleCweMappings(JSONObject driver) {
227+
public Map<String, Integer> customRuleCweMappings(JSONObject tool) {
218228
throw new IllegalArgumentException(
219229
"SARIF Reader using custom CWE mappings MUST overwrite mapping method.");
220230
}

plugin/src/main/java/org/owasp/benchmarkutils/score/report/ScatterHome.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,17 @@ private JFreeChart display(String title, Set<Tool> tools) {
138138
for (XYDataItem item : (List<XYDataItem>) series.getItems()) {
139139
double x = item.getX().doubleValue();
140140
double y = item.getY().doubleValue();
141+
142+
// This should only happen if we don't have all the CWEs mapped to CategoryGroups,
143+
// or there are no actual findings for this category.
144+
if (Double.isNaN(y)) {
145+
System.err.println(
146+
"WARNING: Y PlotPoint for item: "
147+
+ item.toString()
148+
+ " is NaN, so setting it to 0.");
149+
y = 0.0;
150+
}
151+
141152
double z = (x + y) / 2;
142153
XYLineAnnotation score = new XYLineAnnotation(x, y, z, z, dashed, Color.blue);
143154
xyplot.addAnnotation(score);

plugin/src/main/java/org/owasp/benchmarkutils/score/report/ScatterPlot.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,17 @@ void addLabelsToPlotPoints(HashMap<Point2D, String> map, XYPlot xyplot) {
178178
if (e.getValue() != null) {
179179
Point2D p = e.getKey();
180180
String label = sort(e.getValue());
181+
182+
// This should only happen if we don't have all the CWEs mapped to CategoryGroups,
183+
// or there are no actual findings for this category.
184+
if (Double.isNaN(p.getY())) {
185+
System.err.println(
186+
"WARNING: Y PlotPoint for label: "
187+
+ label
188+
+ " is NaN, so setting it to 0.");
189+
p.setLocation(p.getX(), 0.0);
190+
}
191+
181192
XYTextAnnotation annotation = new XYTextAnnotation(label, p.getX(), p.getY());
182193
annotation.setTextAnchor(
183194
p.getX() < 3 ? TextAnchor.TOP_LEFT : TextAnchor.TOP_CENTER);

0 commit comments

Comments
 (0)