From fb07486404ec86127b15e508f84526689f1f2a5a Mon Sep 17 00:00:00 2001 From: Catfriend1 Date: Sun, 27 Jan 2019 20:06:08 +0100 Subject: [PATCH] ConfigXml: Make getOrDefault more safe by catching NumberFormatException --- .../syncthingandroid/util/ConfigXml.java | 22 ++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/com/nutomic/syncthingandroid/util/ConfigXml.java b/app/src/main/java/com/nutomic/syncthingandroid/util/ConfigXml.java index 9a6314bf..1ce2f020 100644 --- a/app/src/main/java/com/nutomic/syncthingandroid/util/ConfigXml.java +++ b/app/src/main/java/com/nutomic/syncthingandroid/util/ConfigXml.java @@ -370,7 +370,11 @@ public class ConfigXml { } 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) { @@ -378,11 +382,23 @@ public class ConfigXml { } 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) {