From 5416295cbf519eee82bfc96e4ffd503562ea1189 Mon Sep 17 00:00:00 2001 From: Yoga Ramalingam Date: Mon, 1 Dec 2014 09:55:10 -0500 Subject: [PATCH] Wrap the content of macro with do/while to make it safer as per code review comment --- src/utils_cmd_flush.c | 16 +++++++++------- src/utils_cmd_getval.c | 16 +++++++++------- src/utils_cmd_listval.c | 16 +++++++++------- src/utils_cmd_putnotif.c | 16 +++++++++------- src/utils_cmd_putval.c | 16 +++++++++------- 5 files changed, 45 insertions(+), 35 deletions(-) diff --git a/src/utils_cmd_flush.c b/src/utils_cmd_flush.c index c35aeb04..b84e8de5 100644 --- a/src/utils_cmd_flush.c +++ b/src/utils_cmd_flush.c @@ -27,13 +27,15 @@ #include "utils_parse_option.h" #define print_to_socket(fh, ...) \ - if (fprintf (fh, __VA_ARGS__) < 0) { \ - char errbuf[1024]; \ - WARNING ("handle_flush: failed to write to socket #%i: %s", \ - fileno (fh), sstrerror (errno, errbuf, sizeof (errbuf))); \ - return -1; \ - } \ - fflush(fh); + do { \ + if (fprintf (fh, __VA_ARGS__) < 0) { \ + char errbuf[1024]; \ + WARNING ("handle_flush: failed to write to socket #%i: %s", \ + fileno (fh), sstrerror (errno, errbuf, sizeof (errbuf))); \ + return -1; \ + } \ + fflush(fh); \ + } while (0); static int add_to_array (char ***array, int *array_num, char *value) { diff --git a/src/utils_cmd_getval.c b/src/utils_cmd_getval.c index 9de66df3..cbdd617c 100644 --- a/src/utils_cmd_getval.c +++ b/src/utils_cmd_getval.c @@ -27,13 +27,15 @@ #include "utils_parse_option.h" #define print_to_socket(fh, ...) \ - if (fprintf (fh, __VA_ARGS__) < 0) { \ - char errbuf[1024]; \ - WARNING ("handle_getval: failed to write to socket #%i: %s", \ - fileno (fh), sstrerror (errno, errbuf, sizeof (errbuf))); \ - return -1; \ - } \ - fflush(fh); + do { \ + if (fprintf (fh, __VA_ARGS__) < 0) { \ + char errbuf[1024]; \ + WARNING ("handle_getval: failed to write to socket #%i: %s", \ + fileno (fh), sstrerror (errno, errbuf, sizeof (errbuf))); \ + return -1; \ + } \ + fflush(fh); \ + } while (0); int handle_getval (FILE *fh, char *buffer) { diff --git a/src/utils_cmd_listval.c b/src/utils_cmd_listval.c index 864e5f44..13906c18 100644 --- a/src/utils_cmd_listval.c +++ b/src/utils_cmd_listval.c @@ -39,13 +39,15 @@ } while (0) #define print_to_socket(fh, ...) \ - if (fprintf (fh, __VA_ARGS__) < 0) { \ - char errbuf[1024]; \ - WARNING ("handle_listval: failed to write to socket #%i: %s", \ - fileno (fh), sstrerror (errno, errbuf, sizeof (errbuf))); \ - free_everything_and_return (-1); \ - } \ - fflush(fh); + do { \ + if (fprintf (fh, __VA_ARGS__) < 0) { \ + char errbuf[1024]; \ + WARNING ("handle_listval: failed to write to socket #%i: %s", \ + fileno (fh), sstrerror (errno, errbuf, sizeof (errbuf))); \ + free_everything_and_return (-1); \ + } \ + fflush(fh); \ + } while (0); int handle_listval (FILE *fh, char *buffer) { diff --git a/src/utils_cmd_putnotif.c b/src/utils_cmd_putnotif.c index d09eeb3e..f8e734de 100644 --- a/src/utils_cmd_putnotif.c +++ b/src/utils_cmd_putnotif.c @@ -26,13 +26,15 @@ #include "utils_parse_option.h" #define print_to_socket(fh, ...) \ - if (fprintf (fh, __VA_ARGS__) < 0) { \ - char errbuf[1024]; \ - WARNING ("handle_putnotif: failed to write to socket #%i: %s", \ - fileno (fh), sstrerror (errno, errbuf, sizeof (errbuf))); \ - return -1; \ - } \ - fflush(fh); + do { \ + if (fprintf (fh, __VA_ARGS__) < 0) { \ + char errbuf[1024]; \ + WARNING ("handle_putnotif: failed to write to socket #%i: %s", \ + fileno (fh), sstrerror (errno, errbuf, sizeof (errbuf))); \ + return -1; \ + } \ + fflush(fh); \ + } while (0); static int set_option_severity (notification_t *n, const char *value) { diff --git a/src/utils_cmd_putval.c b/src/utils_cmd_putval.c index d579b6e8..2522819d 100644 --- a/src/utils_cmd_putval.c +++ b/src/utils_cmd_putval.c @@ -26,13 +26,15 @@ #include "utils_parse_option.h" #define print_to_socket(fh, ...) \ - if (fprintf (fh, __VA_ARGS__) < 0) { \ - char errbuf[1024]; \ - WARNING ("handle_putval: failed to write to socket #%i: %s", \ - fileno (fh), sstrerror (errno, errbuf, sizeof (errbuf))); \ - return -1; \ - } \ - fflush(fh); + do { \ + if (fprintf (fh, __VA_ARGS__) < 0) { \ + char errbuf[1024]; \ + WARNING ("handle_putval: failed to write to socket #%i: %s", \ + fileno (fh), sstrerror (errno, errbuf, sizeof (errbuf))); \ + return -1; \ + } \ + fflush(fh); \ + } while (0); static int dispatch_values (const data_set_t *ds, value_list_t *vl, FILE *fh, char *buffer) -- 2.30.2