commit 563f762ca7eeb649ac3d8777f5924d9c66cfd760 Author: Karsten Loesing karsten.loesing@gmx.net Date: Thu Oct 26 10:28:11 2017 +0200
Fix two NullPointerExceptions.
There's a relay running an alternate Tor version that produces descriptors without "uptime" line, and the directory authorities don't include a "v" line for that relay, likely because its platform string does not include the magic word "Tor".
Fixes #24012. --- CHANGELOG.md | 8 ++++++++ .../java/org/torproject/onionoo/docs/DetailsStatus.java | 3 ++- .../java/org/torproject/onionoo/docs/NodeStatus.java | 3 ++- .../onionoo/updater/NodeDetailsStatusUpdater.java | 17 +++++++++++------ 4 files changed, 23 insertions(+), 8 deletions(-)
diff --git a/CHANGELOG.md b/CHANGELOG.md index b2aff31..ca17fa7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,11 @@ +# Changes in version 4.2-1.6.1 - 2017-10-2? + + * Medium changes + - Fix two NullPointerExceptions caused by accessing optional parts + of relay server descriptors and consensuses without checking + first whether they're available or not. + + # Changes in version 4.2-1.6.0 - 2017-10-09
* Medium changes diff --git a/src/main/java/org/torproject/onionoo/docs/DetailsStatus.java b/src/main/java/org/torproject/onionoo/docs/DetailsStatus.java index 0a0c215..faec117 100644 --- a/src/main/java/org/torproject/onionoo/docs/DetailsStatus.java +++ b/src/main/java/org/torproject/onionoo/docs/DetailsStatus.java @@ -45,7 +45,8 @@ public class DetailsStatus extends Document { private String last_restarted;
public void setLastRestarted(Long lastRestarted) { - this.last_restarted = DateTimeHelper.format(lastRestarted); + this.last_restarted = null == lastRestarted ? null + : DateTimeHelper.format(lastRestarted); }
public Long getLastRestarted() { diff --git a/src/main/java/org/torproject/onionoo/docs/NodeStatus.java b/src/main/java/org/torproject/onionoo/docs/NodeStatus.java index 2943601..24546dd 100644 --- a/src/main/java/org/torproject/onionoo/docs/NodeStatus.java +++ b/src/main/java/org/torproject/onionoo/docs/NodeStatus.java @@ -324,7 +324,8 @@ public class NodeStatus extends Document { private String version;
public void setVersion(String version) { - this.version = version.substring(version.lastIndexOf(" ") + 1); + this.version = null == version ? null + : version.substring(version.lastIndexOf(" ") + 1); }
public String getVersion() { diff --git a/src/main/java/org/torproject/onionoo/updater/NodeDetailsStatusUpdater.java b/src/main/java/org/torproject/onionoo/updater/NodeDetailsStatusUpdater.java index ff14d75..04a04fa 100644 --- a/src/main/java/org/torproject/onionoo/updater/NodeDetailsStatusUpdater.java +++ b/src/main/java/org/torproject/onionoo/updater/NodeDetailsStatusUpdater.java @@ -158,15 +158,13 @@ public class NodeDetailsStatusUpdater implements DescriptorListener, /* Already parsed more recent server descriptor from this relay. */ return; } - long lastRestartedMillis = descriptor.getPublishedMillis() - - descriptor.getUptime() * DateTimeHelper.ONE_SECOND; int bandwidthRate = descriptor.getBandwidthRate(); int bandwidthBurst = descriptor.getBandwidthBurst(); int observedBandwidth = descriptor.getBandwidthObserved(); int advertisedBandwidth = Math.min(bandwidthRate, Math.min(bandwidthBurst, observedBandwidth)); detailsStatus.setDescPublished(descriptor.getPublishedMillis()); - detailsStatus.setLastRestarted(lastRestartedMillis); + detailsStatus.setLastRestarted(calculateLastRestartedMillis(descriptor)); detailsStatus.setBandwidthRate(bandwidthRate); detailsStatus.setBandwidthBurst(bandwidthBurst); detailsStatus.setObservedBandwidth(observedBandwidth); @@ -204,6 +202,15 @@ public class NodeDetailsStatusUpdater implements DescriptorListener, this.documentStore.store(detailsStatus, fingerprint); }
+ private Long calculateLastRestartedMillis(ServerDescriptor descriptor) { + Long lastRestartedMillis = null; + if (null != descriptor.getUptime()) { + lastRestartedMillis = descriptor.getPublishedMillis() + - descriptor.getUptime() * DateTimeHelper.ONE_SECOND; + } + return lastRestartedMillis; + } + private Map<String, Map<String, Long>> exitListEntries = new HashMap<>();
private void processExitList(ExitList exitList) { @@ -318,13 +325,11 @@ public class NodeDetailsStatusUpdater implements DescriptorListener, /* Already parsed more recent server descriptor from this bridge. */ return; } - long lastRestartedMillis = descriptor.getPublishedMillis() - - descriptor.getUptime() * DateTimeHelper.ONE_SECOND; int advertisedBandwidth = Math.min(descriptor.getBandwidthRate(), Math.min(descriptor.getBandwidthBurst(), descriptor.getBandwidthObserved())); detailsStatus.setDescPublished(descriptor.getPublishedMillis()); - detailsStatus.setLastRestarted(lastRestartedMillis); + detailsStatus.setLastRestarted(calculateLastRestartedMillis(descriptor)); detailsStatus.setAdvertisedBandwidth(advertisedBandwidth); detailsStatus.setPlatform(descriptor.getPlatform()); this.documentStore.store(detailsStatus, fingerprint);