diff options
author | David Kalnischkies <david@kalnischkies.de> | 2017-10-05 15:27:58 +0200 |
---|---|---|
committer | David Kalnischkies <david@kalnischkies.de> | 2017-10-05 17:30:25 +0200 |
commit | bf6c329cd9f484f9e6b711f157a3b60fb3a8563d (patch) | |
tree | b78abca2fa5ef0f9fc8fd5bfe315d1d3cd48375c | |
parent | 50920fca5eff021f810d5de0cc9d2fa4d6068c50 (diff) |
avoid using NULL in varadic function for cmdline parsing
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
-rw-r--r-- | apt-private/private-cmndline.cc | 33 |
1 files changed, 13 insertions, 20 deletions
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 <apti18n.h> /*}}}*/ -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 <typename... Tail> +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<CommandLine::Args> &Args, char const * const Cmd)/*{{{*/ { if (CmdMatches("depends", "rdepends", "xvcg", "dotty")) @@ -310,7 +303,7 @@ static bool addArgumentsAPTMark(std::vector<CommandLine::Args> &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); |