Skip to content

Commit 97d296b

Browse files
sedukullDaanHoogland
authored andcommitted
Fixed Coverity reported performance issues like inefficient string concatenations, wrong boxing or unboxing types, inefficent map element retrievals
Signed-off-by: Daan Hoogland <daan@onecht.net>
1 parent 667347d commit 97d296b

25 files changed

Lines changed: 127 additions & 98 deletions

File tree

api/src/org/apache/cloudstack/api/command/admin/systemvm/ScaleSystemVMCmd.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@ public Map<String, String> getDetails() {
8686
Iterator iter = parameterCollection.iterator();
8787
while (iter.hasNext()) {
8888
HashMap<String, String> value = (HashMap<String, String>)iter.next();
89-
for (String key : value.keySet()) {
90-
customparameterMap.put(key, value.get(key));
89+
for (Map.Entry<String, String> entry : value.entrySet()) {
90+
customparameterMap.put(entry.getKey(), entry.getValue());
9191
}
9292
}
9393
}

api/src/org/apache/cloudstack/api/command/admin/systemvm/UpgradeSystemVMCmd.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,8 @@ public Map<String, String> getDetails() {
8181
Iterator iter = parameterCollection.iterator();
8282
while (iter.hasNext()) {
8383
HashMap<String, String> value = (HashMap<String, String>)iter.next();
84-
for (String key : value.keySet()) {
85-
customparameterMap.put(key, value.get(key));
84+
for (Map.Entry<String,String>entry : value.entrySet()) {
85+
customparameterMap.put(entry.getKey(), entry.getValue());
8686
}
8787
}
8888
}

api/src/org/apache/cloudstack/context/CallContext.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -332,8 +332,8 @@ public Map<Object, Object> getContextParameters() {
332332

333333
public void putContextParameters(Map<Object, Object> details){
334334
if (details == null) return;
335-
for(Object key : details.keySet()){
336-
putContextParameter(key, details.get(key));
335+
for(Map.Entry<Object,Object>entry : details.entrySet()){
336+
putContextParameter(entry.getKey(), entry.getValue());
337337
}
338338
}
339339

core/src/com/cloud/agent/resource/virtualnetwork/VirtualRoutingResource.java

Lines changed: 37 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -476,15 +476,14 @@ private List<ConfigItem> generateConfig(LoadBalancerConfigCommand cmd) {
476476
LoadBalancerConfigurator cfgtr = new HAProxyConfigurator();
477477

478478
String[] config = cfgtr.generateConfiguration(cmd);
479-
String tmpCfgFileContents = "";
479+
StringBuffer buff = new StringBuffer();
480480
for (int i = 0; i < config.length; i++) {
481-
tmpCfgFileContents += config[i];
482-
tmpCfgFileContents += "\n";
481+
buff.append(config[i]);
482+
buff.append("\n");
483483
}
484-
485484
String tmpCfgFilePath = "/etc/haproxy/";
486485
String tmpCfgFileName = "haproxy.cfg.new." + String.valueOf(System.currentTimeMillis());
487-
cfg.add(new ConfigItem(tmpCfgFilePath, tmpCfgFileName, tmpCfgFileContents));
486+
cfg.add(new ConfigItem(tmpCfgFilePath, tmpCfgFileName, buff.toString()));
488487

489488
String[][] rules = cfgtr.generateFwRules(cmd);
490489

@@ -610,41 +609,58 @@ private List<ConfigItem> generateConfig(DeleteIpAliasCommand cmd) {
610609
LinkedList<ConfigItem> cfg = new LinkedList<>();
611610

612611
String args = "";
612+
StringBuffer buff = new StringBuffer();
613613
List<IpAliasTO> revokedIpAliasTOs = cmd.getDeleteIpAliasTos();
614614
for (IpAliasTO ipAliasTO : revokedIpAliasTOs) {
615-
args = args + ipAliasTO.getAlias_count() + ":" + ipAliasTO.getRouterip() + ":" + ipAliasTO.getNetmask() + "-";
616-
}
615+
buff.append(ipAliasTO.getAlias_count());
616+
buff.append(":");
617+
buff.append(ipAliasTO.getRouterip());
618+
buff.append(":");
619+
buff.append(ipAliasTO.getNetmask());
620+
buff.append("-");
621+
}
617622
//this is to ensure that thre is some argument passed to the deleteipAlias script when there are no revoked rules.
618-
args = args + "- ";
623+
buff.append("- ");
619624
List<IpAliasTO> activeIpAliasTOs = cmd.getCreateIpAliasTos();
620625
for (IpAliasTO ipAliasTO : activeIpAliasTOs) {
621-
args = args + ipAliasTO.getAlias_count() + ":" + ipAliasTO.getRouterip() + ":" + ipAliasTO.getNetmask() + "-";
622-
}
623-
624-
cfg.add(new ConfigItem(VRScripts.IPALIAS_DELETE, args));
626+
buff.append(ipAliasTO.getAlias_count());
627+
buff.append(":");
628+
buff.append(ipAliasTO.getRouterip());
629+
buff.append(":");
630+
buff.append(ipAliasTO.getNetmask());
631+
buff.append("-");
632+
}
633+
cfg.add(new ConfigItem(VRScripts.IPALIAS_DELETE, buff.toString()));
625634
return cfg;
626635
}
627636

628637
private List<ConfigItem> generateConfig(DnsMasqConfigCommand cmd) {
629638
LinkedList<ConfigItem> cfg = new LinkedList<>();
630639

631640
List<DhcpTO> dhcpTos = cmd.getIps();
632-
String args = "";
641+
StringBuffer buff = new StringBuffer();
633642
for (DhcpTO dhcpTo : dhcpTos) {
634-
args = args + dhcpTo.getRouterIp() + ":" + dhcpTo.getGateway() + ":" + dhcpTo.getNetmask() + ":" + dhcpTo.getStartIpOfSubnet() + "-";
635-
}
636-
637-
cfg.add(new ConfigItem(VRScripts.DNSMASQ_CONFIG, args));
643+
buff.append(dhcpTo.getRouterIp());
644+
buff.append(":");
645+
buff.append(dhcpTo.getGateway());
646+
buff.append(":");
647+
buff.append(dhcpTo.getNetmask());
648+
buff.append(":");
649+
buff.append(dhcpTo.getStartIpOfSubnet());
650+
buff.append("-");
651+
}
652+
cfg.add(new ConfigItem(VRScripts.DNSMASQ_CONFIG, buff.toString()));
638653
return cfg;
639654
}
640655

641656
private CheckS2SVpnConnectionsAnswer execute(CheckS2SVpnConnectionsCommand cmd) {
642-
String args = "";
657+
658+
StringBuffer buff = new StringBuffer();
643659
for (String ip : cmd.getVpnIps()) {
644-
args += ip + " ";
660+
buff.append(ip);
661+
buff.append(" ");
645662
}
646-
647-
ExecutionResult result = _vrDeployer.executeInVR(cmd.getRouterAccessIp(), VRScripts.S2SVPN_CHECK, args);
663+
ExecutionResult result = _vrDeployer.executeInVR(cmd.getRouterAccessIp(), VRScripts.S2SVPN_CHECK, buff.toString());
648664
return new CheckS2SVpnConnectionsAnswer(cmd, result.isSuccess(), result.getDetails());
649665
}
650666

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ protected AgentAttache(final AgentManagerImpl agentMgr, final long id, final Str
126126
_maintenance = maintenance;
127127
_requests = new LinkedList<Request>();
128128
_agentMgr = agentMgr;
129-
_nextSequence = new Long(s_rand.nextInt(Short.MAX_VALUE)) << 48;
129+
_nextSequence = new Long(s_rand.nextInt(Short.MAX_VALUE)).longValue() << 48;
130130
}
131131

132132
public synchronized long getNextSequence() {

plugins/hypervisors/xenserver/src/com/cloud/hypervisor/xenserver/resource/CitrixResourceBase.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -643,8 +643,8 @@ protected String callHostPluginAsync(Connection conn, String plugin,
643643
Map<String, String> args = new HashMap<String, String>();
644644
Task task = null;
645645
try {
646-
for (String key : params.keySet()) {
647-
args.put(key, params.get(key));
646+
for (Map.Entry< String, String > entry : params.entrySet()) {
647+
args.put(entry.getKey(), entry.getValue());
648648
}
649649
if (s_logger.isTraceEnabled()) {
650650
s_logger.trace("callHostPlugin executing for command " + cmd
@@ -2313,8 +2313,8 @@ protected GetVmStatsAnswer execute(GetVmStatsCommand cmd) {
23132313
return new GetVmStatsAnswer(cmd, vmStatsNameMap);
23142314
}
23152315

2316-
for (String vmUUID : vmStatsUUIDMap.keySet()) {
2317-
vmStatsNameMap.put(vmNames.get(vmUUIDs.indexOf(vmUUID)), vmStatsUUIDMap.get(vmUUID));
2316+
for (Map.Entry<String,VmStatsEntry>entry : vmStatsUUIDMap.entrySet()) {
2317+
vmStatsNameMap.put(vmNames.get(vmUUIDs.indexOf(entry.getKey())), entry.getValue());
23182318
}
23192319

23202320
return new GetVmStatsAnswer(cmd, vmStatsNameMap);
@@ -2387,8 +2387,8 @@ protected HashMap<String, VmStatsEntry> getVmStats(Connection conn, GetVmStatsCo
23872387

23882388
}
23892389

2390-
for (String vmUUID : vmResponseMap.keySet()) {
2391-
VmStatsEntry vmStatsAnswer = vmResponseMap.get(vmUUID);
2390+
for (Map.Entry<String, VmStatsEntry> entry: vmResponseMap.entrySet()) {
2391+
VmStatsEntry vmStatsAnswer = entry.getValue();
23922392

23932393
if (vmStatsAnswer.getNumCPUs() != 0) {
23942394
vmStatsAnswer.setCPUUtilization(vmStatsAnswer.getCPUUtilization() / vmStatsAnswer.getNumCPUs());
@@ -6533,8 +6533,9 @@ private VM createWorkingVM(Connection conn, String vmName, String guestOSType, L
65336533
s_logger.warn("Unable to find vdi by uuid: " + vdiUuid + ", skip it");
65346534
}
65356535
}
6536-
for (VDI vdi : vdiMap.keySet()) {
6537-
VolumeObjectTO volumeTO = vdiMap.get(vdi);
6536+
for (Map.Entry<VDI, VolumeObjectTO>entry : vdiMap.entrySet()) {
6537+
VDI vdi = entry.getKey();
6538+
VolumeObjectTO volumeTO = entry.getValue();
65386539
VBD.Record vbdr = new VBD.Record();
65396540
vbdr.VM = vm;
65406541
vbdr.VDI = vdi;

plugins/hypervisors/xenserver/src/com/cloud/hypervisor/xenserver/resource/XenServerPoolVms.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,10 @@ public int size(String clusterId) {
8080
public String toString() {
8181
StringBuilder sbuf = new StringBuilder("PoolVms=");
8282
for (HashMap<String/* vm name */, Pair<String/* host uuid */, State/* vm state */>> clusterVM : _clusterVms.values()) {
83-
for (String vmname : clusterVM.keySet()) {
84-
sbuf.append(vmname).append("-").append(clusterVM.get(vmname).second()).append(",");
83+
for (Map.Entry<String,Pair<String,State>> entry: clusterVM.entrySet()) {
84+
String vmname = entry.getKey();
85+
Pair<String,State> vmstate= entry.getValue();
86+
sbuf.append(vmname).append("-").append(vmstate.second()).append(",");
8587
}
8688
}
8789
return sbuf.toString();

plugins/network-elements/netscaler/src/com/cloud/network/element/NetscalerElement.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -677,11 +677,13 @@ public boolean verifyServicesCombination(Set<Service> services) {
677677
// NetScaler can only act as Lb and Static Nat service provider
678678
if (services != null && !services.isEmpty() && !netscalerServices.containsAll(services)) {
679679
s_logger.warn("NetScaler network element can only support LB and Static NAT services and service combination " + services + " is not supported.");
680-
String servicesList = "";
680+
681+
StringBuffer buff = new StringBuffer();
681682
for (Service service : services) {
682-
servicesList += service.getName() + " ";
683+
buff.append(service.getName());
684+
buff.append(" ");
683685
}
684-
s_logger.warn("NetScaler network element can only support LB and Static NAT services and service combination " + servicesList + " is not supported.");
686+
s_logger.warn("NetScaler network element can only support LB and Static NAT services and service combination " + buff.toString() + " is not supported.");
685687
s_logger.warn("NetScaler network element can only support LB and Static NAT services and service combination " + services + " is not supported.");
686688
return false;
687689
}

plugins/network-elements/netscaler/src/com/cloud/network/resource/NetscalerResource.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1763,14 +1763,14 @@ private static String generateGslbServerName(String serverIP) {
17631763
}
17641764

17651765
private static String genGslbObjectName(Object... args) {
1766-
String objectName = "";
1766+
StringBuffer buff = new StringBuffer();
17671767
for (int i = 0; i < args.length; i++) {
1768-
objectName += args[i];
1768+
buff.append(args[i]);
17691769
if (i != args.length - 1) {
1770-
objectName += "-";
1770+
buff.append("-");
17711771
}
17721772
}
1773-
return objectName;
1773+
return buff.toString();
17741774
}
17751775
}
17761776

@@ -3767,14 +3767,14 @@ private String generateSslCertKeyName(String fingerPrint) {
37673767
}
37683768

37693769
private String genObjectName(Object... args) {
3770-
String objectName = "";
3770+
StringBuffer buff = new StringBuffer();
37713771
for (int i = 0; i < args.length; i++) {
3772-
objectName += args[i];
3772+
buff.append(args[i]);
37733773
if (i != args.length - 1) {
3774-
objectName += _objectNamePathSep;
3774+
buff.append(_objectNamePathSep);
37753775
}
37763776
}
3777-
return objectName;
3777+
return buff.toString();
37783778
}
37793779

37803780
@Override

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ public void setHostname(String hostname) {
155155
}
156156

157157
public Integer getPort() {
158-
return port <= 0 ? 389 : port;
158+
return (Integer)(port.intValue() <= 0 ? 389 : port.intValue());
159159
}
160160

161161
public void setPort(Integer port) {

0 commit comments

Comments
 (0)