From cddfc4e2115ddb576a4e19b5eab0a7d3b9c5a923 Mon Sep 17 00:00:00 2001 From: Stephen Sun <5379172+stephenxs@users.noreply.github.com> Date: Tue, 9 Jun 2020 09:01:24 +0800 Subject: [PATCH] [loglevel] Avoid accessing null ptr in swssloglevel (#349) * [loglevel] Fix issue: trying reading null pointer * [loglevel] Return EXIT_FAILURE in case of access violation occuring. --- common/loglevel.cpp | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/common/loglevel.cpp b/common/loglevel.cpp index d56bf3313..603b41d7c 100644 --- a/common/loglevel.cpp +++ b/common/loglevel.cpp @@ -125,6 +125,8 @@ int main(int argc, char **argv) if (print) { + int errorCount = 0; + if (argc != 2) { exitWithUsage(EXIT_FAILURE, "-p option does not accept other options"); @@ -135,8 +137,20 @@ int main(int argc, char **argv) { const auto redis_key = std::string(key).append(":").append(key); auto level = redisClient.hget(redis_key, DAEMON_LOGLEVEL); - std::cout << std::left << std::setw(30) << key << *level << std::endl; + 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; + } } + + if (errorCount > 0) + return (EXIT_FAILURE); + return (EXIT_SUCCESS); }