1
0
mirror of https://github.com/OpenNebula/one.git synced 2025-01-06 13:17:42 +03:00

M #-: Add CppCheck to smoke tests (#2464)

(cherry picked from commit a5c57b69b3)
This commit is contained in:
Pavel Czerný 2023-01-31 13:46:09 +01:00 committed by Ruben S. Montero
parent c11e111030
commit 4157b6b6cf
No known key found for this signature in database
GPG Key ID: A0CEA6FA880A1D87
43 changed files with 225 additions and 175 deletions

View File

@ -14,6 +14,8 @@ jobs:
ruby-version: 2.6
- name: Install rubocop
run: gem install rubocop
- name: Install CppCheck
run: sudo apt install -y cppcheck
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- uses: actions/checkout@v2

View File

@ -131,15 +131,19 @@ class SingleAttribute : public Attribute
{
public:
SingleAttribute(const std::string& name):Attribute(name){};
SingleAttribute(const std::string& name)
: Attribute(name)
{}
SingleAttribute(const std::string& name, const std::string& value):
Attribute(name),attribute_value(value){};
SingleAttribute(const std::string& name, const std::string& value)
: Attribute(name)
, attribute_value(value)
{}
SingleAttribute(const SingleAttribute& sa):Attribute(sa.attribute_name)
{
attribute_value = sa.attribute_value;
};
SingleAttribute(const SingleAttribute& sa)
: Attribute(sa.attribute_name)
, attribute_value(sa.attribute_value)
{}
~SingleAttribute(){};
@ -253,21 +257,25 @@ class VectorAttribute : public Attribute
{
public:
VectorAttribute(const std::string& name):Attribute(name){};
VectorAttribute(const std::string& name)
: Attribute(name)
{}
VectorAttribute(const std::string& name,
const std::map<std::string,std::string>& value):
Attribute(name),attribute_value(value){};
const std::map<std::string,std::string>& value)
: Attribute(name)
, attribute_value(value)
{}
VectorAttribute(const VectorAttribute& va):Attribute(va.attribute_name)
{
attribute_value = va.attribute_value;
};
VectorAttribute(const VectorAttribute& va)
: Attribute(va.attribute_name)
, attribute_value(va.attribute_value)
{}
VectorAttribute(const VectorAttribute* va):Attribute(va->attribute_name)
{
attribute_value = va->attribute_value;
};
VectorAttribute(const VectorAttribute* va)
: Attribute(va->attribute_name)
, attribute_value(va->attribute_value)
{}
~VectorAttribute(){};

View File

@ -76,7 +76,7 @@ public:
return "";
};
static Operation str_to_operation(std::string str)
static Operation str_to_operation(const std::string& str)
{
if (str == "USE") return USE;
else if (str == "MANAGE") return MANAGE;

View File

@ -101,7 +101,7 @@ public:
* @param result to store the xmlrpc call result
* @param ... xmlrpc arguments
*/
void call(const std::string &method, const std::string format,
void call(const std::string &method, const std::string &format,
xmlrpc_c::value * const result, ...);
private:

View File

@ -32,7 +32,7 @@ public:
/**
* Function to build a XML message for an API hook
*/
static std::string format_message(std::string method, ParamList& paramList,
static std::string format_message(const std::string& method, ParamList& paramList,
const RequestAttributes& att);
/**

View File

@ -37,7 +37,7 @@ class IPAMManager :
{
public:
IPAMManager(time_t timer, const std::string mad_location)
IPAMManager(time_t timer, const std::string& mad_location)
: DriverManager(mad_location)
, Listener("IPAM Manager")
, timer_thread(timer, [this](){timer_action();})

View File

@ -127,7 +127,7 @@ private:
class Listener
{
public:
Listener(std::string _name)
Listener(const std::string& _name)
: name(_name)
{
}

View File

@ -146,14 +146,14 @@ public:
* @param raft attributes in XML format
* @return 0 on success
*/
int update_raft_state(std::string name, std::string& raft_xml);
int update_raft_state(const std::string& name, std::string& raft_xml);
/**
* Returns the raft state attributes as stored in the log
* @param raft_xml attributes in xml
* @return 0 on success
*/
int get_raft_state(std::string name, std::string &raft_xml);
int get_raft_state(const std::string& name, std::string &raft_xml);
/**
* Purge log records. Delete old records applied to database upto the

View File

@ -218,7 +218,7 @@ int Message<E, compress, encode, encrypt, has_timestamp>
is >> buffer;
_type = _type_str._from_str(buffer.c_str());
_type = _type_str._from_str(buffer);
if ( !is.good() || _type == E::UNDEFINED )
{

View File

@ -63,72 +63,33 @@ public:
bool success; /**< True if the call was successfull false otherwise */
RequestAttributes(AuthRequest::Operation api_auth_op)
{
resp_obj = PoolObjectSQL::NONE;
resp_id = -1;
resp_msg = "";
replication_idx = UINT64_MAX;
auth_op = api_auth_op;
};
: resp_obj(PoolObjectSQL::NONE)
, resp_id(-1)
, replication_idx(UINT64_MAX)
, auth_op(api_auth_op)
{}
RequestAttributes(const RequestAttributes& ra)
{
uid = ra.uid;
gid = ra.gid;
uname = ra.uname;
gname = ra.gname;
password = ra.password;
group_ids = ra.group_ids;
session = ra.session;
req_id = ra.req_id;
umask = ra.umask;
retval = ra.retval;
retval_xml = ra.retval_xml;
resp_obj = ra.resp_obj;
resp_id = ra.resp_id;
resp_msg = ra.resp_msg;
replication_idx = ra.replication_idx;
auth_op = ra.auth_op;
};
RequestAttributes(const RequestAttributes& ra) = default;
RequestAttributes(int _uid, int _gid, const RequestAttributes& ra)
{
uid = _uid;
gid = _gid;
password = "";
group_ids = ra.group_ids;
uname = "";
gname = "";
umask = 0;
session = ra.session;
req_id = ra.req_id;
umask = ra.umask;
retval = ra.retval;
retval_xml = ra.retval_xml;
resp_obj = PoolObjectSQL::NONE;
resp_id = -1;
resp_msg = "";
replication_idx = UINT64_MAX;
auth_op = ra.auth_op;
};
: uid(_uid)
, gid(_gid)
, uname()
, gname()
, password()
, group_ids(ra.group_ids)
, session(ra.session)
, req_id(ra.req_id)
, umask(ra.umask)
, retval(ra.retval)
, retval_xml(ra.retval_xml)
, extra_xml()
, resp_obj(PoolObjectSQL::NONE)
, resp_id(-1)
, resp_msg()
, replication_idx(UINT64_MAX)
, auth_op(ra.auth_op)
{}
bool is_admin() const
{
@ -316,22 +277,16 @@ protected:
Request(const std::string& mn,
const std::string& signature,
const std::string& help)
: pool(nullptr)
, method_name(mn)
, vm_action(VMActions::NONE_ACTION)
, log_method_call(true)
, leader_only(true)
, zone_disabled(false)
{
pool = nullptr;
method_name = mn;
_signature = signature;
_help = help;
hidden_params.clear();
log_method_call = true;
leader_only = true;
zone_disabled = false;
vm_action = VMActions::NONE_ACTION;
};
_help = help;
}
virtual ~Request() = default;

View File

@ -73,7 +73,7 @@ private:
std::set<std::string> registered_methods;
xmlrpc_c::registry registry;
void addMethod(std::string const name, xmlrpc_c::methodPtr const methodP)
void addMethod(const std::string& name, const xmlrpc_c::methodPtr& methodP)
{
registered_methods.insert(name);
registry.addMethod(name, methodP);

View File

@ -25,7 +25,7 @@
class RequestManagerProxy: public Request
{
public:
RequestManagerProxy(std::string _method)
RequestManagerProxy(const std::string& _method)
: Request("RequestManagerProxy", "?",
"Forwards the request to another OpenNebula")
, method(_method)

View File

@ -77,7 +77,7 @@ public:
*
* @return ErroCode for the request.
*/
ErrorCode request_execute(int id, std::string name, bool on_hold,
ErrorCode request_execute(int id, const std::string& name, bool on_hold,
const std::string& s_uattr, Template* extra_attrs, int& vid,
RequestAttributes& att);

View File

@ -65,7 +65,7 @@ public:
*
* @return ErroCode for the request.
*/
ErrorCode request_execute(int id, std::string name,
ErrorCode request_execute(int id, const std::string& name,
const std::string& s_uattr, Template* extra_attrs, int& vid,
RequestAttributes& att);

View File

@ -86,7 +86,15 @@ public:
{
cur_sz += STRING_BUFFER_SIZE;
buffer = (char *) realloc((void *) buffer, cur_sz * sizeof(char));
auto new_buffer = (char *) realloc((void *) buffer, cur_sz * sizeof(char));
if (!new_buffer)
{
// Out of memory
return -1;
}
buffer = new_buffer;
cur_ptr = buffer + line_sz;
continue;

View File

@ -48,11 +48,10 @@ public:
xml_root(_xml_root){}
Template(const Template& t)
: replace_mode(t.replace_mode)
, separator(t.separator)
, xml_root(t.xml_root)
{
replace_mode = t.replace_mode;
separator = t.separator;
xml_root = t.xml_root;
for (auto it = t.attributes.begin() ; it != t.attributes.end() ; it++)
{
attributes.insert(make_pair(it->first,(it->second)->clone()));

View File

@ -149,7 +149,7 @@ public:
* @param password
* @return 0 on success
**/
static int split_secret(const std::string secret,
static int split_secret(const std::string& secret,
std::string& user,
std::string& pass);

View File

@ -345,7 +345,7 @@ private:
clear();
}
VMGroupRole * get(T k)
VMGroupRole * get(const T& k)
{
auto it = roles.find(k);

View File

@ -67,13 +67,12 @@ public:
}
};
VMGroupRule(VMGroupPolicy p, role_bitset _roles):policy(p), roles(_roles){};
VMGroupRule(VMGroupPolicy p, const role_bitset& _roles)
: policy(p)
, roles(_roles)
{}
VMGroupRule(const VMGroupRule& other)
{
policy = other.policy;
roles = other.roles;
}
VMGroupRule(const VMGroupRule& other) = default;
/* ---------------------------------------------------------------------- */
/* Rule operators */

View File

@ -141,10 +141,10 @@ private:
/**
* Insert acl rules into rules attribute.
*
* @param default_vdc_acl the mask for the acls.
* @param name_attr the name of the configuration attribute.
* @param type Object type for the acls.
*/
void insert_default_rules(std::string default_vdc_acl,
void insert_default_rules(const std::string& name_attr,
PoolObjectSQL::ObjectType type);
};

View File

@ -0,0 +1,54 @@
<suppressions>
<!--
<suppress>
<id>variableScope</id>
<fileName>*</fileName>
</suppress>
<suppress>
<id>unusedVariable</id>
<fileName>*</fileName>
</suppress>
<suppress>
<id>noConstructor</id>
<fileName>*</fileName>
</suppress>
<suppress>
<id>noExplicitConstructor</id>
<fileName>*</fileName>
</suppress>
<suppress>
<id>constVariable</id>
<fileName>*</fileName>
</suppress>
<suppress>
<id>missingOverride</id>
<fileName>*</fileName>
</suppress>
<suppress>
<id>shadowVariable</id>
<fileName>*</fileName>
</suppress>
<suppress>
<id>unreadVariable</id>
<fileName>*</fileName>
</suppress>
-->
<suppress>
<id>missingInclude</id>
<fileName>*</fileName>
</suppress>
<suppress>
<id>danglingLifetime</id>
<fileName>*</fileName>
</suppress>
<suppress>
<id>containerOutOfBounds</id>
<fileName>src/hm/HookLog.cc</fileName>
<symbolName>query_output</symbolName>
</suppress>
<suppress>
<id>ctuuninitvar</id>
<fileName>src/sql/LogDB.cc</fileName>
</suppress>
</suppressions>

View File

@ -0,0 +1,24 @@
#!/bin/sh
# This script uses static analysis tool CppCheck to find issues in C++ code
# As first step it's doing just basic checks, we should solve them and
# consider adding more with flag --enable:
# - performance - should be added first, shows minor performance issues
# - information
# - warning - adds lot of issues: uninitialized variable, redundant condition,
# index out of range, ...
# - style - adds lot of low priority issues
# - all - all check, too many errors
# To disable specific error messages use cppcheck-suppressions.xml file
SOURCES="src"
INCLUDES="-I include"
DEFINES="-DSQLITE_DB -DMYSQL_DB -DPOSTGRESQL_DB -DSYSTEMD"
ENABLE="--enable=performance,information"
IGNORE="-i .xmlrpc_test/ -i src/sunstone/ -i src/svncterm_server/ -i src/fireedge"
SUPRESS="--suppress-xml=share/smoke_tests/config/cppcheck-suppressions.xml"
OTHERS="--std=c++14 --error-exitcode=2 -q"
cppcheck $SOURCES $INCLUDES $DEFINES $IGNORE $ENABLE $SUPRESS $OTHERS

View File

@ -135,7 +135,7 @@ int Client::read_oneauth(string &secret, string& error_msg)
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
void Client::call(const std::string &method, const std::string format,
void Client::call(const std::string &method, const std::string &format,
xmlrpc_c::value * const result, ...)
{
va_list args;

View File

@ -85,8 +85,10 @@ ExtendedAttribute * ExtendedAttributeSet::delete_attribute(int id)
return nullptr;
}
auto ptr = it->second;
a_set.erase(it);
return it->second;
return ptr;
}

View File

@ -200,6 +200,8 @@ string * one_util::aes256cbc_encrypt(const string& in, const string& password)
}
else
{
delete aes256;
return nullptr;
}
}

View File

@ -276,14 +276,14 @@ int Hook::insert_replace(SqlDB *db, bool replace, std::string& error_str)
set_group(GroupPool::ONEADMIN_ID, GroupPool::ONEADMIN_NAME);
// Update the Hook
sql_name = db->escape_str(name.c_str());
sql_name = db->escape_str(name);
if ( sql_name == 0 )
{
goto error_name;
}
sql_xml = db->escape_str(to_xml(xml_body).c_str());
sql_xml = db->escape_str(to_xml(xml_body));
if ( sql_xml == 0 )
{

View File

@ -71,7 +71,7 @@ const string HookAPI::unsupported_calls[] = {"one.vm.info",
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
std::string HookAPI::format_message(std::string method, ParamList& paramList,
std::string HookAPI::format_message(const std::string& method, ParamList& paramList,
const RequestAttributes& att)
{
ostringstream oss;

View File

@ -179,7 +179,7 @@ int HookLog::add(int hkid, int hkrc, std::string &xml_result)
<< xml_result
<< "</HOOK_EXECUTION_RECORD>";
sql_xml = db->escape_str(oss.str().c_str());
sql_xml = db->escape_str(oss.str());
if ( sql_xml == 0 )
{

View File

@ -446,15 +446,14 @@ HostSharePCI::PCIDevice::PCIDevice(VectorAttribute * _attrs)
/* ------------------------------------------------------------------------*/
HostSharePCI::PCIDevice::PCIDevice(const PCIDevice& src)
: vendor_id(src.vendor_id)
, device_id(src.device_id)
, class_id(src.class_id)
, vmid(src.vmid)
, address(src.address)
, attrs(src.attrs->clone())
{
vendor_id = src.vendor_id;
device_id = src.device_id;
class_id = src.class_id;
vmid = src.vmid;
address = src.address;
attrs = src.attrs->clone();
};
}
/* ------------------------------------------------------------------------*/
/* ------------------------------------------------------------------------*/

View File

@ -1272,7 +1272,7 @@ void Nebula::get_ds_location(string& dsloc) const
string Nebula::get_vm_log_filename(int oid) const
{
ostringstream oss;
bool use_vms_location;
bool use_vms_location = false;
const VectorAttribute * log = nebula_configuration->get("LOG");

View File

@ -120,7 +120,7 @@ void VMTemplateInstantiate::request_execute(xmlrpc_c::paramList const& paramList
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
Request::ErrorCode VMTemplateInstantiate::request_execute(int id, string name,
Request::ErrorCode VMTemplateInstantiate::request_execute(int id, const string& name,
bool on_hold, const string &str_uattrs, Template* extra_attrs, int& vid,
RequestAttributes& att)
{

View File

@ -67,7 +67,7 @@ void VNTemplateInstantiate::request_execute(xmlrpc_c::paramList const& paramList
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
Request::ErrorCode VNTemplateInstantiate::request_execute(int id, string name,
Request::ErrorCode VNTemplateInstantiate::request_execute(int id, const string& name,
const string &str_uattrs, Template* extra_attrs, int& vid,
RequestAttributes& att)
{

View File

@ -294,7 +294,7 @@ static void schecule_affined_set(const std::set<int>& vms,
/* 2. Allocate VMs in the same host as the leader */
/* 3. Aggregate requirements in the leader for scheduling */
/* ------------------------------------------------------------------ */
VirtualMachineXML * vm;
VirtualMachineXML * vm = nullptr;
for ( it = vms.begin(); it != vms.end() ; ++it )
{

View File

@ -552,7 +552,7 @@ ostream& operator<<(ostream& os, VirtualMachineXML& vm)
os << "\tPRI\tID - HOSTS"<< endl
<< "\t------------------------" << endl;
for (i = resources.rbegin(); i != resources.rend() ; i++)
for (i = resources.rbegin(); i != resources.rend(); ++i)
{
os << "\t" << (*i)->priority << "\t" << (*i)->oid << endl;
}
@ -564,7 +564,7 @@ ostream& operator<<(ostream& os, VirtualMachineXML& vm)
const vector<Resource *> ds_resources = vm.match_datastores.get_resources();
for (i = ds_resources.rbegin(); i != ds_resources.rend() ; i++)
for (i = ds_resources.rbegin(); i != ds_resources.rend(); ++i)
{
os << "\t" << (*i)->priority << "\t" << (*i)->oid << endl;
}
@ -582,7 +582,7 @@ ostream& operator<<(ostream& os, VirtualMachineXML& vm)
const vector<Resource *> net_resources = vm.nics[nic_id]->get_match_networks();
for (i = net_resources.rbegin(); i != net_resources.rend() ; i++)
for (i = net_resources.rbegin(); i != net_resources.rend(); ++i)
{
os << "\t" << (*i)->priority << "\t" << (*i)->oid << endl;
}

View File

@ -1358,7 +1358,7 @@ void Scheduler::dispatch()
//----------------------------------------------------------------------
// Get the highest ranked host and best System DS for it
//----------------------------------------------------------------------
for (i = resources.rbegin() ; i != resources.rend() ; i++)
for (i = resources.rbegin() ; i != resources.rend(); ++i)
{
hid = (*i)->oid;
host = hpool->get(hid);
@ -1437,7 +1437,7 @@ void Scheduler::dispatch()
j = ds_resources.rbegin();
}
for ( ; j != ds_resources.rend() ; j++)
for ( ; j != ds_resources.rend(); ++j)
{
ds = dspool->get((*j)->oid);
@ -1517,7 +1517,7 @@ void Scheduler::dispatch()
netid = -1;
for (n = net_resources.rbegin() ; n != net_resources.rend(); n++)
for (n = net_resources.rbegin(); n != net_resources.rend(); ++n)
{
if ( diff_vnets && matched_networks.find((*n)->oid) != matched_networks.end() )
{

View File

@ -235,7 +235,7 @@ void LogDB::get_last_record_index(uint64_t& _i, unsigned int& _t)
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int LogDB::get_raft_state(std::string name, std::string &raft_xml)
int LogDB::get_raft_state(const std::string& name, std::string &raft_xml)
{
ostringstream oss;
@ -260,7 +260,7 @@ int LogDB::get_raft_state(std::string name, std::string &raft_xml)
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int LogDB::update_raft_state(std::string name, std::string& raft_xml)
int LogDB::update_raft_state(const std::string& name, std::string& raft_xml)
{
std::ostringstream oss;

View File

@ -33,30 +33,26 @@ using namespace std;
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
PostgreSqlDB::PostgreSqlDB(
const string& _server,
int _port,
const string& _user,
const string& _password,
const string& _database,
int _connections)
PostgreSqlDB::PostgreSqlDB(const string& _server,
int _port,
const string& _user,
const string& _password,
const string& _database,
int _connections)
: server(_server)
, port(_port)
, user(_user)
, password(_password)
, database(_database)
, max_connections(_connections)
{
PGconn* conn;
server = _server;
port = _port;
user = _user;
password = _password;
database = _database;
if ( port == 0 )
{
port = PG_DEFAULT_PORT;
}
max_connections = _connections;
// Set up connection parameters
string params = "host=" + _server
+ " port=" + to_string(port)

View File

@ -55,7 +55,7 @@ int NebulaTemplate::load_configuration()
if ( j == attributes.end() )
{
attributes.insert(make_pair(aname,attr));
iter++;
++iter;
}
else
{

View File

@ -207,7 +207,7 @@ void OpenNebulaTemplate::register_multiple_conf_default(
std::vector<const VectorAttribute*>::const_iterator j;
std::vector<const VectorAttribute*> attrs;
get(conf_section.c_str(), attrs);
get(conf_section, attrs);
for (i = conf_default.begin(); i != conf_default.end(); )
{
@ -224,7 +224,7 @@ void OpenNebulaTemplate::register_multiple_conf_default(
d_name = d_attr->vector_value("NAME");
for (j = attrs.begin(); j != attrs.end(); j++)
for (j = attrs.begin(); j != attrs.end(); ++j)
{
if ( (*j)->vector_value("NAME") == d_name )
{
@ -237,7 +237,7 @@ void OpenNebulaTemplate::register_multiple_conf_default(
{
// insert into attributes
attributes.insert(make_pair(conf_section, d_attr));
i++;
++i;
}
else
{
@ -249,7 +249,7 @@ void OpenNebulaTemplate::register_multiple_conf_default(
}
else
{
i++;
++i;
}
}
}

View File

@ -94,7 +94,7 @@ int Quota::set(vector<VectorAttribute*> * new_quotas, string& error)
VectorAttribute * tq;
string id;
for ( it = new_quotas->begin(); it != new_quotas->end(); it++)
for (it = new_quotas->begin(); it != new_quotas->end(); ++it)
{
id = (*it)->vector_value("ID");

View File

@ -312,7 +312,7 @@ int User::from_xml(const string& xml)
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int User::split_secret(const string secret, string& user, string& pass)
int User::split_secret(const string& secret, string& user, string& pass)
{
size_t pos;
int rc = -1;

View File

@ -390,7 +390,7 @@ int Vdc::del_group(int group_id, string& error_msg)
/* ------------------------------------------------------------------------ */
/* ------------------------------------------------------------------------ */
void ResourceSet::insert_default_rules(string name_attr, PoolObjectSQL::ObjectType type)
void ResourceSet::insert_default_rules(const string& name_attr, PoolObjectSQL::ObjectType type)
{
string default_vdc_acl;
vector<string> vdc_acl;

View File

@ -46,10 +46,12 @@ ObjectXML::ObjectXML(const std::string &xml_doc):paths(0),num_paths(0),xml(0),ct
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
ObjectXML::ObjectXML(const xmlNodePtr node):paths(0),num_paths(0),xml(0),ctx(0)
ObjectXML::ObjectXML(const xmlNodePtr node)
: paths(0)
, num_paths(0)
, xml(xmlNewDoc(reinterpret_cast<const xmlChar *>("1.0")))
, ctx(0)
{
xml = xmlNewDoc(reinterpret_cast<const xmlChar *>("1.0"));
if (xml == 0)
{
throw("Error allocating XML Document");