Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

coverityfix #835

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions common/consumer_state_table_pops.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,20 @@ local keys = redis.call('SPOP', KEYS[1], ARGV[1])
local n = table.getn(keys)
for i = 1, n do
local key = keys[i]
local fieldvalues = {}
-- Check if there was request to delete the key, clear it in table first
local num = redis.call('SREM', KEYS[3], key)
if num == 1 then
redis.call('DEL', tablename..key)
table.insert(ret, {key, fieldvalues})
end
-- Push the new set of field/value for this key in table
local fieldvalues = redis.call('HGETALL', stateprefix..tablename..key)
table.insert(ret, {key, fieldvalues})
for i = 1, #fieldvalues, 2 do
redis.call('HSET', tablename..key, fieldvalues[i], fieldvalues[i + 1])
if table.getn(fieldvalues) > 0 then
table.insert(ret, {key, fieldvalues})
for i = 1, #fieldvalues, 2 do
redis.call('HSET', tablename..key, fieldvalues[i], fieldvalues[i + 1])
end
end
-- Clean up the key in temporary state table
redis.call('DEL', stateprefix..tablename..key)
Expand Down
58 changes: 37 additions & 21 deletions common/logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -231,34 +231,50 @@ void Logger::settingThread()
SWSS_LOG_DEBUG("%s select timeout", __PRETTY_FUNCTION__);
continue;
}

KeyOpFieldsValuesTuple koValues;
dynamic_cast<ConsumerStateTable *>(selectable)->pop(koValues);
std::string key = kfvKey(koValues), op = kfvOp(koValues);

if (op != SET_COMMAND || !m_settingChangeObservers.contains(key))
/*CID 37420 FORWARD_NULL, PeterWang, 2023/6/14*/
if (selectable==nullptr)
{
continue;
}

const auto& values = kfvFieldsValues(koValues);

for (auto& i : values)
else
{
auto& field = fvField(i);
auto& value = fvValue(i);

if ((field == DAEMON_LOGLEVEL) && (value != m_currentPrios.get(key)))
KeyOpFieldsValuesTuple koValues;
/*CID 37420 FORWARD_NULL, PeterWang, 2023/7/7*/
ConsumerStateTable* consumerTable = dynamic_cast<ConsumerStateTable*>(selectable);
if (consumerTable == nullptr)
{
m_currentPrios.set(key, value);
m_settingChangeObservers.get(key).first(key, value);
continue;
}
else if ((field == DAEMON_LOGOUTPUT) && (value != m_currentOutputs.get(key)))
else
{
m_currentOutputs.set(key, value);
m_settingChangeObservers.get(key).second(key, value);
}
}
consumerTable->pop(koValues);
std::string key = kfvKey(koValues), op = kfvOp(koValues);

if (op != SET_COMMAND || !m_settingChangeObservers.contains(key))
{
continue;
}

const auto& values = kfvFieldsValues(koValues);

for (auto& i : values)
{
auto& field = fvField(i);
auto& value = fvValue(i);

if ((field == DAEMON_LOGLEVEL) && (value != m_currentPrios.get(key)))
{
m_currentPrios.set(key, value);
m_settingChangeObservers.get(key).first(key, value);
}
else if ((field == DAEMON_LOGOUTPUT) && (value != m_currentOutputs.get(key)))
{
m_currentOutputs.set(key, value);
m_settingChangeObservers.get(key).second(key, value);
}
}
}
}
}
}

Expand Down
137 changes: 77 additions & 60 deletions common/loglevel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,92 +106,109 @@ int main(int argc, char **argv)
}
}

DBConnector db("LOGLEVEL_DB", 0);
auto keys = db.keys("*");
for (auto& key : keys)
/*CID 38520 UNCAUGHT_EXCEPT, PeterWang, 2023/6/14*/
try
{
size_t colonPos = key.find(':');
if (colonPos == std::string::npos)
DBConnector db("LOGLEVEL_DB", 0);
auto keys = db.keys("*");
for (auto& key : keys)
{
continue;
size_t colonPos = key.find(':');
if (colonPos == std::string::npos)
{
continue;
}
key = key.substr(0, colonPos);
}
// Ignore autogenerated keysets
keys.erase(std::remove_if(keys.begin(), keys.end(), filterOutKeysets), keys.end());

key = key.substr(0, colonPos);
}
// Ignore autogenerated keysets
keys.erase(std::remove_if(keys.begin(), keys.end(), filterOutKeysets), keys.end());

if (print)
{
int errorCount = 0;

if (argc != 2)
if (print)
{
exitWithUsage(EXIT_FAILURE, "-p option does not accept other options");
}
int errorCount = 0;

std::sort(keys.begin(), keys.end());
for (const auto& key : keys)
{
const auto redis_key = std::string(key).append(":").append(key);
auto level = db.hget(redis_key, DAEMON_LOGLEVEL);
if (nullptr == level)
if (argc != 2)
{
std::cerr << std::left << std::setw(30) << key << "Unknown log level" << std::endl;
errorCount ++;
exitWithUsage(EXIT_FAILURE, "-p option does not accept other options");
}
else

std::sort(keys.begin(), keys.end());
for (const auto& key : keys)
{
std::cout << std::left << std::setw(30) << key << *level << std::endl;
const auto redis_key = std::string(key).append(":").append(key);
/*CID 323404 UNCAUGHT_EXCEPT, PeterWang, 2023/6/14*/
try
{
auto level = db.hget(redis_key, DAEMON_LOGLEVEL);
if (nullptr == level)
{
std::cerr << std::left << std::setw(30) << key << "Unknown log level" << std::endl;
errorCount ++;
}
else
{
std::cout << std::left << std::setw(30) << key << *level << std::endl;
}

}
catch (const std::exception& e)
{
std::cout << "db.hget error " << std::endl;
}
}
}

if (errorCount > 0)
return (EXIT_FAILURE);

return (EXIT_SUCCESS);
}
if (errorCount > 0)
return (EXIT_FAILURE);

if ((prefix == "SAI_API_") && !validateSaiLoglevel(loglevel))
{
exitWithUsage(EXIT_FAILURE, "Invalid SAI loglevel value");
}
else if ((prefix == "") && (Logger::priorityStringMap.find(loglevel) == Logger::priorityStringMap.end()))
{
exitWithUsage(EXIT_FAILURE, "Invalid loglevel value");
}
return (EXIT_SUCCESS);
}

if (applyToAll)
{
if (component != "")
if ((prefix == "SAI_API_") && !validateSaiLoglevel(loglevel))
{
exitWithUsage(EXIT_FAILURE, "Invalid options provided with -a");
exitWithUsage(EXIT_FAILURE, "Invalid SAI loglevel value");
}

if (prefix == "SAI_API_")
else if ((prefix == "") && (Logger::priorityStringMap.find(loglevel) == Logger::priorityStringMap.end()))
{
keys.erase(std::remove_if(keys.begin(), keys.end(), filterSaiKeys), keys.end());
exitWithUsage(EXIT_FAILURE, "Invalid loglevel value");
}
else

if (applyToAll)
{
keys.erase(std::remove_if(keys.begin(), keys.end(), filterOutSaiKeys), keys.end());
if (component != "")
{
exitWithUsage(EXIT_FAILURE, "Invalid options provided with -a");
}

if (prefix == "SAI_API_")
{
keys.erase(std::remove_if(keys.begin(), keys.end(), filterSaiKeys), keys.end());
}
else
{
keys.erase(std::remove_if(keys.begin(), keys.end(), filterOutSaiKeys), keys.end());
}

for (const auto& key : keys)
{
setLoglevel(db, key, loglevel);
}

exit(EXIT_SUCCESS);
}

for (const auto& key : keys)
component = prefix + component;
if (std::find(std::begin(keys), std::end(keys), component) == keys.end())
{
setLoglevel(db, key, loglevel);
exitWithUsage(EXIT_FAILURE, "Component not present in DB");
}

exit(EXIT_SUCCESS);
setLoglevel(db, component, loglevel);
return EXIT_SUCCESS;
}

component = prefix + component;
if (std::find(std::begin(keys), std::end(keys), component) == keys.end())
catch (const std::exception& e)
{
exitWithUsage(EXIT_FAILURE, "Component not present in DB");
std::cout << "db get key error " << std::endl;
}

setLoglevel(db, component, loglevel);

return EXIT_SUCCESS;
}
6 changes: 5 additions & 1 deletion common/netdispatcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,9 @@ void NetDispatcher::onNetlinkMessage(struct nl_msg *msg)
if (callback == nullptr)
return;

nl_msg_parse(msg, NetDispatcher::nlCallback, (callback));
/*CID 38551 CHECKED_RETURN, PeterWang, 2023/6/14*/
if(nl_msg_parse(msg, NetDispatcher::nlCallback, (callback))<0)
{
throw "Unknown message type(RTM_NEWLINK)";
}
}