Skip to content

Commit 4c42aaf

Browse files
brettKKrafaelweingartner
authored andcommitted
[CLOUDSTACK-10356] Fix NPE in Cloudstack found with NPEDetector (#2573)
* fix https://issues.apache.org/jira/browse/CLOUDSTACK-10356 * del patch file * Update ResourceCountDaoImpl.java * fix some format * fix code * fix error message in VolumeOrchestrator * add check null stmt * del import unuse class * use BooleanUtils to check Boolean * fix error message * delete unuse function * delete the deprecated function updateDomainCount * add error log and throw exception in ProjectManagerImpl.java
1 parent efcd24c commit 4c42aaf

11 files changed

Lines changed: 49 additions & 31 deletions

File tree

engine/orchestration/src/main/java/org/apache/cloudstack/engine/orchestration/VolumeOrchestrator.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,9 @@ public VolumeInfo copyVolumeFromSecToPrimary(VolumeInfo volume, VirtualMachine v
499499

500500
// Find a suitable storage to create volume on
501501
StoragePool destPool = findStoragePool(dskCh, dc, pod, clusterId, null, vm, avoidPools);
502+
if (destPool == null) {
503+
throw new CloudRuntimeException("Failed to find a suitable storage pool to create Volume in the pod/cluster of the provided VM "+ vm.getUuid());
504+
}
502505
DataStore destStore = dataStoreMgr.getDataStore(destPool.getId(), DataStoreRole.Primary);
503506
AsyncCallFuture<VolumeApiResult> future = volService.copyVolume(volume, destStore);
504507

engine/schema/src/main/java/com/cloud/configuration/dao/ResourceCountDao.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,6 @@ public interface ResourceCountDao extends GenericDao<ResourceCountVO, Long> {
3939
*/
4040
void setResourceCount(long ownerId, ResourceOwnerType ownerType, ResourceType type, long count);
4141

42-
@Deprecated
43-
void updateDomainCount(long domainId, ResourceType type, boolean increment, long delta);
44-
4542
boolean updateById(long id, boolean increment, long delta);
4643

4744
void createResourceCounts(long ownerId, ResourceOwnerType ownerType);

engine/schema/src/main/java/com/cloud/configuration/dao/ResourceCountDaoImpl.java

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -120,16 +120,6 @@ public void setResourceCount(long ownerId, ResourceOwnerType ownerType, Resource
120120
}
121121
}
122122

123-
@Override
124-
@Deprecated
125-
public void updateDomainCount(long domainId, ResourceType type, boolean increment, long delta) {
126-
delta = increment ? delta : delta * -1;
127-
128-
ResourceCountVO resourceCountVO = findByOwnerAndType(domainId, ResourceOwnerType.Domain, type);
129-
resourceCountVO.setCount(resourceCountVO.getCount() + delta);
130-
update(resourceCountVO.getId(), resourceCountVO);
131-
}
132-
133123
@Override
134124
public boolean updateById(long id, boolean increment, long delta) {
135125
delta = increment ? delta : delta * -1;

plugins/deployment-planners/implicit-dedication/src/main/java/com/cloud/deploy/ImplicitDedicationPlanner.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import com.cloud.utils.NumbersUtil;
4040
import com.cloud.vm.VMInstanceVO;
4141
import com.cloud.vm.VirtualMachineProfile;
42+
import org.springframework.util.CollectionUtils;
4243

4344
public class ImplicitDedicationPlanner extends FirstFitPlanner implements DeploymentClusterPlanner {
4445

@@ -256,14 +257,15 @@ public PlannerResourceUsage getResourceUsage(VirtualMachineProfile vmProfile, De
256257

257258
// Get the list of all the hosts in the given clusters
258259
List<Long> allHosts = new ArrayList<Long>();
259-
for (Long cluster : clusterList) {
260-
List<HostVO> hostsInCluster = resourceMgr.listAllHostsInCluster(cluster);
261-
for (HostVO hostVO : hostsInCluster) {
260+
if (!CollectionUtils.isEmpty(clusterList)) {
261+
for (Long cluster : clusterList) {
262+
List<HostVO> hostsInCluster = resourceMgr.listAllHostsInCluster(cluster);
263+
for (HostVO hostVO : hostsInCluster) {
262264

263-
allHosts.add(hostVO.getId());
265+
allHosts.add(hostVO.getId());
266+
}
264267
}
265268
}
266-
267269
// Go over all the hosts in the cluster and get a list of
268270
// 1. All empty hosts, not running any vms.
269271
// 2. Hosts running vms for this account and created by a service

plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/LibvirtComputingResource.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2339,7 +2339,10 @@ public int compare(final DiskTO arg0, final DiskTO arg1) {
23392339
disk.setCacheMode(DiskDef.DiskCacheMode.valueOf(volumeObjectTO.getCacheMode().toString().toUpperCase()));
23402340
}
23412341
}
2342-
2342+
if (vm.getDevices() == null) {
2343+
s_logger.error("There is no devices for" + vm);
2344+
throw new RuntimeException("There is no devices for" + vm);
2345+
}
23432346
vm.getDevices().addDevice(disk);
23442347
}
23452348

@@ -2393,7 +2396,10 @@ private void createVif(final LibvirtVMDef vm, final NicTO nic, final String nicA
23932396
+ ") is " + nic.getType() + " traffic type. So, vsp-vr-ip " + vrIp + " is set in the metadata");
23942397
}
23952398
}
2396-
2399+
if (vm.getDevices() == null) {
2400+
s_logger.error("LibvirtVMDef object get devices with null result");
2401+
throw new InternalErrorException("LibvirtVMDef object get devices with null result");
2402+
}
23972403
vm.getDevices().addDevice(getVifDriver(nic.getType(), nic.getName()).plug(nic, vm.getPlatformEmulator(), nicAdapter));
23982404
}
23992405

plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/storage/LibvirtStorageAdaptor.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -522,7 +522,9 @@ public KVMStoragePool createStoragePool(String name, String host, int port, Stri
522522
s_logger.debug("Checking path of existing pool " + poolname + " against pool we want to create");
523523
StoragePool p = conn.storagePoolLookupByName(poolname);
524524
LibvirtStoragePoolDef pdef = getStoragePoolDef(conn, p);
525-
525+
if (pdef == null) {
526+
throw new CloudRuntimeException("Unable to parse the storage pool definition for storage pool " + poolname);
527+
}
526528
String targetPath = pdef.getTargetPath();
527529
if (targetPath != null && targetPath.equals(path)) {
528530
s_logger.debug("Storage pool utilizing path '" + path + "' already exists as pool " + poolname +

server/src/main/java/com/cloud/network/router/VirtualNetworkApplianceManagerImpl.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2139,6 +2139,12 @@ public boolean startRemoteAccessVpn(final Network network, final RemoteAccessVpn
21392139
}
21402140

21412141
Answer answer = cmds.getAnswer("users");
2142+
if (answer == null) {
2143+
s_logger.error("Unable to start vpn: unable add users to vpn in zone " + router.getDataCenterId() + " for account " + vpn.getAccountId() + " on domR: "
2144+
+ router.getInstanceName() + " due to null answer");
2145+
throw new ResourceUnavailableException("Unable to start vpn in zone " + router.getDataCenterId() + " for account " + vpn.getAccountId() + " on domR: "
2146+
+ router.getInstanceName() + " due to null answer", DataCenter.class, router.getDataCenterId());
2147+
}
21422148
if (!answer.getResult()) {
21432149
s_logger.error("Unable to start vpn: unable add users to vpn in zone " + router.getDataCenterId() + " for account " + vpn.getAccountId() + " on domR: "
21442150
+ router.getInstanceName() + " due to " + answer.getDetails());

server/src/main/java/com/cloud/network/router/VpcVirtualNetworkApplianceManagerImpl.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -740,18 +740,20 @@ public boolean startRemoteAccessVpn(final RemoteAccessVpn vpn, final VirtualRout
740740
throw new AgentUnavailableException("Unable to send commands to virtual router ", router.getHostId(), e);
741741
}
742742
Answer answer = cmds.getAnswer("users");
743-
if (!answer.getResult()) {
743+
if (answer == null || !answer.getResult()) {
744+
String errorMessage = (answer == null) ? "null answer object" : answer.getDetails();
744745
s_logger.error("Unable to start vpn: unable add users to vpn in zone " + router.getDataCenterId() + " for account " + vpn.getAccountId() + " on domR: "
745-
+ router.getInstanceName() + " due to " + answer.getDetails());
746+
+ router.getInstanceName() + " due to " + errorMessage);
746747
throw new ResourceUnavailableException("Unable to start vpn: Unable to add users to vpn in zone " + router.getDataCenterId() + " for account " + vpn.getAccountId()
747-
+ " on domR: " + router.getInstanceName() + " due to " + answer.getDetails(), DataCenter.class, router.getDataCenterId());
748+
+ " on domR: " + router.getInstanceName() + " due to " + errorMessage, DataCenter.class, router.getDataCenterId());
748749
}
749750
answer = cmds.getAnswer("startVpn");
750-
if (!answer.getResult()) {
751+
if (answer == null || !answer.getResult()) {
752+
String errorMessage = (answer == null) ? "null answer object" : answer.getDetails();
751753
s_logger.error("Unable to start vpn in zone " + router.getDataCenterId() + " for account " + vpn.getAccountId() + " on domR: " + router.getInstanceName() + " due to "
752-
+ answer.getDetails());
754+
+ errorMessage);
753755
throw new ResourceUnavailableException("Unable to start vpn in zone " + router.getDataCenterId() + " for account " + vpn.getAccountId() + " on domR: "
754-
+ router.getInstanceName() + " due to " + answer.getDetails(), DataCenter.class, router.getDataCenterId());
756+
+ router.getInstanceName() + " due to " + errorMessage, DataCenter.class, router.getDataCenterId());
755757
}
756758

757759
return true;

server/src/main/java/com/cloud/projects/ProjectManagerImpl.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,10 @@ public void doInTransactionWithoutResult(TransactionStatus status) throws Resour
479479
throw new InvalidParameterValueException("Unable to find account name=" + newOwnerName + " in domain id=" + project.getDomainId());
480480
}
481481
Account currentOwnerAccount = getProjectOwner(projectId);
482+
if (currentOwnerAccount == null) {
483+
s_logger.error("Unable to find the current owner for the project id=" + projectId);
484+
throw new InvalidParameterValueException("Unable to find the current owner for the project id=" + projectId);
485+
}
482486
if (currentOwnerAccount.getId() != futureOwnerAccount.getId()) {
483487
ProjectAccountVO futureOwner = _projectAccountDao.findByProjectIdAccountId(projectId, futureOwnerAccount.getAccountId());
484488
if (futureOwner == null) {

server/src/main/java/com/cloud/template/TemplateManagerImpl.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1232,7 +1232,10 @@ private boolean attachISOToVM(long vmId, long isoId, boolean attach) {
12321232

12331233
// prepare ISO ready to mount on hypervisor resource level
12341234
TemplateInfo tmplt = prepareIso(isoId, vm.getDataCenterId(), vm.getHostId(), null);
1235-
1235+
if (tmplt == null) {
1236+
s_logger.error("Failed to prepare ISO ready to mount on hypervisor resource level");
1237+
throw new CloudRuntimeException("Failed to prepare ISO ready to mount on hypervisor resource level");
1238+
}
12361239
String vmName = vm.getInstanceName();
12371240

12381241
HostVO host = _hostDao.findById(vm.getHostId());

0 commit comments

Comments
 (0)