* [PATCH] log, conf, tap: Define die() as err() plus exit(), drop cppcheck workarounds
@ 2023-02-27 10:00 Stefano Brivio
2023-02-27 10:56 ` David Gibson
0 siblings, 1 reply; 2+ messages in thread
From: Stefano Brivio @ 2023-02-27 10:00 UTC (permalink / raw)
To: passt-dev; +Cc: Laine Stump, David Gibson
If we define die() as a variadic macro, passing __VA_ARGS__ to err(),
and calling exit() outside err() itself, we can drop the workarounds
introduced in commit 36f0199f6ef4 ("conf, tap: Silence two false
positive invalidFunctionArg from cppcheck").
Suggested-by: David Gibson <david@gibson.dropbear.id.au>
Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
---
conf.c | 3 ---
log.c | 14 +++++---------
log.h | 7 ++++++-
tap.c | 6 +-----
4 files changed, 12 insertions(+), 18 deletions(-)
diff --git a/conf.c b/conf.c
index 37f25d6..0e512f4 100644
--- a/conf.c
+++ b/conf.c
@@ -1064,9 +1064,6 @@ static void conf_ugid(char *runas, uid_t *uid, gid_t *gid)
if ((fd = open("/proc/self/uid_map", O_RDONLY | O_CLOEXEC)) < 0) {
die("Can't determine if we're in init namespace: %s",
strerror(errno));
-
- /* Silence cppcheck's invalidFunctionArg for 'fd' in read() */
- return;
}
if (read(fd, buf, BUFSIZ) != sizeof(root_uid_map) ||
diff --git a/log.c b/log.c
index bfde6ce..d2f08fa 100644
--- a/log.c
+++ b/log.c
@@ -46,7 +46,7 @@ int log_trace; /* --trace mode enabled */
#define BEFORE_DAEMON (setlogmask(0) == LOG_MASK(LOG_EMERG))
-#define logfn(name, level, doexit) \
+#define logfn(name, level) \
void name(const char *format, ...) { \
struct timespec tp; \
va_list args; \
@@ -75,9 +75,6 @@ void name(const char *format, ...) { \
if (format[strlen(format)] != '\n') \
fprintf(stderr, "\n"); \
} \
- \
- if (doexit) \
- exit(EXIT_FAILURE); \
}
/* Prefixes for log file messages, indexed by priority */
@@ -90,11 +87,10 @@ const char *logfile_prefix[] = {
" ", /* LOG_DEBUG */
};
-logfn(die, LOG_ERR, 1)
-logfn(err, LOG_ERR, 0)
-logfn(warn, LOG_WARNING, 0)
-logfn(info, LOG_INFO, 0)
-logfn(debug,LOG_DEBUG, 0)
+logfn(err, LOG_ERR)
+logfn(warn, LOG_WARNING)
+logfn(info, LOG_INFO)
+logfn(debug,LOG_DEBUG)
/**
* trace_init() - Set log_trace depending on trace (debug) mode
diff --git a/log.h b/log.h
index d4e9d85..d4ea141 100644
--- a/log.h
+++ b/log.h
@@ -10,12 +10,17 @@
#define LOGFILE_CUT_RATIO 30 /* When full, cut ~30% size */
#define LOGFILE_SIZE_MIN (5UL * MAX(BUFSIZ, PAGE_SIZE))
-void die(const char *format, ...);
void err(const char *format, ...);
void warn(const char *format, ...);
void info(const char *format, ...);
void debug(const char *format, ...);
+#define die(...) \
+ do { \
+ err(__VA_ARGS__); \
+ exit(EXIT_FAILURE); \
+ } while (0)
+
extern int log_trace;
void trace_init(int enable);
#define trace(...) \
diff --git a/tap.c b/tap.c
index d6f962e..88eed88 100644
--- a/tap.c
+++ b/tap.c
@@ -1037,13 +1037,9 @@ static void tap_sock_unix_init(struct ctx *c)
snprintf(path, UNIX_PATH_MAX - 1, UNIX_SOCK_PATH, i);
ex = socket(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK, 0);
- if (ex < 0) {
+ if (ex < 0)
die("UNIX domain socket check: %s", strerror(errno));
- /* Silence cppcheck's invalidFunctionArg for 'ex' */
- return;
- }
-
ret = connect(ex, (const struct sockaddr *)&addr, sizeof(addr));
if (!ret || (errno != ENOENT && errno != ECONNREFUSED &&
errno != EACCES)) {
--
@@ -1037,13 +1037,9 @@ static void tap_sock_unix_init(struct ctx *c)
snprintf(path, UNIX_PATH_MAX - 1, UNIX_SOCK_PATH, i);
ex = socket(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK, 0);
- if (ex < 0) {
+ if (ex < 0)
die("UNIX domain socket check: %s", strerror(errno));
- /* Silence cppcheck's invalidFunctionArg for 'ex' */
- return;
- }
-
ret = connect(ex, (const struct sockaddr *)&addr, sizeof(addr));
if (!ret || (errno != ENOENT && errno != ECONNREFUSED &&
errno != EACCES)) {
--
2.39.1
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH] log, conf, tap: Define die() as err() plus exit(), drop cppcheck workarounds
2023-02-27 10:00 [PATCH] log, conf, tap: Define die() as err() plus exit(), drop cppcheck workarounds Stefano Brivio
@ 2023-02-27 10:56 ` David Gibson
0 siblings, 0 replies; 2+ messages in thread
From: David Gibson @ 2023-02-27 10:56 UTC (permalink / raw)
To: Stefano Brivio; +Cc: passt-dev, Laine Stump
[-- Attachment #1: Type: text/plain, Size: 3866 bytes --]
On Mon, Feb 27, 2023 at 11:00:54AM +0100, Stefano Brivio wrote:
> If we define die() as a variadic macro, passing __VA_ARGS__ to err(),
> and calling exit() outside err() itself, we can drop the workarounds
> introduced in commit 36f0199f6ef4 ("conf, tap: Silence two false
> positive invalidFunctionArg from cppcheck").
>
> Suggested-by: David Gibson <david@gibson.dropbear.id.au>
> Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
Reviewed-by: David Gibson <david@gibson.dropbear.id.au>
> ---
> conf.c | 3 ---
> log.c | 14 +++++---------
> log.h | 7 ++++++-
> tap.c | 6 +-----
> 4 files changed, 12 insertions(+), 18 deletions(-)
>
> diff --git a/conf.c b/conf.c
> index 37f25d6..0e512f4 100644
> --- a/conf.c
> +++ b/conf.c
> @@ -1064,9 +1064,6 @@ static void conf_ugid(char *runas, uid_t *uid, gid_t *gid)
> if ((fd = open("/proc/self/uid_map", O_RDONLY | O_CLOEXEC)) < 0) {
> die("Can't determine if we're in init namespace: %s",
> strerror(errno));
> -
> - /* Silence cppcheck's invalidFunctionArg for 'fd' in read() */
> - return;
> }
>
> if (read(fd, buf, BUFSIZ) != sizeof(root_uid_map) ||
> diff --git a/log.c b/log.c
> index bfde6ce..d2f08fa 100644
> --- a/log.c
> +++ b/log.c
> @@ -46,7 +46,7 @@ int log_trace; /* --trace mode enabled */
>
> #define BEFORE_DAEMON (setlogmask(0) == LOG_MASK(LOG_EMERG))
>
> -#define logfn(name, level, doexit) \
> +#define logfn(name, level) \
> void name(const char *format, ...) { \
> struct timespec tp; \
> va_list args; \
> @@ -75,9 +75,6 @@ void name(const char *format, ...) { \
> if (format[strlen(format)] != '\n') \
> fprintf(stderr, "\n"); \
> } \
> - \
> - if (doexit) \
> - exit(EXIT_FAILURE); \
> }
>
> /* Prefixes for log file messages, indexed by priority */
> @@ -90,11 +87,10 @@ const char *logfile_prefix[] = {
> " ", /* LOG_DEBUG */
> };
>
> -logfn(die, LOG_ERR, 1)
> -logfn(err, LOG_ERR, 0)
> -logfn(warn, LOG_WARNING, 0)
> -logfn(info, LOG_INFO, 0)
> -logfn(debug,LOG_DEBUG, 0)
> +logfn(err, LOG_ERR)
> +logfn(warn, LOG_WARNING)
> +logfn(info, LOG_INFO)
> +logfn(debug,LOG_DEBUG)
>
> /**
> * trace_init() - Set log_trace depending on trace (debug) mode
> diff --git a/log.h b/log.h
> index d4e9d85..d4ea141 100644
> --- a/log.h
> +++ b/log.h
> @@ -10,12 +10,17 @@
> #define LOGFILE_CUT_RATIO 30 /* When full, cut ~30% size */
> #define LOGFILE_SIZE_MIN (5UL * MAX(BUFSIZ, PAGE_SIZE))
>
> -void die(const char *format, ...);
> void err(const char *format, ...);
> void warn(const char *format, ...);
> void info(const char *format, ...);
> void debug(const char *format, ...);
>
> +#define die(...) \
> + do { \
> + err(__VA_ARGS__); \
> + exit(EXIT_FAILURE); \
> + } while (0)
> +
> extern int log_trace;
> void trace_init(int enable);
> #define trace(...) \
> diff --git a/tap.c b/tap.c
> index d6f962e..88eed88 100644
> --- a/tap.c
> +++ b/tap.c
> @@ -1037,13 +1037,9 @@ static void tap_sock_unix_init(struct ctx *c)
> snprintf(path, UNIX_PATH_MAX - 1, UNIX_SOCK_PATH, i);
>
> ex = socket(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK, 0);
> - if (ex < 0) {
> + if (ex < 0)
> die("UNIX domain socket check: %s", strerror(errno));
>
> - /* Silence cppcheck's invalidFunctionArg for 'ex' */
> - return;
> - }
> -
> ret = connect(ex, (const struct sockaddr *)&addr, sizeof(addr));
> if (!ret || (errno != ENOENT && errno != ECONNREFUSED &&
> errno != EACCES)) {
--
David Gibson | I'll have my music baroque, and my code
david AT gibson.dropbear.id.au | minimalist, thank you. NOT _the_ _other_
| _way_ _around_!
http://www.ozlabs.org/~dgibson
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2023-02-27 13:30 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-02-27 10:00 [PATCH] log, conf, tap: Define die() as err() plus exit(), drop cppcheck workarounds Stefano Brivio
2023-02-27 10:56 ` David Gibson
Code repositories for project(s) associated with this public inbox
https://passt.top/passt
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for IMAP folder(s).