Skip to content

Commit 825f3d7

Browse files
committed
NPE triage
1 parent e3eba3b commit 825f3d7

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

tmc-plugin/src/fi/helsinki/cs/tmc/spyware/HostInformationGenerator.java

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import java.util.List;
1212
import java.util.logging.Level;
1313
import java.util.logging.Logger;
14-
import javax.crypto.Mac;
1514

1615
/**
1716
* Generates host information used by spyware to identify requests coming from
@@ -55,7 +54,11 @@ private static JsonMaker getStaticHostInformation() {
5554
while (iterator.hasMoreElements()) {
5655
NetworkInterface networkInterface = iterator.nextElement();
5756
if (!networkInterface.isLoopback()) {
58-
macs.add(trySecureHash(networkInterface.getHardwareAddress()));
57+
byte[] address = networkInterface.getHardwareAddress();
58+
if (address == null) {
59+
continue;
60+
}
61+
macs.add(trySecureHash(address));
5962
}
6063
}
6164
builder.add("mac_addresses", macs);
@@ -81,10 +84,20 @@ private static JsonMaker getStaticHostInformation() {
8184
* algorithm be missing original string is returned.
8285
*/
8386
private static String trySecureHash(String mac) {
84-
return trySecureHash(mac.getBytes(UTF8));
87+
if (mac == null) {
88+
return "mac_is_null";
89+
}
90+
try {
91+
return trySecureHash(mac.getBytes(UTF8));
92+
} catch (Exception e) {
93+
return "error";
94+
}
8595
}
8696

8797
private static String trySecureHash(byte[] mac) {
98+
if (mac == null) {
99+
return "bmac_is_null";
100+
}
88101
try {
89102
byte[] bytes = MessageDigest.getInstance("SHA-256").digest(mac);
90103
return byteToHex(bytes);
@@ -96,12 +109,15 @@ private static String trySecureHash(byte[] mac) {
96109
try {
97110
return byteToHex(mac);
98111
} catch (Exception ex) {
99-
return "error, e: " + ex.toString();
112+
return "error";
100113
}
101114
}
102115
}
103116

104117
private static String byteToHex(byte[] bytes) {
118+
if (bytes == null) {
119+
return "byte_to_hex_null";
120+
}
105121
StringBuilder sb = new StringBuilder();
106122
for (int i = 0; i < bytes.length; i++) {
107123
sb.append(String.format("%02X", bytes[i]));

0 commit comments

Comments
 (0)