Skip to content

Commit ef6ec7b

Browse files
committed
Fixed few coverity issues like invalid boxing unboxing issues, resource leaks, null dereferences
1 parent fc4dcea commit ef6ec7b

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
@@ -497,17 +497,16 @@ public SocketChannel connectToPeer(String peerName, SocketChannel prevCh) {
497497
} catch (UnknownHostException e) {
498498
throw new CloudRuntimeException("Unable to resolve " + ip);
499499
}
500-
try {
501-
ch = SocketChannel.open(new InetSocketAddress(addr, Port.value()));
502-
ch.configureBlocking(true); // make sure we are working at blocking mode
503-
ch.socket().setKeepAlive(true);
504-
ch.socket().setSoTimeout(60 * 1000);
500+
try (SocketChannel ch1 = SocketChannel.open(new InetSocketAddress(addr, Port.value()));){
501+
ch1.configureBlocking(true); // make sure we are working at blocking mode
502+
ch1.socket().setKeepAlive(true);
503+
ch1.socket().setSoTimeout(60 * 1000);
505504
try {
506505
SSLContext sslContext = Link.initSSLContext(true);
507506
sslEngine = sslContext.createSSLEngine(ip, Port.value());
508507
sslEngine.setUseClientMode(true);
509508

510-
Link.doHandshake(ch, sslEngine, true);
509+
Link.doHandshake(ch1, sslEngine, true);
511510
s_logger.info("SSL: Handshake done");
512511
} catch (Exception e) {
513512
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
@@ -336,9 +336,10 @@ public Answer copyTemplateToPrimaryStorage(CopyCommand cmd) {
336336
TemplateObjectTO newTemplate = new TemplateObjectTO();
337337

338338
if (managed) {
339-
String path = dsMo.getDatastorePath(managedStoragePoolRootVolumeName + ".vmdk");
340-
341-
newTemplate.setPath(path);
339+
if(dsMo != null) {
340+
String path = dsMo.getDatastorePath(managedStoragePoolRootVolumeName + ".vmdk");
341+
newTemplate.setPath(path);
342+
}
342343
}
343344
else {
344345
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
@@ -401,12 +401,14 @@ public void handle(final HttpRequest request, final HttpResponse response, final
401401
// Map<String, Collection<String>> parameterMap = paramMultiMap.asMap();
402402
final Map parameterMap = new HashMap<String, String[]>();
403403
String responseType = HttpUtils.RESPONSE_TYPE_XML;
404-
for (final NameValuePair param : paramList) {
405-
if (param.getName().equalsIgnoreCase("response")) {
406-
responseType = param.getValue();
407-
continue;
404+
if(paramList != null) {
405+
for (final NameValuePair param : paramList) {
406+
if (param.getName().equalsIgnoreCase("response")) {
407+
responseType = param.getValue();
408+
continue;
409+
}
410+
parameterMap.put(param.getName(), new String[]{param.getValue()});
408411
}
409-
parameterMap.put(param.getName(), new String[] {param.getValue()});
410412
}
411413

412414
// 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
@@ -1632,12 +1632,14 @@ protected HostVO createHostVO(StartupCommand[] cmds, ServerResource resource, Ma
16321632
try {
16331633
clusterId = Long.valueOf(cluster);
16341634
} catch (NumberFormatException e) {
1635-
ClusterVO c = _clusterDao.findBy(cluster, podId);
1636-
if (c == null) {
1637-
c = new ClusterVO(dcId, podId, cluster);
1638-
c = _clusterDao.persist(c);
1635+
if (podId != null) {
1636+
ClusterVO c = _clusterDao.findBy(cluster, podId.longValue());
1637+
if (c == null) {
1638+
c = new ClusterVO(dcId, podId.longValue(), cluster);
1639+
c = _clusterDao.persist(c);
1640+
}
1641+
clusterId = c.getId();
16391642
}
1640-
clusterId = c.getId();
16411643
}
16421644
}
16431645

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)