Ticket #30544: 0001-Fix-leaking-resource-in-TorperfResultImpl.java.patch

File 0001-Fix-leaking-resource-in-TorperfResultImpl.java.patch, 3.0 KB (added by fava, 11 months ago)

Fix leaking resource TorpertResultImpl

  • src/main/java/org/torproject/descriptor/impl/TorperfResultImpl.java

    From 114f548674d8586453577f2a16ad6b39b1714118 Mon Sep 17 00:00:00 2001
    From: fava <fava@libertymail.net>
    Date: Thu, 29 Aug 2019 06:52:16 +0000
    Subject: [PATCH] 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 b public class TorperfResultImpl extends DescriptorImpl 
    3030    List<Descriptor> parsedDescriptors = new ArrayList<>();
    3131    String descriptorString = new String(rawDescriptorBytes,
    3232        StandardCharsets.UTF_8);
    33     Scanner scanner = new Scanner(descriptorString).useDelimiter("\r?\n");
    34     String typeAnnotation = "";
    35     while (scanner.hasNext()) {
    36       String line = scanner.next();
    37       if (line.startsWith("@type torperf ")) {
    38         String[] parts = line.split(" ");
    39         if (parts.length != 3) {
    40           throw new DescriptorParseException("Illegal line '" + line
    41               + "'.");
    42         }
    43         String version = parts[2];
    44         if (!version.startsWith("1.")) {
    45           throw new DescriptorParseException("Unsupported version in "
    46               + " line '" + line + "'.");
     33    try (Scanner scanner =
     34                 new Scanner(descriptorString).useDelimiter("\r?\n")) {
     35      String typeAnnotation = "";
     36      while (scanner.hasNext()) {
     37        String line = scanner.next();
     38        if (line.startsWith("@type torperf ")) {
     39          String[] parts = line.split(" ");
     40          if (parts.length != 3) {
     41            throw new DescriptorParseException("Illegal line '" + line
     42                    + "'.");
     43          }
     44          String version = parts[2];
     45          if (!version.startsWith("1.")) {
     46            throw new DescriptorParseException("Unsupported version in "
     47                    + " line '" + line + "'.");
     48          }
     49          typeAnnotation = line + "\n";
     50        } else {
     51          /* XXX21932 */
     52          parsedDescriptors.add(new TorperfResultImpl(
     53                  (typeAnnotation + line + "\n")
     54                          .getBytes(StandardCharsets.UTF_8),
     55                  descriptorFile));
     56          typeAnnotation = "";
    4757        }
    48         typeAnnotation = line + "\n";
    49       } else {
    50         /* XXX21932 */
    51         parsedDescriptors.add(new TorperfResultImpl(
    52             (typeAnnotation + line + "\n").getBytes(StandardCharsets.UTF_8),
    53             descriptorFile));
    54         typeAnnotation = "";
    5558      }
    5659    }
    5760    return parsedDescriptors;