Skip to content

Commit 2774b62

Browse files
wilderrodriguesDaanHoogland
authored andcommitted
Fixing bugs from Coverity related to Dereferenced Null after check and as return value.
Signed-off-by: Daan Hoogland <daan@onecht.net>
1 parent c781e3b commit 2774b62

6 files changed

Lines changed: 160 additions & 143 deletions

File tree

agent/src/com/cloud/agent/AgentShell.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,8 @@ public void setPersistentProperty(String prefix, String name, String value) {
167167

168168
void loadProperties() throws ConfigurationException {
169169
final File file = PropertiesUtil.findConfigFile("agent.properties");
170-
if (file == null) {
170+
171+
if (null == file) {
171172
throw new ConfigurationException("Unable to find agent.properties.");
172173
}
173174

@@ -303,12 +304,17 @@ public void init(String[] args) throws ConfigurationException {
303304
// For KVM agent, do it specially here
304305

305306
File file = new File("/etc/cloudstack/agent/log4j-cloud.xml");
306-
if (!file.exists()) {
307+
if(!file.exists()) {
307308
file = PropertiesUtil.findConfigFile("log4j-cloud.xml");
308309
}
309-
DOMConfigurator.configureAndWatch(file.getAbsolutePath());
310310

311-
s_logger.info("Agent started");
311+
if (null != file) {
312+
DOMConfigurator.configureAndWatch(file.getAbsolutePath());
313+
314+
s_logger.info("Agent started");
315+
} else {
316+
s_logger.error("Could not start the Agent because the absolut path of the \"log4j-cloud.xml\" file cannot be determined.");
317+
}
312318

313319
final Class<?> c = this.getClass();
314320
_version = c.getPackage().getImplementationVersion();

plugins/network-elements/juniper-contrail/src/org/apache/cloudstack/network/contrail/management/ServiceManagerImpl.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,11 @@
2929
import net.juniper.contrail.api.types.ServiceInstance;
3030
import net.juniper.contrail.api.types.VirtualNetwork;
3131

32-
import org.apache.log4j.Logger;
33-
34-
import com.google.gson.Gson;
35-
3632
import org.apache.cloudstack.context.CallContext;
3733
import org.apache.cloudstack.network.contrail.api.response.ServiceInstanceResponse;
3834
import org.apache.cloudstack.network.contrail.model.ServiceInstanceModel;
3935
import org.apache.cloudstack.network.contrail.model.VirtualMachineModel;
36+
import org.apache.log4j.Logger;
4037

4138
import com.cloud.api.ApiDBUtils;
4239
import com.cloud.dc.DataCenter;
@@ -61,6 +58,7 @@
6158
import com.cloud.vm.VirtualMachineManager;
6259
import com.cloud.vm.VirtualMachineName;
6360
import com.cloud.vm.dao.UserVmDao;
61+
import com.google.gson.Gson;
6462

6563
@Local(value = {ServiceManager.class})
6664
public class ServiceManagerImpl implements ServiceManager {
@@ -98,7 +96,7 @@ public class ServiceManagerImpl implements ServiceManager {
9896
*/
9997
@ActionEvent(eventType = EventTypes.EVENT_VM_CREATE, eventDescription = "createServiceInstance", create = true)
10098
private ServiceVirtualMachine createServiceVM(DataCenter zone, Account owner, VirtualMachineTemplate template, ServiceOffering serviceOffering, String name,
101-
ServiceInstance siObj, Network left, Network right) {
99+
ServiceInstance siObj, Network left, Network right) {
102100
long id = _vmDao.getNextInSequence(Long.class, "id");
103101

104102
DataCenterDeployment plan = new DataCenterDeployment(zone.getId());
@@ -111,8 +109,8 @@ private ServiceVirtualMachine createServiceVM(DataCenter zone, Account owner, Vi
111109

112110
String instanceName = VirtualMachineName.getVmName(id, owner.getId(), "SRV");
113111
ServiceVirtualMachine svm =
114-
new ServiceVirtualMachine(id, instanceName, name, template.getId(), serviceOffering.getId(), template.getHypervisorType(), template.getGuestOSId(),
115-
zone.getId(), owner.getDomainId(), owner.getAccountId(), false);
112+
new ServiceVirtualMachine(id, instanceName, name, template.getId(), serviceOffering.getId(), template.getHypervisorType(), template.getGuestOSId(),
113+
zone.getId(), owner.getDomainId(), owner.getAccountId(), false);
116114

117115
// database synchronization code must be able to distinguish service instance VMs.
118116
Map<String, String> kvmap = new HashMap<String, String>();
@@ -132,7 +130,7 @@ private ServiceVirtualMachine createServiceVM(DataCenter zone, Account owner, Vi
132130

133131
@Override
134132
public ServiceVirtualMachine createServiceInstance(DataCenter zone, Account owner, VirtualMachineTemplate template, ServiceOffering serviceOffering, String name,
135-
Network left, Network right) {
133+
Network left, Network right) {
136134
s_logger.debug("createServiceInstance by " + owner.getAccountName());
137135
// TODO: permission model.
138136
// service instances need to be able to access the public network.
@@ -227,10 +225,12 @@ public void startServiceInstance(long instanceId) {
227225
@Override
228226
public ServiceInstanceResponse createServiceInstanceResponse(long instanceId) {
229227
s_logger.debug("ServiceInstance response for id: " + instanceId);
228+
230229
UserVmVO vm = _vmDao.findById(instanceId);
231230
ServiceInstanceResponse response = new ServiceInstanceResponse();
232231
response.setId(vm.getUuid());
233232
Account owner = _accountService.getAccount(vm.getAccountId());
233+
234234
if (owner.getType() == Account.ACCOUNT_TYPE_PROJECT) {
235235
Project project = ApiDBUtils.findProjectByProjectAccountIdIncludingRemoved(owner.getAccountId());
236236
response.setProjectId(project.getUuid());

plugins/network-elements/juniper-contrail/src/org/apache/cloudstack/network/contrail/model/VirtualMachineModel.java

Lines changed: 35 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,8 @@
2727
import net.juniper.contrail.api.types.ServiceInstance;
2828
import net.juniper.contrail.api.types.VirtualMachine;
2929

30-
import org.apache.log4j.Logger;
31-
32-
import com.google.gson.Gson;
33-
import com.google.gson.reflect.TypeToken;
34-
3530
import org.apache.cloudstack.network.contrail.management.ContrailManager;
31+
import org.apache.log4j.Logger;
3632

3733
import com.cloud.exception.InternalErrorException;
3834
import com.cloud.network.dao.NetworkDao;
@@ -42,11 +38,13 @@
4238
import com.cloud.vm.NicVO;
4339
import com.cloud.vm.VMInstanceVO;
4440
import com.cloud.vm.dao.NicDao;
41+
import com.google.gson.Gson;
42+
import com.google.gson.reflect.TypeToken;
4543

4644
public class VirtualMachineModel extends ModelObjectBase {
4745
private static final Logger s_logger = Logger.getLogger(VirtualMachineModel.class);
4846

49-
private String _uuid;
47+
private final String _uuid;
5048
private long _instanceId;
5149

5250
/*
@@ -113,8 +111,12 @@ private void buildServiceInstance(ModelController controller, String serviceUuid
113111
throw new CloudRuntimeException("Unable to read service-instance object", ex);
114112
}
115113
if (siObj == null) {
114+
//If the ServiceInstance object is null, do not call build. It will break in many places. Instead, call update passing the controller as parameter.
115+
//It will then create a new ServiceInstance is that's null.
116116
siModel = new ServiceInstanceModel(serviceUuid);
117-
siModel.build(controller, siObj);
117+
siModel.update(controller);
118+
119+
siObj = siModel.getServiceInstance();
118120
}
119121
}
120122
_serviceModel = siModel;
@@ -197,21 +199,21 @@ public boolean isActive() {
197199

198200
boolean isActiveInstance(VMInstanceVO instance) {
199201
switch (instance.getState()) {
200-
case Migrating:
201-
case Starting:
202-
case Running:
203-
case Shutdowned:
204-
case Stopped:
205-
case Stopping:
206-
return true;
207-
208-
case Destroyed:
209-
case Error:
210-
case Expunging:
211-
return false;
212-
213-
default:
214-
s_logger.warn("Unknown VMInstance state " + instance.getState().getDescription());
202+
case Migrating:
203+
case Starting:
204+
case Running:
205+
case Shutdowned:
206+
case Stopped:
207+
case Stopping:
208+
return true;
209+
210+
case Destroyed:
211+
case Error:
212+
case Expunging:
213+
return false;
214+
215+
default:
216+
s_logger.warn("Unknown VMInstance state " + instance.getState().getDescription());
215217
}
216218
return true;
217219
}
@@ -255,17 +257,17 @@ private void setServiceInstanceNics(ModelController controller, VMInstanceVO ins
255257
String tag;
256258

257259
switch (nic.getDeviceId()) {
258-
case 0:
259-
tag = "management";
260-
break;
261-
case 1:
262-
tag = "left";
263-
break;
264-
case 2:
265-
tag = "right";
266-
break;
267-
default:
268-
tag = null;
260+
case 0:
261+
tag = "management";
262+
break;
263+
case 1:
264+
tag = "left";
265+
break;
266+
case 2:
267+
tag = "right";
268+
break;
269+
default:
270+
tag = null;
269271
}
270272

271273
VMInterfaceModel vmiModel = getVMInterface(nic.getUuid());

plugins/user-authenticators/ldap/src/org/apache/cloudstack/api/command/LdapImportUsersCmd.java

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,6 @@
2525

2626
import javax.inject.Inject;
2727

28-
import org.apache.commons.lang.StringUtils;
29-
import org.apache.log4j.Logger;
30-
import org.bouncycastle.util.encoders.Base64;
31-
3228
import org.apache.cloudstack.api.APICommand;
3329
import org.apache.cloudstack.api.ApiConstants;
3430
import org.apache.cloudstack.api.ApiErrorCode;
@@ -41,6 +37,9 @@
4137
import org.apache.cloudstack.ldap.LdapManager;
4238
import org.apache.cloudstack.ldap.LdapUser;
4339
import org.apache.cloudstack.ldap.NoLdapUserMatchingQueryException;
40+
import org.apache.commons.lang.StringUtils;
41+
import org.apache.log4j.Logger;
42+
import org.bouncycastle.util.encoders.Base64;
4443

4544
import com.cloud.domain.Domain;
4645
import com.cloud.exception.ConcurrentOperationException;
@@ -60,29 +59,29 @@ public class LdapImportUsersCmd extends BaseListCmd {
6059
private static final String s_name = "ldapuserresponse";
6160

6261
@Parameter(name = ApiConstants.TIMEZONE,
63-
type = CommandType.STRING,
64-
description = "Specifies a timezone for this command. For more information on the timezone parameter, see Time Zone Format.")
62+
type = CommandType.STRING,
63+
description = "Specifies a timezone for this command. For more information on the timezone parameter, see Time Zone Format.")
6564
private String timezone;
6665

6766
@Parameter(name = ApiConstants.ACCOUNT_TYPE,
68-
type = CommandType.SHORT,
69-
required = true,
70-
description = "Type of the account. Specify 0 for user, 1 for root admin, and 2 for domain admin")
67+
type = CommandType.SHORT,
68+
required = true,
69+
description = "Type of the account. Specify 0 for user, 1 for root admin, and 2 for domain admin")
7170
private Short accountType;
7271

7372
@Parameter(name = ApiConstants.ACCOUNT_DETAILS, type = CommandType.MAP, description = "details for account used to store specific parameters")
7473
private Map<String, String> details;
7574

7675
@Parameter(name = ApiConstants.DOMAIN_ID,
77-
type = CommandType.UUID,
78-
entityType = DomainResponse.class,
79-
description = "Specifies the domain to which the ldap users are to be "
80-
+ "imported. If no domain is specified, a domain will created using group parameter. If the group is also not specified, a domain name based on the OU information will be "
81-
+ "created. If no OU hierarchy exists, will be defaulted to ROOT domain")
76+
type = CommandType.UUID,
77+
entityType = DomainResponse.class,
78+
description = "Specifies the domain to which the ldap users are to be "
79+
+ "imported. If no domain is specified, a domain will created using group parameter. If the group is also not specified, a domain name based on the OU information will be "
80+
+ "created. If no OU hierarchy exists, will be defaulted to ROOT domain")
8281
private Long domainId;
8382

8483
@Parameter(name = ApiConstants.GROUP, type = CommandType.STRING, description = "Specifies the group name from which the ldap users are to be imported. "
85-
+ "If no group is specified, all the users will be imported.")
84+
+ "If no group is specified, all the users will be imported.")
8685
private String groupName;
8786

8887
private Domain _domain;
@@ -103,11 +102,12 @@ public LdapImportUsersCmd(final LdapManager ldapManager, final DomainService dom
103102

104103
@Override
105104
public void execute() throws ResourceUnavailableException, InsufficientCapacityException, ServerApiException, ConcurrentOperationException,
106-
ResourceAllocationException, NetworkRuleConflictException {
105+
ResourceAllocationException, NetworkRuleConflictException {
107106

108107
List<LdapUser> users;
109108
try {
110109
if (StringUtils.isNotBlank(groupName)) {
110+
111111
users = _ldapManager.getUsersInGroup(groupName);
112112
} else {
113113
users = _ldapManager.getUsers();
@@ -122,7 +122,7 @@ public void execute() throws ResourceUnavailableException, InsufficientCapacityE
122122
Domain domain = getDomain(user);
123123
try {
124124
_accountService.createUserAccount(user.getUsername(), generatePassword(), user.getFirstname(), user.getLastname(), user.getEmail(), timezone,
125-
user.getUsername(), accountType, domain.getId(), domain.getNetworkDomain(), details, UUID.randomUUID().toString(), UUID.randomUUID().toString());
125+
user.getUsername(), accountType, domain.getId(), domain.getNetworkDomain(), details, UUID.randomUUID().toString(), UUID.randomUUID().toString());
126126
addedUsers.add(user);
127127
} catch (InvalidParameterValueException ex) {
128128
s_logger.error("Failed to create user with username: " + user.getUsername() + " ::: " + ex.getMessage());
@@ -194,7 +194,8 @@ private String generatePassword() throws ServerApiException {
194194
final SecureRandom randomGen = SecureRandom.getInstance("SHA1PRNG");
195195
final byte bytes[] = new byte[20];
196196
randomGen.nextBytes(bytes);
197-
return Base64.encode(bytes).toString();
197+
String encodedPassword = new String(Base64.encode(bytes));
198+
return encodedPassword;
198199
} catch (final NoSuchAlgorithmException e) {
199200
throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to generate random password");
200201
}

0 commit comments

Comments
 (0)