commit 114f548674d8586453577f2a16ad6b39b1714118 Author: fava fava@libertymail.net Date: Thu Aug 29 06:52:16 2019 +0000
Fix leaking resource in TorperfResultImpl.java
Fix leaking resource in TorperfResultImpl.java using try-with-resource statement. This fix is related to analysis on metrics-lib using sonarqube Implements part of #30544
Signed-off-by: fava fava@libertymail.net --- .../descriptor/impl/TorperfResultImpl.java | 45 ++++++++++++---------- 1 file changed, 24 insertions(+), 21 deletions(-)
diff --git a/src/main/java/org/torproject/descriptor/impl/TorperfResultImpl.java b/src/main/java/org/torproject/descriptor/impl/TorperfResultImpl.java index 4eb00ac..83e9491 100644 --- a/src/main/java/org/torproject/descriptor/impl/TorperfResultImpl.java +++ b/src/main/java/org/torproject/descriptor/impl/TorperfResultImpl.java @@ -30,28 +30,31 @@ public class TorperfResultImpl extends DescriptorImpl List<Descriptor> parsedDescriptors = new ArrayList<>(); String descriptorString = new String(rawDescriptorBytes, StandardCharsets.UTF_8); - Scanner scanner = new Scanner(descriptorString).useDelimiter("\r?\n"); - String typeAnnotation = ""; - while (scanner.hasNext()) { - String line = scanner.next(); - if (line.startsWith("@type torperf ")) { - String[] parts = line.split(" "); - if (parts.length != 3) { - throw new DescriptorParseException("Illegal line '" + line - + "'."); - } - String version = parts[2]; - if (!version.startsWith("1.")) { - throw new DescriptorParseException("Unsupported version in " - + " line '" + line + "'."); + try (Scanner scanner = + new Scanner(descriptorString).useDelimiter("\r?\n")) { + String typeAnnotation = ""; + while (scanner.hasNext()) { + String line = scanner.next(); + if (line.startsWith("@type torperf ")) { + String[] parts = line.split(" "); + if (parts.length != 3) { + throw new DescriptorParseException("Illegal line '" + line + + "'."); + } + String version = parts[2]; + if (!version.startsWith("1.")) { + throw new DescriptorParseException("Unsupported version in " + + " line '" + line + "'."); + } + typeAnnotation = line + "\n"; + } else { + /* XXX21932 */ + parsedDescriptors.add(new TorperfResultImpl( + (typeAnnotation + line + "\n") + .getBytes(StandardCharsets.UTF_8), + descriptorFile)); + typeAnnotation = ""; } - typeAnnotation = line + "\n"; - } else { - /* XXX21932 */ - parsedDescriptors.add(new TorperfResultImpl( - (typeAnnotation + line + "\n").getBytes(StandardCharsets.UTF_8), - descriptorFile)); - typeAnnotation = ""; } } return parsedDescriptors;
tor-commits@lists.torproject.org