Skip to content

Commit

Permalink
java:S2259: Null pointers should not be dereferenced
Browse files Browse the repository at this point in the history
  • Loading branch information
Benjamin Reed committed Jul 12, 2023
1 parent 431d3cf commit 7a591bb
Show file tree
Hide file tree
Showing 73 changed files with 472 additions and 395 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/*******************************************************************************
* This file is part of OpenNMS(R).
*
* Copyright (C) 2014 The OpenNMS Group, Inc.
* OpenNMS(R) is Copyright (C) 1999-2014 The OpenNMS Group, Inc.
* Copyright (C) 2014-2023 The OpenNMS Group, Inc.
* OpenNMS(R) is Copyright (C) 1999-2023 The OpenNMS Group, Inc.
*
* OpenNMS(R) is a registered trademark of The OpenNMS Group, Inc.
*
Expand Down Expand Up @@ -75,6 +75,7 @@ public byte[] toOctets() {

@Override
public boolean equals(final Object obj) {
if (obj == null) return false;
if (obj instanceof IPAddress) {
return Arrays.equals(m_inetAddress.getAddress(), ((IPAddress) obj).m_inetAddress.getAddress());
}
Expand Down
35 changes: 10 additions & 25 deletions core/criteria/src/main/java/org/opennms/core/criteria/Alias.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/*******************************************************************************
* This file is part of OpenNMS(R).
*
* Copyright (C) 2012-2014 The OpenNMS Group, Inc.
* OpenNMS(R) is Copyright (C) 1999-2014 The OpenNMS Group, Inc.
* Copyright (C) 2012-2023 The OpenNMS Group, Inc.
* OpenNMS(R) is Copyright (C) 1999-2023 The OpenNMS Group, Inc.
*
* OpenNMS(R) is a registered trademark of The OpenNMS Group, Inc.
*
Expand All @@ -28,6 +28,8 @@

package org.opennms.core.criteria;

import java.util.Objects;

import org.opennms.core.criteria.restrictions.Restriction;

public class Alias {
Expand Down Expand Up @@ -90,36 +92,19 @@ public void setJoinCondition(Restriction joinCondition) {

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((m_alias == null) ? 0 : m_alias.hashCode());
result = prime * result + ((m_associationPath == null) ? 0 : m_associationPath.hashCode());
result = prime * result + ((m_type == null) ? 0 : m_type.hashCode());
result = prime * result + ((m_joinCondition == null) ? 0 : m_joinCondition.hashCode());
return result;
return Objects.hash(m_alias, m_associationPath, m_type, m_joinCondition);
}

@Override
public boolean equals(final Object obj) {
if (this == obj) return true;
if (obj == null) return false;
if (!(obj instanceof Alias)) return false;
final Alias other = (Alias) obj;
if (m_alias == null) {
if (other.m_alias != null) return false;
} else if (!m_alias.equals(other.m_alias)) {
return false;
}
if (m_associationPath == null) {
if (other.m_associationPath != null) return false;
} else if (!m_associationPath.equals(other.m_associationPath)) {
return false;
}
if (m_type != other.m_type) return false;
if (m_joinCondition == null && other.m_joinCondition != null) return false;
if (m_joinCondition != null && other.m_joinCondition == null) return false;
if (!m_joinCondition.equals(other.m_joinCondition)) return false;
return true;
final Alias that = (Alias) obj;
return Objects.equals(this.m_alias, that.m_alias)
&& Objects.equals(this.m_associationPath, that.m_associationPath)
&& Objects.equals(this.m_type, that.m_type)
&& Objects.equals(this.m_joinCondition, that.m_joinCondition);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,18 +62,18 @@ public String getFactory() {
}

public boolean isLocalConnection() throws MalformedURLException {
Objects.requireNonNull(getIpAddress());
Objects.requireNonNull(getPort());
final var addr = Objects.requireNonNull(getIpAddress());
final var jvmPort = Objects.requireNonNull(getPort());

// If we're trying to create a connection to a localhost address...
if (getIpAddress().isLoopbackAddress()) {
if (addr.isLoopbackAddress()) {
final String jmxPort = System.getProperty(JMX_PORT_SYSTEM_PROPERTY); // returns null if REMOTE JMX is enabled

// ... and if the port matches the port of the current JVM...
if (getPort().equals(jmxPort) ||
// ... or if remote JMX RMI is disabled and we're attempting to connect
// to the default OpenNMS JMX port...
(jmxPort == null && DEFAULT_OPENNMS_JMX_PORT.equals(getPort()))) {
if (jvmPort.equals(jmxPort)) {
return true;
}
if (jmxPort == null && DEFAULT_OPENNMS_JMX_PORT.equals(jvmPort)) {
return true;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -847,12 +847,11 @@ else if (pRight instanceof Comparable) {
}

else {
if (pLogger.isLoggingError ()) {
pLogger.logError
(Constants.ARITH_OP_BAD_TYPE,
pOperator.getOperatorSymbol (),
pLeft.getClass ().getName (),
pRight.getClass ().getName ());
if (pLogger.isLoggingError()) {
pLogger.logError(Constants.ARITH_OP_BAD_TYPE,
pOperator.getOperatorSymbol(),
pLeft == null? null : pLeft.getClass().getName(),
pRight == null? null : pRight.getClass().getName());
}
return Boolean.FALSE;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
* @author Shawn Bayern (in the style of Nathan's other classes)
**/

@SuppressWarnings("java:S2259") // sonar doesn't recognize pLogger.logError() as short-circuiting null checks
public class FunctionInvocation
extends Expression
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/*******************************************************************************
* This file is part of OpenNMS(R).
*
* Copyright (C) 2011-2014 The OpenNMS Group, Inc.
* OpenNMS(R) is Copyright (C) 1999-2014 The OpenNMS Group, Inc.
* Copyright (C) 2011-2023 The OpenNMS Group, Inc.
* OpenNMS(R) is Copyright (C) 1999-2023 The OpenNMS Group, Inc.
*
* OpenNMS(R) is a registered trademark of The OpenNMS Group, Inc.
*
Expand Down Expand Up @@ -123,8 +123,9 @@ public String getFirstResponseString() {
return getResponseString(0);
}

public SnmpSyntax getResponseValue(int index) {
return getResponseVarBind(index).getValue();
public SnmpSyntax getResponseValue(final int index) {
final var vb = getResponseVarBind(index);
return vb == null ? null : vb.getValue();
}

public String getResponseString(int index) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,7 @@ public static void assertDepthEquals(final Object expected, Object actual) {
assertDepthEquals(0, "", expected, actual);
}

@SuppressWarnings("java:S2259") // sonar doesn't know fail() short-circuits with an AssertionError, so it thinks expected or actual could be null
private static void assertDepthEquals(final int depth, final String propertyName, final Object expected, Object actual) {
if (expected == null && actual == null) {
return;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/*******************************************************************************
* This file is part of OpenNMS(R).
*
* Copyright (C) 2017-2022 The OpenNMS Group, Inc.
* OpenNMS(R) is Copyright (C) 1999-2022 The OpenNMS Group, Inc.
* Copyright (C) 2017-2023 The OpenNMS Group, Inc.
* OpenNMS(R) is Copyright (C) 1999-2023 The OpenNMS Group, Inc.
*
* OpenNMS(R) is a registered trademark of The OpenNMS Group, Inc.
*
Expand Down Expand Up @@ -345,12 +345,14 @@ private boolean resourcesMatch(final Resource a, final Resource b) {
return aPath.equals(bPath);
}

@SuppressWarnings("java:S2259") // sonar doesn't know that StringUtils.hasText null-checks
private String getPath(final Resource resource) {
String ret = null;
if (resource instanceof UrlResource) {
try {
ret = resource.getURL().toExternalForm();
} catch (final IOException e) {
return null;
}
} else if (resource instanceof ClassPathResource) {
ret = ((ClassPathResource) resource).getPath();
Expand All @@ -361,9 +363,13 @@ private String getPath(final Resource resource) {
try {
ret = resource.getURL().getPath();
} catch (final IOException e) {
return null;
}
}
return StringUtils.hasText(ret)? ret.startsWith("/")? ret.substring(1) : ret : null;
if (StringUtils.hasText(ret)) {
return ret.startsWith("/")? ret.substring(1) : ret;
}
return null;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/*******************************************************************************
* This file is part of OpenNMS(R).
*
* Copyright (C) 2016 The OpenNMS Group, Inc.
* OpenNMS(R) is Copyright (C) 1999-2016 The OpenNMS Group, Inc.
* Copyright (C) 2016-2023 The OpenNMS Group, Inc.
* OpenNMS(R) is Copyright (C) 1999-2023 The OpenNMS Group, Inc.
*
* OpenNMS(R) is a registered trademark of The OpenNMS Group, Inc.
*
Expand Down Expand Up @@ -119,13 +119,13 @@ public void setBusinessServices(List<BusinessService> businessServices) {
}
}

if (m_alarmProvider == null && reductionsKeysToLookup.size() > 0) {
LOG.warn("There are one or more reduction keys to lookup, but no alarm provider is set.");
} else {
// Query the status of the reductions keys that were added
// We do this so that we can immediately reflect the state of the new
// graph without having to wait for calls to handleNewOrUpdatedAlarm()
if (reductionsKeysToLookup.size() > 0) {
if (!reductionsKeysToLookup.isEmpty()) {
if (m_alarmProvider == null) {
LOG.warn("There are one or more reduction keys to lookup, but no alarm provider is set.");
} else {
// Query the status of the reductions keys that were added
// We do this so that we can immediately reflect the state of the new
// graph without having to wait for calls to handleNewOrUpdatedAlarm()
final Map<String, AlarmWrapper> lookup = m_alarmProvider.lookup(reductionsKeysToLookup);
for (Entry<String, AlarmWrapper> eachEntry : lookup.entrySet()) {
updateAndPropagateVertex(g, g.getVertexByReductionKey(eachEntry.getKey()), eachEntry.getValue().getStatus());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/*******************************************************************************
* This file is part of OpenNMS(R).
*
* Copyright (C) 2006-2020 The OpenNMS Group, Inc.
* OpenNMS(R) is Copyright (C) 1999-2014 The OpenNMS Group, Inc.
* Copyright (C) 2006-2023 The OpenNMS Group, Inc.
* OpenNMS(R) is Copyright (C) 1999-2023 The OpenNMS Group, Inc.
*
* OpenNMS(R) is a registered trademark of The OpenNMS Group, Inc.
*
Expand Down Expand Up @@ -61,12 +61,6 @@
import org.slf4j.LoggerFactory;
import org.springframework.util.Assert;

/**
* <p>PersistOperationBuilder class.</p>
*
* @author ranger
* @version $Id: $
*/
public class RrdPersistOperationBuilder implements PersistOperationBuilder {
private static final Logger LOG = LoggerFactory.getLogger(RrdPersistOperationBuilder.class);

Expand Down Expand Up @@ -287,7 +281,7 @@ private static boolean createRRD(RrdStrategy<?, ?> rrdStrategy, String creator,

return true;
} catch (Throwable e) {
String path = directory + File.separator + rrdName + rrdStrategy.getDefaultFileExtension();
final var path = directory + File.separator + rrdName + (rrdStrategy == null? "" : rrdStrategy.getDefaultFileExtension());
LOG.error("createRRD: An error occurred creating rrdfile {}", path, e);
throw new org.opennms.netmgt.rrd.RrdException("An error occurred creating rrdfile " + path + ": " + e, e);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/*******************************************************************************
* This file is part of OpenNMS(R).
*
* Copyright (C) 2022 The OpenNMS Group, Inc.
* OpenNMS(R) is Copyright (C) 1999-2022 The OpenNMS Group, Inc.
* Copyright (C) 2022-2023 The OpenNMS Group, Inc.
* OpenNMS(R) is Copyright (C) 1999-2023 The OpenNMS Group, Inc.
*
* OpenNMS(R) is a registered trademark of The OpenNMS Group, Inc.
*
Expand Down Expand Up @@ -35,6 +35,7 @@
import java.net.UnknownHostException;
import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.CompletableFuture;
Expand Down Expand Up @@ -129,6 +130,10 @@ public CompletableFuture<DeviceConfig> getDeviceConfig(String ipAddress, String
public List<RetrievalDefinition> getRetrievalDefinitions(final String ipAddress, final String location) {
final var iface = findMatchingInterface(ipAddress, location, null);

if (iface == null) {
return Collections.emptyList();
}

return iface
// Get all device config services defined for this interface
.getMonitoredServices().stream()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/*******************************************************************************
* This file is part of OpenNMS(R).
*
* Copyright (C) 2014-2021 The OpenNMS Group, Inc.
* OpenNMS(R) is Copyright (C) 1999-2021 The OpenNMS Group, Inc.
* Copyright (C) 2014-2023 The OpenNMS Group, Inc.
* OpenNMS(R) is Copyright (C) 1999-2023 The OpenNMS Group, Inc.
*
* OpenNMS(R) is a registered trademark of The OpenNMS Group, Inc.
*
Expand Down Expand Up @@ -129,21 +129,18 @@ public Set<SubNetwork> findAllLegalLoopbacks() {
.collect(Collectors.toSet());
}

private SubNetwork getNextSubnetwork(final Set<SubNetwork> subNetworks) {
private static SubNetwork getNextSubnetwork(final Set<SubNetwork> subNetworks) {
SubNetwork starting = null;
for (SubNetwork subnet : subNetworks) {
for (final var subnet : subNetworks) {
if (starting == null) {
starting=subnet;
continue;
}
if (starting.getNodeIds().size() < subnet.getNodeIds().size()) {
starting = subnet;
continue;
}
if (starting.getNodeIds().size() == subnet.getNodeIds().size()
&&
InetAddressUtils.difference(starting.getNetwork(), subnet.getNetwork()).signum() > 0) {
} else if (starting.getNodeIds().size() < subnet.getNodeIds().size()) {
starting = subnet;
} else {
if (starting.getNodeIds().size() == subnet.getNodeIds().size()
&& InetAddressUtils.difference(starting.getNetwork(), subnet.getNetwork()).signum() > 0) {
starting = subnet;
}
}
}
return starting;
Expand Down Expand Up @@ -193,9 +190,13 @@ public Map<Integer, Integer> getNodeidPriorityMap(ProtocolSupported protocol) {
LOG.info("getNodeidPriorityMap: subnetworks.size: {}", allLegalSubnets.size());
int priority = 0;
int loop = 0;
while (allLegalSubnets.size() > 0) {
while (!allLegalSubnets.isEmpty()) {
loop++;
SubNetwork start = getNextSubnetwork(allLegalSubnets);
final var start = getNextSubnetwork(allLegalSubnets);
if (start == null) {
LOG.warn("List of legal subnets isn't completely processed, but we were unable to match next subnetwork. Stopping processing. remainder={}", allLegalSubnets);
break;
}
allLegalSubnets.remove(start);
LOG.info("getNodeidPriorityMap: loop-{}: start: {}", loop, start);
LOG.info("getNodeidPriorityMap: loop-{}: priority: {}", loop, priority);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -963,6 +963,7 @@ public MatchIntegerUntil(BiConsumer<ParserState,Integer> consumer, String ends)
public Integer getValue(ParserStageState state) {
// Trim the leading zeros from this value
String value = getAccumulatedValue(state);
if (value == null) return null;
boolean trimmed = false;
while (value.startsWith("0")) {
value = value.substring(1);
Expand Down Expand Up @@ -1020,6 +1021,7 @@ public Integer getValue(ParserStageState state) {
* @param value
* @return
*/
@SuppressWarnings("java:S2259") // HOW exactly is trimmed nullable? it's a primitive!
public static int trimAndConvert(String value) {
if (value == null) {
return 0;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/*******************************************************************************
* This file is part of OpenNMS(R).
*
* Copyright (C) 2016-2016 The OpenNMS Group, Inc.
* OpenNMS(R) is Copyright (C) 1999-2016 The OpenNMS Group, Inc.
* Copyright (C) 2016-2023 The OpenNMS Group, Inc.
* OpenNMS(R) is Copyright (C) 1999-2023 The OpenNMS Group, Inc.
*
* OpenNMS(R) is a registered trademark of The OpenNMS Group, Inc.
*
Expand Down Expand Up @@ -159,7 +159,7 @@ public void testDifferentImplementations() throws Exception {
radixParser.teach(GrokParserStageSequenceBuilder.parseGrok("<%{INT:facilityPriority}>%{NOSPACE:messageId}: %{INT:year}-%{INT:month}-%{INT:day} %{STRING:hostname} %{STRING:message}").toArray(new ParserStage[0]));
radixParser.teach(GrokParserStageSequenceBuilder.parseGrok("<%{INT:facilityPriority}> %{INT:year}-%{INT:month}-%{INT:day} %{STRING:hostname} %{STRING:message}").toArray(new ParserStage[0]));

final int iterations = 100000;
final int iterations = 1_000_000;

{
CompletableFuture<SyslogMessage> event = null;
Expand Down
Loading

0 comments on commit 7a591bb

Please sign in to comment.