From bf6c329cd9f484f9e6b711f157a3b60fb3a8563d Mon Sep 17 00:00:00 2001 From: David Kalnischkies Date: Thu, 5 Oct 2017 15:27:58 +0200 Subject: avoid using NULL in varadic function for cmdline parsing MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit cppcheck reports: (portability) Passing NULL after the last typed argument to a variadic function leads to undefined behaviour. We don't ship on any platform which has this as undefined behaviour through – or it would be pretty well defined "bad" behaviour which always works, so even through UB is a trigger word, its hardly noteworthy as a change (and as a bonus the scanners of gcc/clang don't consider it UB). The commonly accepted method of fixing that seems to be (const char*)NULL, but it is in fact much simpler to just switch to the varadic functions C++ provides resolving the warning and reducing code. Reported-By: cppcheck Gbp-Dch: Ignore --- apt-private/private-cmndline.cc | 33 +++++++++++++-------------------- 1 file changed, 13 insertions(+), 20 deletions(-) (limited to 'apt-private') diff --git a/apt-private/private-cmndline.cc b/apt-private/private-cmndline.cc index f5f1cc04e..5ad3b65d3 100644 --- a/apt-private/private-cmndline.cc +++ b/apt-private/private-cmndline.cc @@ -22,27 +22,20 @@ #include /*}}}*/ -APT_SENTINEL static bool strcmp_match_in_list(char const * const Cmd, ...) /*{{{*/ +APT_NONNULL(1, 2) +static bool CmdMatches_fn(char const *const Cmd, char const *const Match) { - if (Cmd == nullptr) - return false; - va_list args; - bool found = false; - va_start(args, Cmd); - char const * Match = NULL; - while ((Match = va_arg(args, char const *)) != NULL) - { - if (strcmp(Cmd, Match) != 0) - continue; - found = true; - break; - } - va_end(args); - return found; + return strcmp(Cmd, Match) == 0; } - /*}}}*/ -#define addArg(w,x,y,z) Args.push_back(CommandLine::MakeArgs(w,x,y,z)) -#define CmdMatches(...) strcmp_match_in_list(Cmd, __VA_ARGS__, NULL) +template +APT_NONNULL(1, 2) +static bool CmdMatches_fn(char const *const Cmd, char const *const Match, Tail... MoreMatches) +{ + return CmdMatches_fn(Cmd, Match) || CmdMatches_fn(Cmd, MoreMatches...); +} +#define addArg(w, x, y, z) Args.emplace_back(CommandLine::MakeArgs(w, x, y, z)) +#define CmdMatches(...) (Cmd != nullptr && CmdMatches_fn(Cmd, __VA_ARGS__)) + static bool addArgumentsAPTCache(std::vector &Args, char const * const Cmd)/*{{{*/ { if (CmdMatches("depends", "rdepends", "xvcg", "dotty")) @@ -310,7 +303,7 @@ static bool addArgumentsAPTMark(std::vector &Args, char const addArg('v',"verbose","APT::MarkAuto::Verbose",0); } - if (strncmp(Cmd, "show", strlen("show")) != 0) + if (Cmd != nullptr && strncmp(Cmd, "show", strlen("show")) != 0) { addArg('s',"simulate","APT::Mark::Simulate",0); addArg('s',"just-print","APT::Mark::Simulate",0); -- cgit v1.2.3