Skip to content

Commit

Permalink
ConfigXml: Make getOrDefault more safe by catching NumberFormatException
Browse files Browse the repository at this point in the history
  • Loading branch information
Catfriend1 committed Jan 27, 2019
1 parent e4dae20 commit fb07486
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions app/src/main/java/com/nutomic/syncthingandroid/util/ConfigXml.java
Original file line number Diff line number Diff line change
Expand Up @@ -370,19 +370,35 @@ private Boolean getAttributeOrDefault(final Element element, String attribute, B
}

private Integer getAttributeOrDefault(final Element element, String attribute, Integer defaultValue) {
return element.hasAttribute(attribute) ? Integer.parseInt(element.getAttribute(attribute)) : defaultValue;
try {
return element.hasAttribute(attribute) ? Integer.parseInt(element.getAttribute(attribute)) : defaultValue;
} catch (NumberFormatException e) {
return defaultValue;
}
}

private String getAttributeOrDefault(final Element element, String attribute, String defaultValue) {
return element.hasAttribute(attribute) ? element.getAttribute(attribute) : defaultValue;
}

private Boolean getContentOrDefault(final Node node, Boolean defaultValue) {
return (node == null) ? defaultValue : Boolean.parseBoolean(node.getTextContent());
return (node == null) ? defaultValue : Boolean.parseBoolean(node.getTextContent());
}

private Integer getContentOrDefault(final Node node, Integer defaultValue) {
return (node == null) ? defaultValue : Integer.parseInt(node.getTextContent());
try {
return (node == null) ? defaultValue : Integer.parseInt(node.getTextContent());
} catch (NumberFormatException e) {
return defaultValue;
}
}

private Float getContentOrDefault(final Node node, Float defaultValue) {
try {
return (node == null) ? defaultValue : Float.parseFloat(node.getTextContent());
} catch (NumberFormatException e) {
return defaultValue;
}
}

private String getContentOrDefault(final Node node, String defaultValue) {
Expand Down

0 comments on commit fb07486

Please sign in to comment.