From 710d8254d4f9247ec04180dfd014be59520124d1 Mon Sep 17 00:00:00 2001 From: Alex Young Date: Mon, 11 Jun 2012 14:34:17 +0100 Subject: [PATCH] Make sure all ifs are braced --- src/acl.c | 6 +++--- src/bitset.h | 24 ++++++++---------------- src/client.c | 18 ++++++++---------- src/control.c | 40 ++++++++++++++++++++++++---------------- src/flexnbd.c | 25 +++++++++++++------------ src/ioutil.c | 36 ++++++++++++++++++++---------------- src/parse.c | 9 +++++---- src/readwrite.c | 22 ++++++++++++++-------- src/readwrite.h | 4 ++-- src/remote.c | 3 ++- src/serve.c | 18 ++++++++++++------ src/util.c | 3 +-- src/util.h | 15 +++++++-------- 13 files changed, 119 insertions(+), 104 deletions(-) diff --git a/src/acl.c b/src/acl.c index a8d8518..acd66c0 100644 --- a/src/acl.c +++ b/src/acl.c @@ -55,13 +55,13 @@ static int is_included_in_acl(int list_length, struct ip_and_mask (*list)[], uni for (testbits = entry->mask; testbits > 0; testbits -= 8) { debug("testbits=%d, c1=%02x, c2=%02x", testbits, raw_address1[0], raw_address2[0]); if (testbits >= 8) { - if (raw_address1[0] != raw_address2[0]) - goto no_match; + if (raw_address1[0] != raw_address2[0]) { goto no_match; } } else { if ((raw_address1[0] & testmasks[testbits%8]) != - (raw_address2[0] & testmasks[testbits%8]) ) + (raw_address2[0] & testmasks[testbits%8]) ) { goto no_match; + } } raw_address1++; diff --git a/src/bitset.h b/src/bitset.h index 522d291..23f688f 100644 --- a/src/bitset.h +++ b/src/bitset.h @@ -20,10 +20,8 @@ static inline int bit_is_clear(char* b, int idx) { } /** Tests whether the bit at ''idx'' in array ''b'' has value ''value'' */ static inline int bit_has_value(char* b, int idx, int value) { - if (value) - return bit_is_set(b, idx); - else - return bit_is_clear(b, idx); + if (value) { return bit_is_set(b, idx); } + else { return bit_is_clear(b, idx); } } /** Sets the bit ''idx'' in array ''b'' */ static inline void bit_set(char* b, int idx) { @@ -37,21 +35,15 @@ static inline void bit_clear(char* b, int idx) { } /** Sets ''len'' bits in array ''b'' starting at offset ''from'' */ static inline void bit_set_range(char* b, int from, int len) { - for (; from%8 != 0 && len > 0; len--) - bit_set(b, from++); - if (len >= 8) - memset(b+(from/8), 255, len/8); - for (; len > 0; len--) - bit_set(b, from++); + for (; from%8 != 0 && len > 0; len--) { bit_set(b, from++); } + if (len >= 8) { memset(b+(from/8), 255, len/8); } + for (; len > 0; len--) { bit_set(b, from++); } } /** Clears ''len'' bits in array ''b'' starting at offset ''from'' */ static inline void bit_clear_range(char* b, int from, int len) { - for (; from%8 != 0 && len > 0; len--) - bit_clear(b, from++); - if (len >= 8) - memset(b+(from/8), 0, len/8); - for (; len > 0; len--) - bit_clear(b, from++); + for (; from%8 != 0 && len > 0; len--) { bit_clear(b, from++); } + if (len >= 8) { memset(b+(from/8), 0, len/8); } + for (; len > 0; len--) { bit_clear(b, from++); } } /** Counts the number of contiguous bits in array ''b'', starting at ''from'' diff --git a/src/client.c b/src/client.c index dc0bb16..f905a13 100644 --- a/src/client.c +++ b/src/client.c @@ -88,11 +88,9 @@ void write_not_zeroes(struct client* client, uint64_t from, int len) for (i=0; iserve->size; i+=map->resolution) { int here = (from >= i && from < i+map->resolution); - if (here) - fprintf(stderr, ">"); + if (here) { fprintf(stderr, ">"); } fprintf(stderr, bitset_is_set_at(map, i) ? "1" : "0"); - if (here) - fprintf(stderr, "<"); + if (here) { fprintf(stderr, "<"); } } fprintf(stderr, "\n"); } @@ -239,8 +237,9 @@ int client_request_needs_reply( struct client * client, struct nbd_request reque { debug("request type %d", request.type); - if (request.magic != REQUEST_MAGIC) + if (request.magic != REQUEST_MAGIC) { fatal("Bad magic %08x", request.magic); + } switch (request.type) { @@ -376,12 +375,11 @@ void client_cleanup(struct client* client, { info("client cleanup"); - if (client->socket) - close(client->socket); - if (client->mapped) + if (client->socket) { close(client->socket); } + if (client->mapped) { munmap(client->mapped, client->serve->size); - if (client->fileno) - close(client->fileno); + } + if (client->fileno) { close(client->fileno); } } void* client_serve(void* client_uncast) diff --git a/src/control.c b/src/control.c index d104eef..9586853 100644 --- a/src/control.c +++ b/src/control.c @@ -117,17 +117,18 @@ void* mirror_runner(void* serve_params_uncast) current += run; if (serve->mirror->signal_abandon) { - if (pass == last_pass) - server_unlock_io( serve ); + if (pass == last_pass) { server_unlock_io( serve ); } close(serve->mirror->client); goto abandon_mirror; } } /* if we've not written anything */ - if (written < mirror_last_pass_after_bytes_written) + if (written < mirror_last_pass_after_bytes_written) { pass = last_pass; + } } + /* a successful finish ends here */ switch (serve->mirror->action_at_finish) @@ -204,12 +205,15 @@ int control_mirror(struct control_params* client, int linesc, char** lines) action_at_finish = ACTION_PROXY; if (linesc > 4) { - if (strcmp("proxy", lines[3]) == 0) + if (strcmp("proxy", lines[3]) == 0) { action_at_finish = ACTION_PROXY; - else if (strcmp("exit", lines[3]) == 0) + } + else if (strcmp("exit", lines[3]) == 0) { action_at_finish = ACTION_EXIT; - else if (strcmp("nothing", lines[3]) == 0) + } + else if (strcmp("nothing", lines[3]) == 0) { action_at_finish = ACTION_NOTHING; + } else { write_socket("1: action must be one of 'proxy', 'exit' or 'nothing'"); return -1; @@ -222,10 +226,12 @@ int control_mirror(struct control_params* client, int linesc, char** lines) } /** I don't like use_connect_from but socket_connect doesn't take *mysockaddr :( */ - if (use_connect_from) + if (use_connect_from) { fd = socket_connect(&connect_to.generic, &connect_from.generic); - else + } + else { fd = socket_connect(&connect_to.generic, NULL); + } remote_size = socket_nbd_read_hello(fd); @@ -303,8 +309,7 @@ int control_status( void control_cleanup(struct control_params* client, int fatal __attribute__ ((unused)) ) { - if (client->socket) - close(client->socket); + if (client->socket) { close(client->socket); } free(client); } @@ -328,24 +333,28 @@ void* control_serve(void* client_uncast) /* ignore failure */ } else if (strcmp(lines[0], "acl") == 0) { - if (control_acl(client, linesc-1, lines+1) < 0) + if (control_acl(client, linesc-1, lines+1) < 0) { finished = 1; + } } else if (strcmp(lines[0], "mirror") == 0) { - if (control_mirror(client, linesc-1, lines+1) < 0) + if (control_mirror(client, linesc-1, lines+1) < 0) { finished = 1; + } } else if (strcmp(lines[0], "status") == 0) { - if (control_status(client, linesc-1, lines+1) < 0) + if (control_status(client, linesc-1, lines+1) < 0) { finished = 1; + } } else { write(client->socket, "10: unknown command\n", 23); finished = 1; } - for (i=0; icontrol_socket_name) - return; + if (!params->control_socket_name) { return; } params->control_fd = socket(AF_UNIX, SOCK_STREAM, 0); FATAL_IF_NEGATIVE(params->control_fd , diff --git a/src/flexnbd.c b/src/flexnbd.c index 5992c0b..fb3cf34 100644 --- a/src/flexnbd.c +++ b/src/flexnbd.c @@ -81,8 +81,10 @@ void params_readwrite( s_ip_address ); - if (s_bind_address != NULL && parse_ip_to_sockaddr(&out->connect_from.generic, s_bind_address) == 0) + if (s_bind_address != NULL && + parse_ip_to_sockaddr(&out->connect_from.generic, s_bind_address) == 0) { fatal("Couldn't parse bind address '%s'", s_bind_address); + } parse_port( s_port, &out->connect_to.v4 ); @@ -252,8 +254,7 @@ int mode_serve( int argc, char *argv[] ) while (1) { c = getopt_long(argc, argv, serve_short_options, serve_options, NULL); - if ( c == -1 ) - break; + if ( c == -1 ) { break; } read_serve_param( c, &ip_addr, &ip_port, &file, &sock, &default_deny ); } @@ -290,8 +291,7 @@ int mode_read( int argc, char *argv[] ) while (1){ c = getopt_long(argc, argv, read_short_options, read_options, NULL); - if ( c == -1 ) - break; + if ( c == -1 ) { break; } read_readwrite_param( c, &ip_addr, &ip_port, &bind_addr, &from, &size ); } @@ -326,8 +326,7 @@ int mode_write( int argc, char *argv[] ) while (1){ c = getopt_long(argc, argv, write_short_options, write_options, NULL); - if ( c == -1 ) - break; + if ( c == -1 ) { break; } read_readwrite_param( c, &ip_addr, &ip_port, &bind_addr, &from, &size ); } @@ -355,7 +354,7 @@ int mode_acl( int argc, char *argv[] ) while (1) { c = getopt_long( argc, argv, acl_short_options, acl_options, NULL ); - if ( c == -1 ) break; + if ( c == -1 ) { break; } read_acl_param( c, &sock ); } @@ -382,7 +381,7 @@ int mode_mirror( int argc, char *argv[] ) while (1) { c = getopt_long( argc, argv, mirror_short_options, mirror_options, NULL); - if ( -1 == c ) break; + if ( -1 == c ) { break; } read_mirror_param( c, &sock, &remote_argv[0], &remote_argv[1], &remote_argv[2] ); } @@ -396,10 +395,12 @@ int mode_mirror( int argc, char *argv[] ) } if ( err ) { exit_err( mirror_help_text ); } - if (argv[2] == NULL) + if (argv[2] == NULL) { do_remote_command( "mirror", sock, 2, remote_argv ); - else + } + else { do_remote_command( "mirror", sock, 3, remote_argv ); + } return 0; } @@ -412,7 +413,7 @@ int mode_status( int argc, char *argv[] ) while (1) { c = getopt_long( argc, argv, status_short_options, status_options, NULL ); - if ( -1 == c ) break; + if ( -1 == c ) { break; } read_status_param( c, &sock ); } diff --git a/src/ioutil.c b/src/ioutil.c index e8f3af7..442189d 100644 --- a/src/ioutil.c +++ b/src/ioutil.c @@ -29,8 +29,9 @@ struct bitset_mapping* build_allocation_map(int fd, uint64_t size, int resolutio fiemap_count->fm_mapped_extents = 0; /* Find out how many extents there are */ - if (ioctl(fd, FS_IOC_FIEMAP, fiemap_count) < 0) + if (ioctl(fd, FS_IOC_FIEMAP, fiemap_count) < 0) { return NULL; + } /* Resize fiemap to allow us to read in the extents */ fiemap = (struct fiemap*)xmalloc( @@ -80,20 +81,24 @@ int open_and_mmap(char* filename, int* out_fd, off64_t *out_size, void **out_map off64_t size; *out_fd = open(filename, O_RDWR|O_DIRECT|O_SYNC); - if (*out_fd < 1) + if (*out_fd < 1) { return *out_fd; + } size = lseek64(*out_fd, 0, SEEK_END); - if (size < 0) + if (size < 0) { return size; - if (out_size) + } + if (out_size) { *out_size = size; + } if (out_map) { *out_map = mmap64(NULL, size, PROT_READ|PROT_WRITE, MAP_SHARED, *out_fd, 0); - if (((long) *out_map) == -1) + if (((long) *out_map) == -1) { return -1; + } } debug("opened %s size %ld on fd %d @ %p", filename, size, *out_fd, *out_map); @@ -173,20 +178,19 @@ int splice_via_pipe_loop(int fd_in, int fd_out, size_t len) int pipefd[2]; /* read end, write end */ size_t spliced=0; - if (pipe(pipefd) == -1) + if (pipe(pipefd) == -1) { return -1; + } while (spliced < len) { ssize_t run = len-spliced; ssize_t s2, s1 = spliceloop(fd_in, NULL, pipefd[1], NULL, run, SPLICE_F_NONBLOCK); /*if (run > 65535) run = 65535;*/ - if (s1 < 0) - break; + if (s1 < 0) { break; } s2 = spliceloop(pipefd[0], NULL, fd_out, NULL, s1, 0); - if (s2 < 0) - break; + if (s2 < 0) { break; } spliced += s2; } close(pipefd[0]); @@ -202,10 +206,8 @@ int read_until_newline(int fd, char* buf, int bufsize) for (cur=0; cur < bufsize; cur++) { int result = read(fd, buf+cur, 1); - if (result < 0) - return -1; - if (buf[cur] == 10) - break; + if (result < 0) { return -1; } + if (buf[cur] == 10) { break; } } buf[cur++] = 0; @@ -221,12 +223,14 @@ int read_lines_until_blankline(int fd, int max_line_length, char ***lines) memset(line, 0, max_line_length+1); while (1) { - if (read_until_newline(fd, line, max_line_length) < 0) + if (read_until_newline(fd, line, max_line_length) < 0) { return lines_count; + } *lines = xrealloc(*lines, (lines_count+1) * sizeof(char*)); (*lines)[lines_count] = strdup(line); - if ((*lines)[lines_count][0] == 0) + if ((*lines)[lines_count][0] == 0) { return lines_count; + } lines_count++; } } diff --git a/src/parse.c b/src/parse.c index 70f34d4..790b72f 100644 --- a/src/parse.c +++ b/src/parse.c @@ -18,10 +18,10 @@ int parse_ip_to_sockaddr(struct sockaddr* out, char* src) /* allow user to start with [ and end with any other invalid char */ { int i=0, j=0; - if (src[i] == '[') - i++; - for (; i<64 && IS_IP_VALID_CHAR(src[i]); i++) + if (src[i] == '[') { i++; } + for (; i<64 && IS_IP_VALID_CHAR(src[i]); i++) { temp[j++] = src[i]; + } temp[j] = 0; } @@ -73,8 +73,9 @@ int parse_acl(struct ip_and_mask (**out)[], int max, char **entries) if (entries[i][j] == '/') { outentry->mask = atoi(entries[i]+j+1); - if (outentry->mask < 1 || outentry->mask > MAX_MASK_BITS) + if (outentry->mask < 1 || outentry->mask > MAX_MASK_BITS) { return i; + } } else { outentry->mask = MAX_MASK_BITS; diff --git a/src/readwrite.c b/src/readwrite.c index c427742..8f1ad9b 100644 --- a/src/readwrite.c +++ b/src/readwrite.c @@ -12,11 +12,12 @@ int socket_connect(struct sockaddr* to, struct sockaddr* from) int fd = socket(to->sa_family == AF_INET ? PF_INET : PF_INET6, SOCK_STREAM, 0); FATAL_IF_NEGATIVE(fd, "Couldn't create client socket"); - if (NULL != from) + if (NULL != from) { FATAL_IF_NEGATIVE( bind(fd, from, sizeof(struct sockaddr_in6)), "bind() failed" ); + } FATAL_IF_NEGATIVE( connect(fd, to, sizeof(struct sockaddr_in6)),"connect failed" @@ -29,10 +30,12 @@ off64_t socket_nbd_read_hello(int fd) struct nbd_init init; FATAL_IF_NEGATIVE(readloop(fd, &init, sizeof(init)), "Couldn't read init"); - if (strncmp(init.passwd, INIT_PASSWD, 8) != 0) + if (strncmp(init.passwd, INIT_PASSWD, 8) != 0) { fatal("wrong passwd"); - if (be64toh(init.magic) != INIT_MAGIC) + } + if (be64toh(init.magic) != INIT_MAGIC) { fatal("wrong magic (%x)", be64toh(init.magic)); + } return be64toh(init.size); } @@ -50,12 +53,15 @@ void read_reply(int fd, struct nbd_request *request, struct nbd_reply *reply) { FATAL_IF_NEGATIVE(readloop(fd, reply, sizeof(*reply)), "Couldn't read reply"); - if (be32toh(reply->magic) != REPLY_MAGIC) + if (be32toh(reply->magic) != REPLY_MAGIC) { fatal("Reply magic incorrect (%p)", be32toh(reply->magic)); - if (be32toh(reply->error) != 0) + } + if (be32toh(reply->error) != 0) { fatal("Server replied with error %d", be32toh(reply->error)); - if (strncmp(request->handle, reply->handle, 8) != 0) + } + if (strncmp(request->handle, reply->handle, 8) != 0) { fatal("Did not reply with correct handle"); + } } void socket_nbd_read(int fd, off64_t from, int len, int out_fd, void* out_buf) @@ -105,11 +111,11 @@ void socket_nbd_write(int fd, off64_t from, int len, int in_fd, void* in_buf) #define CHECK_RANGE(error_type) { \ off64_t size = socket_nbd_read_hello(params->client); \ - if (params->from < 0 || (params->from + params->len) > size) \ + if (params->from < 0 || (params->from + params->len) > size) {\ fatal(error_type \ " request %d+%d is out of range given size %d", \ params->from, params->len, size\ - ); \ + ); }\ } void do_read(struct mode_readwrite_params* params) diff --git a/src/readwrite.h b/src/readwrite.h index 2dabbfc..22ce723 100644 --- a/src/readwrite.h +++ b/src/readwrite.h @@ -1,6 +1,6 @@ -#ifndef __READWRITE_H +#ifndef READWRITE_H -#define __READWRITE_H +#define READWRITE_H int socket_connect(struct sockaddr* to, struct sockaddr* from); off64_t socket_nbd_read_hello(int fd); diff --git a/src/remote.c b/src/remote.c index 29d805b..5eb9df6 100644 --- a/src/remote.c +++ b/src/remote.c @@ -41,8 +41,9 @@ void do_remote_command(char* command, char* socket_name, int argc, char** argv) ); exit_status = atoi(response); - if (exit_status > 0) + if (exit_status > 0) { fprintf(stderr, "%s\n", strchr(response, ':')+2); + } exit(atoi(response)); diff --git a/src/serve.c b/src/serve.c index e5bee9c..ce2cf1c 100644 --- a/src/serve.c +++ b/src/serve.c @@ -27,10 +27,12 @@ static inline void* sockaddr_address_data(struct sockaddr* sockaddr) struct sockaddr_in* in = (struct sockaddr_in*) sockaddr; struct sockaddr_in6* in6 = (struct sockaddr_in6*) sockaddr; - if (sockaddr->sa_family == AF_INET) + if (sockaddr->sa_family == AF_INET) { return &in->sin_addr; - if (sockaddr->sa_family == AF_INET6) + } + if (sockaddr->sa_family == AF_INET6) { return &in6->sin6_addr; + } return NULL; } @@ -63,8 +65,9 @@ struct server * server_create ( out->control_socket_name = s_ctrl_sock; out->acl = acl_create( acl_entries, s_acl_entries, default_deny ); - if (out->acl && out->acl->len != acl_entries) + if (out->acl && out->acl->len != acl_entries) { fatal("Bad ACL entry '%s'", s_acl_entries[out->acl->len]); + } parse_port( s_port, &out->bind_to.v4 ); @@ -103,8 +106,9 @@ void server_dirty(struct server *serve, off64_t from, int len) { NULLCHECK( serve ); - if (serve->mirror) + if (serve->mirror) { bitset_set_range(serve->mirror->dirty_map, from, len); + } } #define SERVER_LOCK( s, f, msg ) \ @@ -209,8 +213,9 @@ int tryjoin_client_thread( struct client_tbl_entry *entry, int (*joinfunc)(pthre 64 ); if (joinfunc(entry->thread, &status) != 0) { - if (errno != EBUSY) + if (errno != EBUSY) { FATAL_IF_NEGATIVE(-1, "Problem with joining thread"); + } } else { debug("nbd thread %p exited (%s) with status %ld", @@ -476,8 +481,9 @@ int server_accept( struct server * params ) FD_SET(params->server_fd, &fds); self_pipe_fd_set( params->close_signal, &fds ); self_pipe_fd_set( params->acl_updated_signal, &fds ); - if (params->control_socket_name) + if (params->control_socket_name) { FD_SET(params->control_fd, &fds); + } FATAL_IF_NEGATIVE(select(FD_SETSIZE, &fds, NULL, NULL, NULL), "select() failed"); diff --git a/src/util.c b/src/util.c index 95e2c69..1af9a32 100644 --- a/src/util.c +++ b/src/util.c @@ -33,8 +33,7 @@ void mylog(int line_level, const char* format, ...) { va_list argptr; - if (line_level < log_level) - return; + if (line_level < log_level) { return; } va_start(argptr, format); vfprintf(stderr, format, argptr); diff --git a/src/util.h b/src/util.h index 253b894..eee1af0 100644 --- a/src/util.h +++ b/src/util.h @@ -49,8 +49,7 @@ extern pthread_key_t cleanup_handler_key; switch (setjmp(context->jmp)) \ { \ case 0: /* setup time */ \ - if (old) \ - free(old); \ + if (old) { free(old); }\ pthread_setspecific(cleanup_handler_key, context); \ break; \ case 1: /* fatal error, terminate thread */ \ @@ -95,12 +94,12 @@ void mylog(int line_level, const char* format, ...); error_handler(1); \ } -#define ERROR_IF_NULL(value, msg, ...) if (NULL == value) error(msg " (errno=%d, %s)", ##__VA_ARGS__, errno, strerror(errno)) -#define ERROR_IF_NEGATIVE(value, msg, ...) if (value < 0) error(msg, ##__VA_ARGS__) -#define ERROR_IF_ZERO(value, msg, ...) if (0 == value) error(msg, ##__VA_ARGS__) -#define FATAL_IF_NULL(value, msg, ...) if (NULL == value) fatal(msg, ##__VA_ARGS__) -#define FATAL_IF_NEGATIVE(value, msg, ...) if (value < 0) fatal(msg " (errno=%d, %s)", ##__VA_ARGS__, errno, strerror(errno)) -#define FATAL_IF_ZERO(value, msg, ...) if (0 == value) fatal(msg, ##__VA_ARGS__) +#define ERROR_IF_NULL(value, msg, ...) if (NULL == value) { error(msg " (errno=%d, %s)", ##__VA_ARGS__, errno, strerror(errno)) } +#define ERROR_IF_NEGATIVE(value, msg, ...) if (value < 0) { error(msg, ##__VA_ARGS__) } +#define ERROR_IF_ZERO(value, msg, ...) if (0 == value) { error(msg, ##__VA_ARGS__) } +#define FATAL_IF_NULL(value, msg, ...) if (NULL == value) { fatal(msg, ##__VA_ARGS__) } +#define FATAL_IF_NEGATIVE(value, msg, ...) if (value < 0) { fatal(msg " (errno=%d, %s)", ##__VA_ARGS__, errno, strerror(errno)) } +#define FATAL_IF_ZERO(value, msg, ...) if (0 == value) { fatal(msg, ##__VA_ARGS__) } #define NULLCHECK(value) FATAL_IF_NULL(value, "BUG: " #value " is null")