Skip to content

Commit 78bfaa7

Browse files
sedukullyadvr
authored andcommitted
Fixed few coverity issues like invalid boxing unboxing issues, resource leaks, null dereferences
(cherry picked from commit ef6ec7b) Signed-off-by: Rohit Yadav <rohit.yadav@shapeblue.com>
1 parent 405ed3c commit 78bfaa7

10 files changed

Lines changed: 63 additions & 56 deletions

File tree

api/src/org/apache/cloudstack/api/command/user/firewall/CreateEgressFirewallRuleCmd.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ public Long getSourceIpAddressId() {
179179
@Override
180180
public Integer getSourcePortStart() {
181181
if (publicStartPort != null) {
182-
return publicStartPort.intValue();
182+
return publicStartPort;
183183
}
184184
return null;
185185
}
@@ -188,12 +188,11 @@ public Integer getSourcePortStart() {
188188
public Integer getSourcePortEnd() {
189189
if (publicEndPort == null) {
190190
if (publicStartPort != null) {
191-
return publicStartPort.intValue();
191+
return publicStartPort;
192192
}
193193
} else {
194-
return publicEndPort.intValue();
194+
return publicEndPort;
195195
}
196-
197196
return null;
198197
}
199198

api/src/org/apache/cloudstack/api/command/user/firewall/DeleteEgressFirewallRuleCmd.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,11 @@ public String getSyncObjType() {
115115

116116
@Override
117117
public Long getSyncObjId() {
118-
return _firewallService.getFirewallRule(id).getNetworkId();
119-
}
118+
FirewallRule fw = _firewallService.getFirewallRule(id);
119+
if (fw != null)
120+
return fw.getNetworkId();
121+
return null;
122+
}
120123

121124
@Override
122125
public ApiCommandJobType getInstanceType() {

api/test/org/apache/cloudstack/api/command/test/AddIpToVmNicTest.java

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,10 @@ public class AddIpToVmNicTest extends TestCase {
4747
private ResponseGenerator responseGenerator;
4848
private SuccessResponse successResponseGenerator;
4949

50-
@Rule
51-
public ExpectedException expectedException = ExpectedException.none();
52-
5350
@Override
5451
@Before
5552
public void setUp() {
56-
addIpToVmNicCmd = new AddIpToVmNicCmd() {
57-
};
58-
removeIpFromVmNicCmd = new RemoveIpFromVmNicCmd();
53+
5954
}
6055

6156
@Test
@@ -108,8 +103,6 @@ public void testRemoveIpFromVmNicSuccess() throws ResourceAllocationException, R
108103
Mockito.when(networkService.releaseSecondaryIpFromNic(Matchers.anyInt())).thenReturn(true);
109104

110105
removeIpFromNic._networkService = networkService;
111-
successResponseGenerator = Mockito.mock(SuccessResponse.class);
112-
113106
removeIpFromNic.execute();
114107
}
115108

engine/orchestration/src/com/cloud/agent/manager/ClusteredAgentManagerImpl.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -498,18 +498,17 @@ public SocketChannel connectToPeer(String peerName, SocketChannel prevCh) {
498498
} catch (UnknownHostException e) {
499499
throw new CloudRuntimeException("Unable to resolve " + ip);
500500
}
501-
try {
502-
ch = SocketChannel.open(new InetSocketAddress(addr, Port.value()));
503-
ch.configureBlocking(true); // make sure we are working at blocking mode
504-
ch.socket().setKeepAlive(true);
505-
ch.socket().setSoTimeout(60 * 1000);
501+
try (SocketChannel ch1 = SocketChannel.open(new InetSocketAddress(addr, Port.value()));){
502+
ch1.configureBlocking(true); // make sure we are working at blocking mode
503+
ch1.socket().setKeepAlive(true);
504+
ch1.socket().setSoTimeout(60 * 1000);
506505
try {
507506
SSLContext sslContext = Link.initSSLContext(true);
508507
sslEngine = sslContext.createSSLEngine(ip, Port.value());
509508
sslEngine.setUseClientMode(true);
510509
sslEngine.setEnabledProtocols(SSLUtils.getSupportedProtocols(sslEngine.getEnabledProtocols()));
511510

512-
Link.doHandshake(ch, sslEngine, true);
511+
Link.doHandshake(ch1, sslEngine, true);
513512
s_logger.info("SSL: Handshake done");
514513
} catch (Exception e) {
515514
throw new IOException("SSL: Fail to init SSL! " + e);

plugins/hypervisors/vmware/src/com/cloud/storage/resource/VmwareStorageProcessor.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -327,9 +327,10 @@ public Answer copyTemplateToPrimaryStorage(CopyCommand cmd) {
327327
TemplateObjectTO newTemplate = new TemplateObjectTO();
328328

329329
if (managed) {
330-
String path = dsMo.getDatastorePath(managedStoragePoolRootVolumeName + ".vmdk");
331-
332-
newTemplate.setPath(path);
330+
if(dsMo != null) {
331+
String path = dsMo.getDatastorePath(managedStoragePoolRootVolumeName + ".vmdk");
332+
newTemplate.setPath(path);
333+
}
333334
}
334335
else {
335336
newTemplate.setPath(templateUuidName);

plugins/network-elements/juniper-srx/src/com/cloud/network/resource/JuniperSrxResource.java

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -191,16 +191,18 @@ private String getXml(String filename) {
191191
throw new Exception("Failed to find Juniper SRX XML file: " + filename);
192192
}
193193

194-
FileReader fr = new FileReader(xmlFilePath);
195-
BufferedReader br = new BufferedReader(fr);
196-
197-
String xml = "";
198-
String line;
199-
while ((line = br.readLine()) != null) {
200-
xml += line.trim();
194+
try(FileReader fr = new FileReader(xmlFilePath);
195+
BufferedReader br = new BufferedReader(fr);) {
196+
String xml = "";
197+
String line;
198+
while ((line = br.readLine()) != null) {
199+
xml += line.trim();
200+
}
201+
return xml;
202+
}catch (Exception e) {
203+
s_logger.debug(e);
204+
return null;
201205
}
202-
203-
return xml;
204206
} catch (Exception e) {
205207
s_logger.debug(e);
206208
return null;

server/src/com/cloud/api/ApiServer.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -405,12 +405,14 @@ public void handle(final HttpRequest request, final HttpResponse response, final
405405
// Map<String, Collection<String>> parameterMap = paramMultiMap.asMap();
406406
final Map parameterMap = new HashMap<String, String[]>();
407407
String responseType = HttpUtils.RESPONSE_TYPE_XML;
408-
for (final NameValuePair param : paramList) {
409-
if (param.getName().equalsIgnoreCase("response")) {
410-
responseType = param.getValue();
411-
continue;
408+
if(paramList != null) {
409+
for (final NameValuePair param : paramList) {
410+
if (param.getName().equalsIgnoreCase("response")) {
411+
responseType = param.getValue();
412+
continue;
413+
}
414+
parameterMap.put(param.getName(), new String[]{param.getValue()});
412415
}
413-
parameterMap.put(param.getName(), new String[] {param.getValue()});
414416
}
415417

416418
// Get the type of http method being used.

server/src/com/cloud/ha/HighAvailabilityManagerImpl.java

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -265,18 +265,20 @@ public void scheduleRestartForVmsOnHost(final HostVO host, boolean investigate)
265265
"] is down." +
266266
((sb != null) ? sb.toString() : ""));
267267

268-
for (VMInstanceVO vm : vms) {
269-
if (s_logger.isDebugEnabled()) {
270-
s_logger.debug("Notifying HA Mgr of to restart vm " + vm.getId() + "-" + vm.getInstanceName());
271-
}
272-
vm = _instanceDao.findByUuid(vm.getUuid());
273-
Long hostId = vm.getHostId();
274-
if ( hostId != null && !hostId.equals(host.getId()) ) {
275-
s_logger.debug("VM " + vm.getInstanceName() + " is not on down host " + host.getId() + " it is on other host "
276-
+ hostId + " VM HA is done");
277-
continue;
268+
if (vms != null) {
269+
for (VMInstanceVO vm : vms) {
270+
if (s_logger.isDebugEnabled()) {
271+
s_logger.debug("Notifying HA Mgr of to restart vm " + vm.getId() + "-" + vm.getInstanceName());
272+
}
273+
vm = _instanceDao.findByUuid(vm.getUuid());
274+
Long hostId = vm.getHostId();
275+
if (hostId != null && !hostId.equals(host.getId())) {
276+
s_logger.debug("VM " + vm.getInstanceName() + " is not on down host " + host.getId() + " it is on other host "
277+
+ hostId + " VM HA is done");
278+
continue;
279+
}
280+
scheduleRestart(vm, investigate);
278281
}
279-
scheduleRestart(vm, investigate);
280282
}
281283
}
282284

server/src/com/cloud/resource/ResourceManagerImpl.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1630,12 +1630,14 @@ protected HostVO createHostVO(StartupCommand[] cmds, ServerResource resource, Ma
16301630
try {
16311631
clusterId = Long.valueOf(cluster);
16321632
} catch (NumberFormatException e) {
1633-
ClusterVO c = _clusterDao.findBy(cluster, podId);
1634-
if (c == null) {
1635-
c = new ClusterVO(dcId, podId, cluster);
1636-
c = _clusterDao.persist(c);
1633+
if (podId != null) {
1634+
ClusterVO c = _clusterDao.findBy(cluster, podId.longValue());
1635+
if (c == null) {
1636+
c = new ClusterVO(dcId, podId.longValue(), cluster);
1637+
c = _clusterDao.persist(c);
1638+
}
1639+
clusterId = c.getId();
16371640
}
1638-
clusterId = c.getId();
16391641
}
16401642
}
16411643
if (startup instanceof StartupRoutingCommand) {

utils/src/com/cloud/utils/net/NetUtils.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1317,11 +1317,15 @@ public static BigInteger countIp6InRange(String ip6Range) {
13171317
BigInteger startInt = convertIPv6AddressToBigInteger(start);
13181318
BigInteger endInt = convertIPv6AddressToBigInteger(end);
13191319
if (endInt != null) {
1320-
if (startInt.compareTo(endInt) > 0) {
1321-
return null;
1320+
if (startInt != null)
1321+
{
1322+
if(startInt.compareTo(endInt) > 0) {
1323+
return null;
1324+
}
13221325
}
1326+
return endInt.subtract(startInt).add(BigInteger.ONE);
13231327
}
1324-
return endInt.subtract(startInt).add(BigInteger.ONE);
1328+
return null;
13251329
}
13261330

13271331
public static boolean isIp6InRange(String ip6, String ip6Range) {

0 commit comments

Comments
 (0)