From 76c7550c8ab0e9c0ee14a9c396baa008cfb9bc42 Mon Sep 17 00:00:00 2001 From: Guus Sliepen Date: Sun, 13 Aug 2017 15:06:51 +0200 Subject: [PATCH] Convert sizeof foo to sizeof(foo). While technically sizeof is an operator and doesn't need the parentheses around expressions it operates on, except if they are type names, code formatters don't seem to handle this very well. --- examples/channels.c | 2 +- examples/chat.c | 2 +- examples/chatpp.cc | 2 +- examples/manynodes.c | 8 +-- src/conf.c | 10 ++-- src/discovery.c | 2 +- src/event.c | 4 +- src/graph.c | 2 +- src/hash.c | 10 ++-- src/logger.c | 4 +- src/meshlink.c | 122 +++++++++++++++++++++---------------------- src/meshlink_queue.h | 2 +- src/meta.c | 4 +- src/net_packet.c | 4 +- src/net_socket.c | 26 ++++----- src/netutl.c | 12 ++--- src/node.c | 2 +- src/prf.c | 8 +-- src/protocol.c | 4 +- src/protocol_auth.c | 30 +++++------ src/protocol_key.c | 12 ++--- src/route.c | 6 +-- src/sptps.c | 12 ++--- src/sptps_speed.c | 8 +-- src/sptps_test.c | 14 ++--- src/utils.c | 4 +- src/xalloc.h | 2 +- test/channels-fork.c | 8 +-- test/sign-verify.c | 14 ++--- 29 files changed, 170 insertions(+), 170 deletions(-) diff --git a/examples/channels.c b/examples/channels.c index c0c7210d..a869f513 100644 --- a/examples/channels.c +++ b/examples/channels.c @@ -261,7 +261,7 @@ int main(int argc, char *argv[]) { printf("Chat started.\nType /help for a list of commands.\n"); - while(fgets(buf, sizeof buf, stdin)) + while(fgets(buf, sizeof(buf), stdin)) parse_input(mesh, buf); printf("Chat stopping.\n"); diff --git a/examples/chat.c b/examples/chat.c index 4d65d10e..f5f41c93 100644 --- a/examples/chat.c +++ b/examples/chat.c @@ -209,7 +209,7 @@ int main(int argc, char *argv[]) { printf("Chat started.\nType /help for a list of commands.\n"); - while(fgets(buf, sizeof buf, stdin)) + while(fgets(buf, sizeof(buf), stdin)) parse_input(mesh, buf); printf("Chat stopping.\n"); diff --git a/examples/chatpp.cc b/examples/chatpp.cc index 8c925231..b5fe44e6 100644 --- a/examples/chatpp.cc +++ b/examples/chatpp.cc @@ -206,7 +206,7 @@ int main(int argc, char *argv[]) { printf("Chat started.\nType /help for a list of commands.\n"); - while(fgets(buf, sizeof buf, stdin)) + while(fgets(buf, sizeof(buf), stdin)) parse_input(&mesh, buf); printf("Chat stopping.\n"); diff --git a/examples/manynodes.c b/examples/manynodes.c index 0089c206..77d895a3 100644 --- a/examples/manynodes.c +++ b/examples/manynodes.c @@ -368,7 +368,7 @@ int main(int argc, char *argv[]) { if(argc > 4) graphexporttimeout = argv[4]; - mesh = calloc(n, sizeof *mesh); + mesh = calloc(n, sizeof(*mesh)); meshlink_set_log_cb(NULL, MESHLINK_DEBUG, log_message); #ifndef _WIN32 @@ -380,8 +380,8 @@ int main(int argc, char *argv[]) { char filename[PATH_MAX]; char nodename[100]; for(int i = 0; i < n; i++) { - snprintf(nodename, sizeof nodename, "%snode%d", namesprefix,i); - snprintf(filename, sizeof filename, "%s/%s", basebase, nodename); + snprintf(nodename, sizeof(nodename), "%snode%d", namesprefix,i); + snprintf(filename, sizeof(filename), "%s/%s", basebase, nodename); bool itsnew = access(filename, R_OK); if(n/(i+1) > n/4) mesh[i] = meshlink_open(filename, nodename, "manynodes", DEV_CLASS_BACKBONE); @@ -415,7 +415,7 @@ int main(int argc, char *argv[]) { printf("%d nodes started.\nType /help for a list of commands.\n", started); // handle input - while(fgets(buf, sizeof buf, stdin)) + while(fgets(buf, sizeof(buf), stdin)) parse_input(buf); exportmeshgraph_end(NULL); diff --git a/src/conf.c b/src/conf.c index 426f6dc4..19cfa364 100644 --- a/src/conf.c +++ b/src/conf.c @@ -284,7 +284,7 @@ bool read_config_file(splay_tree_t *config_tree, const char *fname) { } for(;;) { - line = readline(fp, buffer, sizeof buffer); + line = readline(fp, buffer, sizeof(buffer)); if(!line) { if(feof(fp)) @@ -390,8 +390,8 @@ bool modify_config_file(struct meshlink_handle *mesh, const char *name, const ch char tmpname[PATH_MAX]; bool error = false; - snprintf(filename, sizeof filename, "%s" SLASH "hosts" SLASH "%s", mesh->confbase, name); - snprintf(tmpname, sizeof tmpname, "%s.tmp", filename); + snprintf(filename, sizeof(filename), "%s" SLASH "hosts" SLASH "%s", mesh->confbase, name); + snprintf(tmpname, sizeof(tmpname), "%s.tmp", filename); FILE *fr = fopen(filename, "r"); @@ -412,7 +412,7 @@ bool modify_config_file(struct meshlink_handle *mesh, const char *name, const ch char *sep; bool found = false; - while(readline(fr, buf, sizeof buf)) { + while(readline(fr, buf, sizeof(buf))) { if(!*buf || *buf == '#') goto copy; @@ -472,7 +472,7 @@ copy: // Try to atomically replace the old config file with the new one. #ifdef HAVE_MINGW char bakname[PATH_MAX]; - snprintf(bakname, sizeof bakname, "%s.bak", filename); + snprintf(bakname, sizeof(bakname), "%s.bak", filename); if(rename(filename, bakname) || rename(tmpname, filename)) { rename(bakname, filename); #else diff --git a/src/discovery.c b/src/discovery.c index 885b2bfd..d82f48b7 100644 --- a/src/discovery.c +++ b/src/discovery.c @@ -463,7 +463,7 @@ bool discovery_start(meshlink_handle_t *mesh) { // Start the discovery thread if(pthread_create(&mesh->discovery_thread, NULL, discovery_loop, mesh) != 0) { logger(mesh, MESHLINK_ERROR, "Could not start discovery thread: %s\n", strerror(errno)); - memset(&mesh->discovery_thread, 0, sizeof mesh->discovery_thread); + memset(&mesh->discovery_thread, 0, sizeof(mesh)->discovery_thread); goto fail; } diff --git a/src/event.c b/src/event.c index 0a5f97e8..6ca44d48 100644 --- a/src/event.c +++ b/src/event.c @@ -216,8 +216,8 @@ bool event_loop_run(event_loop_t *loop, pthread_mutex_t *mutex) { tv = ⁢ } - memcpy(&readable, &loop->readfds, sizeof readable); - memcpy(&writable, &loop->writefds, sizeof writable); + memcpy(&readable, &loop->readfds, sizeof(readable)); + memcpy(&writable, &loop->writefds, sizeof(writable)); int fds = 0; diff --git a/src/graph.c b/src/graph.c index c57cd90f..caf33f5e 100644 --- a/src/graph.c +++ b/src/graph.c @@ -227,7 +227,7 @@ static void check_reachability(meshlink_handle_t *mesh) { if(!n->status.reachable) { update_node_udp(mesh, n, NULL); - memset(&n->status, 0, sizeof n->status); + memset(&n->status, 0, sizeof(n)->status); n->options = 0; } else if(n->connection) { if(n->connection->outgoing) diff --git a/src/hash.c b/src/hash.c index e0733386..5d90e55c 100644 --- a/src/hash.c +++ b/src/hash.c @@ -52,11 +52,11 @@ static uint32_t modulo(uint32_t hash, size_t n) { /* (De)allocation */ hash_t *hash_alloc(size_t n, size_t size) { - hash_t *hash = xzalloc(sizeof *hash); + hash_t *hash = xzalloc(sizeof(*hash)); hash->n = n; hash->size = size; hash->keys = xzalloc(hash->n * hash->size); - hash->values = xzalloc(hash->n * sizeof *hash->values); + hash->values = xzalloc(hash->n * sizeof(*hash->values)); return hash; } @@ -93,14 +93,14 @@ void *hash_search_or_insert(hash_t *hash, const void *key, const void *value) { /* Utility functions */ void hash_clear(hash_t *hash) { - memset(hash->values, 0, hash->n * sizeof *hash->values); + memset(hash->values, 0, hash->n * sizeof(*hash->values)); } void hash_resize(hash_t *hash, size_t n) { hash->keys = xrealloc(hash->keys, n * hash->size); - hash->values = xrealloc(hash->values, n * sizeof *hash->values); + hash->values = xrealloc(hash->values, n * sizeof(*hash->values)); if(n > hash->n) { memset(hash->keys + hash->n * hash->size, 0, (n - hash->n) * hash->size); - memset(hash->values + hash->n, 0, (n - hash->n) * sizeof *hash->values); + memset(hash->values + hash->n, 0, (n - hash->n) * sizeof(*hash->values)); } } diff --git a/src/logger.c b/src/logger.c index ecb55a53..f77252bd 100644 --- a/src/logger.c +++ b/src/logger.c @@ -37,10 +37,10 @@ void logger(meshlink_handle_t *mesh, meshlink_log_level_t level, const char *for char message[1024] = ""; va_start(ap, format); - int len = vsnprintf(message, sizeof message, format, ap); + int len = vsnprintf(message, sizeof(message), format, ap); va_end(ap); - if(len > 0 && len < sizeof message && message[len - 1] == '\n') + if(len > 0 && len < sizeof(message) && message[len - 1] == '\n') message[len - 1] = 0; if(mesh) diff --git a/src/meshlink.c b/src/meshlink.c index 6a2c99da..7da7a9e1 100644 --- a/src/meshlink.c +++ b/src/meshlink.c @@ -126,7 +126,7 @@ static bool fcopy(FILE *out, const char *filename) { char buf[1024]; size_t len; - while((len = fread(buf, 1, sizeof buf, in))) + while((len = fread(buf, 1, sizeof(buf), in))) fwrite(buf, len, 1, out); fclose(in); return true; @@ -148,7 +148,7 @@ static void scan_for_hostname(const char *filename, char **hostname, char **port if(!f) return; - while(fgets(line, sizeof line, f)) { + while(fgets(line, sizeof(line), f)) { if(!rstrip(line)) continue; char *p = line, *q; @@ -199,8 +199,8 @@ static void set_timeout(int sock, int timeout) { tv.tv_sec = timeout / 1000; tv.tv_usec = (timeout - tv.tv_sec * 1000) * 1000; #endif - setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof tv); - setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof tv); + setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)); + setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)); } char *meshlink_get_external_address(meshlink_handle_t *mesh) { @@ -222,8 +222,8 @@ char *meshlink_get_external_address(meshlink_handle_t *mesh) { } } if(s >= 0) { - send(s, request, sizeof request - 1, 0); - int len = recv(s, line, sizeof line - 1, MSG_WAITALL); + send(s, request, sizeof(request) - 1, 0); + int len = recv(s, line, sizeof(line) - 1, MSG_WAITALL); if(len > 0) { line[len] = 0; if(line[len - 1] == '\n') @@ -264,7 +264,7 @@ static char *get_my_hostname(meshlink_handle_t* mesh) { FILE *f; // Use first Address statement in own host config file - snprintf(filename, sizeof filename, "%s" SLASH "hosts" SLASH "%s", mesh->confbase, name); + snprintf(filename, sizeof(filename), "%s" SLASH "hosts" SLASH "%s", mesh->confbase, name); scan_for_hostname(filename, &hostname, &port); if(hostname) @@ -311,7 +311,7 @@ static char *get_line(const char **data) { static char line[1024]; const char *end = strchr(*data, '\n'); size_t len = end ? end - *data : strlen(*data); - if(len >= sizeof line) { + if(len >= sizeof(line)) { logger(NULL, MESHLINK_ERROR, "Maximum line length exceeded!\n"); return NULL; } @@ -354,7 +354,7 @@ static bool try_bind(int port) { }; char portstr[16]; - snprintf(portstr, sizeof portstr, "%d", port); + snprintf(portstr, sizeof(portstr), "%d", port); if(getaddrinfo(NULL, portstr, &hint, &ai) || !ai) return false; @@ -383,7 +383,7 @@ static int check_port(meshlink_handle_t *mesh) { int port = 0x1000 + (rand() & 0x7fff); if(try_bind(port)) { char filename[PATH_MAX]; - snprintf(filename, sizeof filename, "%s" SLASH "hosts" SLASH "%s", mesh->confbase, mesh->name); + snprintf(filename, sizeof(filename), "%s" SLASH "hosts" SLASH "%s", mesh->confbase, mesh->name); FILE *f = fopen(filename, "a"); if(!f) { logger(mesh, MESHLINK_DEBUG, "Please change MeshLink's Port manually.\n"); @@ -413,7 +413,7 @@ static bool finalize_join(meshlink_handle_t *mesh) { } char filename[PATH_MAX]; - snprintf(filename, sizeof filename, "%s" SLASH "meshlink.conf", mesh->confbase); + snprintf(filename, sizeof(filename), "%s" SLASH "meshlink.conf", mesh->confbase); FILE *f = fopen(filename, "w"); if(!f) { @@ -423,7 +423,7 @@ static bool finalize_join(meshlink_handle_t *mesh) { fprintf(f, "Name = %s\n", name); - snprintf(filename, sizeof filename, "%s" SLASH "hosts" SLASH "%s", mesh->confbase, name); + snprintf(filename, sizeof(filename), "%s" SLASH "hosts" SLASH "%s", mesh->confbase, name); FILE *fh = fopen(filename, "w"); if(!fh) { logger(mesh, MESHLINK_DEBUG, "Could not create file %s: %s\n", filename, strerror(errno)); @@ -496,7 +496,7 @@ static bool finalize_join(meshlink_handle_t *mesh) { return false; } - snprintf(filename, sizeof filename, "%s" SLASH "hosts" SLASH "%s", mesh->confbase, value); + snprintf(filename, sizeof(filename), "%s" SLASH "hosts" SLASH "%s", mesh->confbase, value); f = fopen(filename, "w"); if(!f) { @@ -570,7 +570,7 @@ static bool invitation_receive(void *handle, uint8_t type, const void *msg, uint meshlink_handle_t* mesh = handle; switch(type) { case SPTPS_HANDSHAKE: - return sptps_send_record(&(mesh->sptps), 0, mesh->cookie, sizeof mesh->cookie); + return sptps_send_record(&(mesh->sptps), 0, mesh->cookie, sizeof(mesh)->cookie); case 0: mesh->data = xrealloc(mesh->data, mesh->thedatalen + len + 1); @@ -603,7 +603,7 @@ static bool recvline(meshlink_handle_t* mesh, size_t len) { abort(); while(!(newline = memchr(mesh->buffer, '\n', mesh->blen))) { - int result = recv(mesh->sock, mesh->buffer + mesh->blen, sizeof mesh->buffer - mesh->blen, 0); + int result = recv(mesh->sock, mesh->buffer + mesh->blen, sizeof(mesh)->buffer - mesh->blen, 0); if(result == -1 && errno == EINTR) continue; else if(result <= 0) @@ -630,10 +630,10 @@ static bool sendline(int fd, char *format, ...) { va_list ap; va_start(ap, format); - blen = vsnprintf(buffer, sizeof buffer, format, ap); + blen = vsnprintf(buffer, sizeof(buffer), format, ap); va_end(ap); - if(blen < 1 || blen >= sizeof buffer) + if(blen < 1 || blen >= sizeof(buffer)) return false; buffer[blen] = '\n'; @@ -666,7 +666,7 @@ static const char *errstr[] = { }; const char *meshlink_strerror(meshlink_errno_t err) { - if(err < 0 || err >= sizeof errstr / sizeof *errstr) + if(err < 0 || err >= sizeof(errstr) / sizeof(*errstr)) return "Invalid error code"; return errstr[err]; } @@ -685,7 +685,7 @@ static bool ecdsa_keygen(meshlink_handle_t *mesh) { } else logger(mesh, MESHLINK_DEBUG, "Done.\n"); - snprintf(privname, sizeof privname, "%s" SLASH "ecdsa_key.priv", mesh->confbase); + snprintf(privname, sizeof(privname), "%s" SLASH "ecdsa_key.priv", mesh->confbase); f = fopen(privname, "wb"); if(!f) { @@ -707,7 +707,7 @@ static bool ecdsa_keygen(meshlink_handle_t *mesh) { fclose(f); - snprintf(pubname, sizeof pubname, "%s" SLASH "hosts" SLASH "%s", mesh->confbase, mesh->name); + snprintf(pubname, sizeof(pubname), "%s" SLASH "hosts" SLASH "%s", mesh->confbase, mesh->name); f = fopen(pubname, "a"); if(!f) { @@ -766,7 +766,7 @@ static bool getlocaladdrname(char *destaddr, char *host, socklen_t hostlen) { freeaddrinfo(rai); struct sockaddr_storage sn; - socklen_t sl = sizeof sn; + socklen_t sl = sizeof(sn); if(getsockname(sock, (struct sockaddr *)&sn, &sl)) return false; @@ -784,15 +784,15 @@ static void add_local_addresses(meshlink_handle_t *mesh) { // IPv4 example.org - if(getlocaladdrname("93.184.216.34", host, sizeof host)) { - snprintf(entry, sizeof entry, "%s %s", host, mesh->myport); + if(getlocaladdrname("93.184.216.34", host, sizeof(host))) { + snprintf(entry, sizeof(entry), "%s %s", host, mesh->myport); append_config_file(mesh, mesh->name, "Address", entry); } // IPv6 example.org - if(getlocaladdrname("2606:2800:220:1:248:1893:25c8:1946", host, sizeof host)) { - snprintf(entry, sizeof entry, "%s %s", host, mesh->myport); + if(getlocaladdrname("2606:2800:220:1:248:1893:25c8:1946", host, sizeof(host))) { + snprintf(entry, sizeof(entry), "%s %s", host, mesh->myport); append_config_file(mesh, mesh->name, "Address", entry); } } @@ -805,7 +805,7 @@ static bool meshlink_setup(meshlink_handle_t *mesh) { } char filename[PATH_MAX]; - snprintf(filename, sizeof filename, "%s" SLASH "hosts", mesh->confbase); + snprintf(filename, sizeof(filename), "%s" SLASH "hosts", mesh->confbase); if(mkdir(filename, 0777) && errno != EEXIST) { logger(mesh, MESHLINK_DEBUG, "Could not create directory %s: %s\n", filename, strerror(errno)); @@ -813,7 +813,7 @@ static bool meshlink_setup(meshlink_handle_t *mesh) { return false; } - snprintf(filename, sizeof filename, "%s" SLASH "meshlink.conf", mesh->confbase); + snprintf(filename, sizeof(filename), "%s" SLASH "meshlink.conf", mesh->confbase); if(!access(filename, F_OK)) { logger(mesh, MESHLINK_DEBUG, "Configuration file %s already exists!\n", filename); @@ -897,7 +897,7 @@ meshlink_handle_t *meshlink_open(const char *confbase, const char *name, const c // Check whether meshlink.conf already exists char filename[PATH_MAX]; - snprintf(filename, sizeof filename, "%s" SLASH "meshlink.conf", confbase); + snprintf(filename, sizeof(filename), "%s" SLASH "meshlink.conf", confbase); if(access(filename, R_OK)) { if(errno == ENOENT) { @@ -1001,7 +1001,7 @@ bool meshlink_start(meshlink_handle_t *mesh) { if(pthread_create(&mesh->thread, NULL, meshlink_main_loop, mesh) != 0) { logger(mesh, MESHLINK_DEBUG, "Could not start thread: %s\n", strerror(errno)); - memset(&mesh->thread, 0, sizeof mesh->thread); + memset(&mesh->thread, 0, sizeof(mesh)->thread); meshlink_errno = MESHLINK_EINTERNAL; pthread_mutex_unlock(&(mesh->mesh_mutex)); return false; @@ -1080,7 +1080,7 @@ void meshlink_close(meshlink_handle_t *mesh) { free(mesh->confbase); pthread_mutex_destroy(&(mesh->mesh_mutex)); - memset(mesh, 0, sizeof *mesh); + memset(mesh, 0, sizeof(*mesh)); free(mesh); } @@ -1093,7 +1093,7 @@ static void deltree(const char *dirname) { if(ent->d_name[0] == '.') continue; char filename[PATH_MAX]; - snprintf(filename, sizeof filename, "%s" SLASH "%s", dirname, ent->d_name); + snprintf(filename, sizeof(filename), "%s" SLASH "%s", dirname, ent->d_name); if(unlink(filename)) deltree(filename); } @@ -1110,7 +1110,7 @@ bool meshlink_destroy(const char *confbase) { } char filename[PATH_MAX]; - snprintf(filename, sizeof filename, "%s" SLASH "meshlink.conf", confbase); + snprintf(filename, sizeof(filename), "%s" SLASH "meshlink.conf", confbase); if(unlink(filename)) { if(errno == ENOENT) { @@ -1166,7 +1166,7 @@ bool meshlink_send(meshlink_handle_t *mesh, meshlink_node_t *destination, const meshlink_packethdr_t *hdr; // Validate arguments - if(!mesh || !destination || len >= MAXSIZE - sizeof *hdr) { + if(!mesh || !destination || len >= MAXSIZE - sizeof(*hdr)) { meshlink_errno = MESHLINK_EINVAL; return false; } @@ -1180,7 +1180,7 @@ bool meshlink_send(meshlink_handle_t *mesh, meshlink_node_t *destination, const } // Prepare the packet - vpn_packet_t *packet = malloc(sizeof *packet); + vpn_packet_t *packet = malloc(sizeof(*packet)); if(!packet) { meshlink_errno = MESHLINK_ENOMEM; return false; @@ -1188,16 +1188,16 @@ bool meshlink_send(meshlink_handle_t *mesh, meshlink_node_t *destination, const packet->probe = false; packet->tcp = false; - packet->len = len + sizeof *hdr; + packet->len = len + sizeof(*hdr); hdr = (meshlink_packethdr_t *)packet->data; - memset(hdr, 0, sizeof *hdr); + memset(hdr, 0, sizeof(*hdr)); // leave the last byte as 0 to make sure strings are always // null-terminated if they are longer than the buffer - strncpy(hdr->destination, destination->name, (sizeof hdr->destination) - 1); - strncpy(hdr->source, mesh->self->name, (sizeof hdr->source) -1); + strncpy(hdr->destination, destination->name, (sizeof(hdr)->destination) - 1); + strncpy(hdr->source, mesh->self->name, (sizeof(hdr)->source) -1); - memcpy(packet->data + sizeof *hdr, data, len); + memcpy(packet->data + sizeof(*hdr), data, len); // Queue it if(!meshlink_queue_push(&mesh->outpacketqueue, packet)) { @@ -1302,7 +1302,7 @@ meshlink_node_t **meshlink_get_all_nodes(meshlink_handle_t *mesh, meshlink_node_ pthread_mutex_lock(&(mesh->mesh_mutex)); *nmemb = mesh->nodes->count; - result = realloc(nodes, *nmemb * sizeof *nodes); + result = realloc(nodes, *nmemb * sizeof(*nodes)); if(result) { meshlink_node_t **p = result; @@ -1374,7 +1374,7 @@ static bool refresh_invitation_key(meshlink_handle_t *mesh) { pthread_mutex_lock(&(mesh->mesh_mutex)); - snprintf(filename, sizeof filename, "%s" SLASH "invitations", mesh->confbase); + snprintf(filename, sizeof(filename), "%s" SLASH "invitations", mesh->confbase); if(mkdir(filename, 0700) && errno != EEXIST) { logger(mesh, MESHLINK_DEBUG, "Could not create directory %s: %s\n", filename, strerror(errno)); meshlink_errno = MESHLINK_ESTORAGE; @@ -1401,7 +1401,7 @@ static bool refresh_invitation_key(meshlink_handle_t *mesh) { continue; char invname[PATH_MAX]; struct stat st; - snprintf(invname, sizeof invname, "%s" SLASH "%s", filename, ent->d_name); + snprintf(invname, sizeof(invname), "%s" SLASH "%s", filename, ent->d_name); if(!stat(invname, &st)) { if(mesh->invitation_key && deadline < st.st_mtime) count++; @@ -1423,7 +1423,7 @@ static bool refresh_invitation_key(meshlink_handle_t *mesh) { closedir(dir); - snprintf(filename, sizeof filename, "%s" SLASH "invitations" SLASH "ecdsa_key.priv", mesh->confbase); + snprintf(filename, sizeof(filename), "%s" SLASH "invitations" SLASH "ecdsa_key.priv", mesh->confbase); // Remove the key if there are no outstanding invitations. if(!count) { @@ -1560,8 +1560,8 @@ bool meshlink_set_port(meshlink_handle_t *mesh, int port) { exit_configuration(&mesh->config); char portstr[10]; - snprintf(portstr, sizeof portstr, "%d", port); - portstr[sizeof portstr - 1] = 0; + snprintf(portstr, sizeof(portstr), "%d", port); + portstr[sizeof(portstr) - 1] = 0; modify_config_file(mesh, mesh->name, "Port", portstr, true); @@ -1598,7 +1598,7 @@ char *meshlink_invite(meshlink_handle_t *mesh, const char *name) { // Ensure no host configuration file with that name exists char filename[PATH_MAX]; - snprintf(filename, sizeof filename, "%s" SLASH "hosts" SLASH "%s", mesh->confbase, name); + snprintf(filename, sizeof(filename), "%s" SLASH "hosts" SLASH "%s", mesh->confbase, name); if(!access(filename, F_OK)) { logger(mesh, MESHLINK_DEBUG, "A host config file for %s already exists!\n", name); meshlink_errno = MESHLINK_EEXIST; @@ -1644,8 +1644,8 @@ char *meshlink_invite(meshlink_handle_t *mesh, const char *name) { char buf[18 + strlen(fingerprint)]; char cookiehash[64]; memcpy(buf, cookie, 18); - memcpy(buf + 18, fingerprint, sizeof buf - 18); - sha512(buf, sizeof buf, cookiehash); + memcpy(buf + 18, fingerprint, sizeof(buf) - 18); + sha512(buf, sizeof(buf), cookiehash); b64encode_urlsafe(cookiehash, cookiehash, 18); b64encode_urlsafe(cookie, cookie, 18); @@ -1653,7 +1653,7 @@ char *meshlink_invite(meshlink_handle_t *mesh, const char *name) { free(fingerprint); // Create a file containing the details of the invitation. - snprintf(filename, sizeof filename, "%s" SLASH "invitations" SLASH "%s", mesh->confbase, cookiehash); + snprintf(filename, sizeof(filename), "%s" SLASH "invitations" SLASH "%s", mesh->confbase, cookiehash); int ifd = open(filename, O_RDWR | O_CREAT | O_EXCL, 0600); if(!ifd) { logger(mesh, MESHLINK_DEBUG, "Could not create invitation file %s: %s\n", filename, strerror(errno)); @@ -1672,11 +1672,11 @@ char *meshlink_invite(meshlink_handle_t *mesh, const char *name) { fprintf(f, "ConnectTo = %s\n", mesh->self->name); // Copy Broadcast and Mode - snprintf(filename, sizeof filename, "%s" SLASH "meshlink.conf", mesh->confbase); + snprintf(filename, sizeof(filename), "%s" SLASH "meshlink.conf", mesh->confbase); FILE *tc = fopen(filename, "r"); if(tc) { char buf[1024]; - while(fgets(buf, sizeof buf, tc)) { + while(fgets(buf, sizeof(buf), tc)) { if((!strncasecmp(buf, "Mode", 4) && strchr(" \t=", buf[4])) || (!strncasecmp(buf, "Broadcast", 9) && strchr(" \t=", buf[9]))) { fputs(buf, f); @@ -1696,7 +1696,7 @@ char *meshlink_invite(meshlink_handle_t *mesh, const char *name) { fprintf(f, "#---------------------------------------------------------------#\n"); fprintf(f, "Name = %s\n", mesh->self->name); - snprintf(filename, sizeof filename, "%s" SLASH "hosts" SLASH "%s", mesh->confbase, mesh->self->name); + snprintf(filename, sizeof(filename), "%s" SLASH "hosts" SLASH "%s", mesh->confbase, mesh->self->name); fcopy(f, filename); fclose(f); @@ -1817,7 +1817,7 @@ bool meshlink_join(meshlink_handle_t *mesh, const char *invitation) { char hisname[4096] = ""; int code, hismajor, hisminor = 0; - if(!recvline(mesh, sizeof mesh->line) || sscanf(mesh->line, "%d %s %d.%d", &code, hisname, &hismajor, &hisminor) < 3 || code != 0 || hismajor != PROT_MAJOR || !check_id(hisname) || !recvline(mesh, sizeof mesh->line) || !rstrip(mesh->line) || sscanf(mesh->line, "%d ", &code) != 1 || code != ACK || strlen(mesh->line) < 3) { + if(!recvline(mesh, sizeof(mesh)->line) || sscanf(mesh->line, "%d %s %d.%d", &code, hisname, &hismajor, &hisminor) < 3 || code != 0 || hismajor != PROT_MAJOR || !check_id(hisname) || !recvline(mesh, sizeof(mesh)->line) || !rstrip(mesh->line) || sscanf(mesh->line, "%d ", &code) != 1 || code != ACK || strlen(mesh->line) < 3) { logger(mesh, MESHLINK_DEBUG, "Cannot read greeting from peer\n"); closesocket(mesh->sock); meshlink_errno = MESHLINK_ENETWORK; @@ -1850,7 +1850,7 @@ bool meshlink_join(meshlink_handle_t *mesh, const char *invitation) { } // Start an SPTPS session - if(!sptps_start(&mesh->sptps, mesh, true, false, key, hiskey, meshlink_invitation_label, sizeof meshlink_invitation_label, invitation_send, invitation_receive)) { + if(!sptps_start(&mesh->sptps, mesh, true, false, key, hiskey, meshlink_invitation_label, sizeof(meshlink_invitation_label), invitation_send, invitation_receive)) { meshlink_errno = MESHLINK_EINTERNAL; pthread_mutex_unlock(&(mesh->mesh_mutex)); return false; @@ -1865,7 +1865,7 @@ bool meshlink_join(meshlink_handle_t *mesh, const char *invitation) { int len; - while((len = recv(mesh->sock, mesh->line, sizeof mesh->line, 0))) { + while((len = recv(mesh->sock, mesh->line, sizeof(mesh)->line, 0))) { if(len < 0) { if(errno == EINTR) continue; @@ -1913,7 +1913,7 @@ char *meshlink_export(meshlink_handle_t *mesh) { pthread_mutex_lock(&(mesh->mesh_mutex)); char filename[PATH_MAX]; - snprintf(filename, sizeof filename, "%s" SLASH "hosts" SLASH "%s", mesh->confbase, mesh->self->name); + snprintf(filename, sizeof(filename), "%s" SLASH "hosts" SLASH "%s", mesh->confbase, mesh->self->name); FILE *f = fopen(filename, "r"); if(!f) { logger(mesh, MESHLINK_DEBUG, "Could not open %s: %s\n", filename, strerror(errno)); @@ -1979,7 +1979,7 @@ bool meshlink_import(meshlink_handle_t *mesh, const char *data) { } char filename[PATH_MAX]; - snprintf(filename, sizeof filename, "%s" SLASH "hosts" SLASH "%s", mesh->confbase, name); + snprintf(filename, sizeof(filename), "%s" SLASH "hosts" SLASH "%s", mesh->confbase, name); if(!access(filename, F_OK)) { logger(mesh, MESHLINK_DEBUG, "File %s already exists, not importing\n", filename); meshlink_errno = MESHLINK_EEXIST; @@ -2120,7 +2120,7 @@ meshlink_edge_t **meshlink_get_all_edges_state(meshlink_handle_t *mesh, meshlink n++; // the first *nmemb members of result can be re-used if(n > *nmemb) - copy = xzalloc(sizeof *copy); + copy = xzalloc(sizeof(*copy)); else copy = *p; copy->from = (meshlink_node_t*)e->from; @@ -2168,7 +2168,7 @@ static void channel_accept(struct utcp_connection *utcp_connection, uint16_t por meshlink_handle_t *mesh = n->mesh; if(!mesh->channel_accept_cb) return; - meshlink_channel_t *channel = xzalloc(sizeof *channel); + meshlink_channel_t *channel = xzalloc(sizeof(*channel)); channel->node = n; channel->c = utcp_connection; if(mesh->channel_accept_cb(mesh, channel, port, NULL, 0)) @@ -2245,7 +2245,7 @@ meshlink_channel_t *meshlink_channel_open_ex(meshlink_handle_t *mesh, meshlink_n return NULL; } } - meshlink_channel_t *channel = xzalloc(sizeof *channel); + meshlink_channel_t *channel = xzalloc(sizeof(*channel)); channel->node = n; channel->receive_cb = cb; channel->c = utcp_connect_ex(n->utcp, port, channel_recv, channel, flags); @@ -2351,7 +2351,7 @@ end: static void __attribute__((constructor)) meshlink_init(void) { crypto_init(); unsigned int seed; - randomize(&seed, sizeof seed); + randomize(&seed, sizeof(seed)); srand(seed); } diff --git a/src/meshlink_queue.h b/src/meshlink_queue.h index 511d0b3c..a713f93f 100644 --- a/src/meshlink_queue.h +++ b/src/meshlink_queue.h @@ -37,7 +37,7 @@ typedef struct meshlink_queue_item { } meshlink_queue_item_t; static inline bool meshlink_queue_push(meshlink_queue_t *queue, void *data) { - meshlink_queue_item_t *item = malloc(sizeof *item); + meshlink_queue_item_t *item = malloc(sizeof(*item)); if(!item) return false; item->data = data; diff --git a/src/meta.c b/src/meta.c index 9420a49b..3ae0d697 100644 --- a/src/meta.c +++ b/src/meta.c @@ -111,7 +111,7 @@ bool receive_meta(meshlink_handle_t *mesh, connection_t *c) { int inlen; char inbuf[MAXBUFSIZE]; - inlen = recv(c->socket, inbuf, sizeof inbuf, 0); + inlen = recv(c->socket, inbuf, sizeof(inbuf), 0); if(inlen <= 0) { if(!inlen || !errno) { @@ -142,7 +142,7 @@ bool receive_meta(meshlink_handle_t *mesh, connection_t *c) { return true; } - if(c->inbuf.len >= sizeof inbuf) { + if(c->inbuf.len >= sizeof(inbuf)) { logger(mesh, MESHLINK_ERROR, "Input buffer full for %s (%s)", c->name, c->hostname); return false; } else diff --git a/src/net_packet.c b/src/net_packet.c index 03ecc945..e0b36431 100644 --- a/src/net_packet.c +++ b/src/net_packet.c @@ -238,7 +238,7 @@ static void receive_udppacket(meshlink_handle_t *mesh, node_t *n, vpn_packet_t * void receive_tcppacket(meshlink_handle_t *mesh, connection_t *c, const char *buffer, int len) { vpn_packet_t outpkt; - if(len > sizeof outpkt.data) + if(len > sizeof(outpkt).data) return; outpkt.len = len; @@ -555,7 +555,7 @@ void handle_incoming_vpn_data(event_loop_t *loop, void *data, int flags) { vpn_packet_t pkt; char *hostname; sockaddr_t from = {{0}}; - socklen_t fromlen = sizeof from; + socklen_t fromlen = sizeof(from); node_t *n; int len; diff --git a/src/net_socket.c b/src/net_socket.c index 251572b9..ef34d0a8 100644 --- a/src/net_socket.c +++ b/src/net_socket.c @@ -61,12 +61,12 @@ static void configure_tcp(connection_t *c) { #if defined(SOL_TCP) && defined(TCP_NODELAY) int nodelay = 1; - setsockopt(c->socket, SOL_TCP, TCP_NODELAY, (void *)&nodelay, sizeof nodelay); + setsockopt(c->socket, SOL_TCP, TCP_NODELAY, (void *)&nodelay, sizeof(nodelay)); #endif #if defined(SOL_IP) && defined(IP_TOS) && defined(IPTOS_LOWDELAY) int lowdelay = IPTOS_LOWDELAY; - setsockopt(c->socket, SOL_IP, IP_TOS, (void *)&lowdelay, sizeof lowdelay); + setsockopt(c->socket, SOL_IP, IP_TOS, (void *)&lowdelay, sizeof(lowdelay)); #endif } @@ -117,11 +117,11 @@ int setup_listen_socket(const sockaddr_t *sa) { /* Optimize TCP settings */ option = 1; - setsockopt(nfd, SOL_SOCKET, SO_REUSEADDR, (void *)&option, sizeof option); + setsockopt(nfd, SOL_SOCKET, SO_REUSEADDR, (void *)&option, sizeof(option)); #if defined(SOL_IPV6) && defined(IPV6_V6ONLY) if(sa->sa.sa_family == AF_INET6) - setsockopt(nfd, SOL_IPV6, IPV6_V6ONLY, (void *)&option, sizeof option); + setsockopt(nfd, SOL_IPV6, IPV6_V6ONLY, (void *)&option, sizeof(option)); #endif if(bind(nfd, &sa->sa, SALEN(sa->sa))) { @@ -180,12 +180,12 @@ int setup_vpn_in_socket(meshlink_handle_t *mesh, const sockaddr_t *sa) { #endif option = 1; - setsockopt(nfd, SOL_SOCKET, SO_REUSEADDR, (void *)&option, sizeof option); - setsockopt(nfd, SOL_SOCKET, SO_BROADCAST, (void *)&option, sizeof option); + setsockopt(nfd, SOL_SOCKET, SO_REUSEADDR, (void *)&option, sizeof(option)); + setsockopt(nfd, SOL_SOCKET, SO_BROADCAST, (void *)&option, sizeof(option)); #if defined(IPPROTO_IPV6) && defined(IPV6_V6ONLY) if(sa->sa.sa_family == AF_INET6) - setsockopt(nfd, IPPROTO_IPV6, IPV6_V6ONLY, (void *)&option, sizeof option); + setsockopt(nfd, IPPROTO_IPV6, IPV6_V6ONLY, (void *)&option, sizeof(option)); #endif #if defined(IP_DONTFRAG) && !defined(IP_DONTFRAGMENT) @@ -336,7 +336,7 @@ static void handle_meta_io(event_loop_t *loop, void *data, int flags) { c->status.connecting = false; int result; - socklen_t len = sizeof result; + socklen_t len = sizeof(result); getsockopt(c->socket, SOL_SOCKET, SO_ERROR, (void *)&result, &len); if(!result) @@ -373,7 +373,7 @@ static struct addrinfo *get_known_addresses(node_t *n) { continue; // Create a new struct addrinfo, and put it at the head of the list. - struct addrinfo *nai = xzalloc(sizeof *nai + SALEN(e->reverse->address.sa)); + struct addrinfo *nai = xzalloc(sizeof(*nai) + SALEN(e->reverse->address.sa)); nai->ai_next = ai; ai = nai; @@ -484,7 +484,7 @@ begin: #if defined(SOL_IPV6) && defined(IPV6_V6ONLY) int option = 1; if(c->address.sa.sa_family == AF_INET6) - setsockopt(c->socket, SOL_IPV6, IPV6_V6ONLY, (void *)&option, sizeof option); + setsockopt(c->socket, SOL_IPV6, IPV6_V6ONLY, (void *)&option, sizeof(option)); #endif bind_to_address(mesh, c); @@ -565,7 +565,7 @@ void handle_new_meta_connection(event_loop_t *loop, void *data, int flags) { connection_t *c; sockaddr_t sa; int fd; - socklen_t len = sizeof sa; + socklen_t len = sizeof(sa); fd = accept(l->tcp.fd, &sa.sa, &len); @@ -609,7 +609,7 @@ void handle_new_meta_connection(event_loop_t *loop, void *data, int flags) { } } - memcpy(&prev_sa, &sa, sizeof sa); + memcpy(&prev_sa, &sa, sizeof(sa)); // Check if we get many connections from different hosts @@ -709,7 +709,7 @@ void try_outgoing_connections(meshlink_handle_t *mesh) { } if(!found) { - outgoing_t *outgoing = xzalloc(sizeof *outgoing); + outgoing_t *outgoing = xzalloc(sizeof(*outgoing)); outgoing->mesh = mesh; outgoing->name = name; list_insert_tail(mesh->outgoings, outgoing); diff --git a/src/netutl.c b/src/netutl.c index 93e58a80..983ad914 100644 --- a/src/netutl.c +++ b/src/netutl.c @@ -87,7 +87,7 @@ void sockaddr2str(const sockaddr_t *sa, char **addrstr, char **portstr) { return; } - err = getnameinfo(&sa->sa, SALEN(sa->sa), address, sizeof address, port, sizeof port, NI_NUMERICHOST | NI_NUMERICSERV); + err = getnameinfo(&sa->sa, SALEN(sa->sa), address, sizeof(address), port, sizeof(port), NI_NUMERICHOST | NI_NUMERICSERV); if(err) { logger(NULL, MESHLINK_ERROR, "Error while translating addresses: %s", err == EAI_SYSTEM ? strerror(errno) : gai_strerror(err)); @@ -116,7 +116,7 @@ char *sockaddr2hostname(const sockaddr_t *sa) { return str; } - err = getnameinfo(&sa->sa, SALEN(sa->sa), address, sizeof address, port, sizeof port, + err = getnameinfo(&sa->sa, SALEN(sa->sa), address, sizeof(address), port, sizeof(port), hostnames ? 0 : (NI_NUMERICHOST | NI_NUMERICSERV)); if(err) logger(NULL, MESHLINK_ERROR, "Error while looking up hostname: %s", err == EAI_SYSTEM ? strerror(errno) : gai_strerror(err)); @@ -175,20 +175,20 @@ int sockaddrcmp(const sockaddr_t *a, const sockaddr_t *b) { return strcmp(a->unknown.port, b->unknown.port); case AF_INET: - result = memcmp(&a->in.sin_addr, &b->in.sin_addr, sizeof a->in.sin_addr); + result = memcmp(&a->in.sin_addr, &b->in.sin_addr, sizeof(a)->in.sin_addr); if(result) return result; - return memcmp(&a->in.sin_port, &b->in.sin_port, sizeof a->in.sin_port); + return memcmp(&a->in.sin_port, &b->in.sin_port, sizeof(a)->in.sin_port); case AF_INET6: - result = memcmp(&a->in6.sin6_addr, &b->in6.sin6_addr, sizeof a->in6.sin6_addr); + result = memcmp(&a->in6.sin6_addr, &b->in6.sin6_addr, sizeof(a)->in6.sin6_addr); if(result) return result; - return memcmp(&a->in6.sin6_port, &b->in6.sin6_port, sizeof a->in6.sin6_port); + return memcmp(&a->in6.sin6_port, &b->in6.sin6_port, sizeof(a)->in6.sin6_port); default: logger(NULL, MESHLINK_ERROR, "sockaddrcmp() was called with unknown address family %d, exitting!", diff --git a/src/node.c b/src/node.c index bbb574f2..523c201f 100644 --- a/src/node.c +++ b/src/node.c @@ -48,7 +48,7 @@ void exit_nodes(meshlink_handle_t *mesh) { } node_t *new_node(void) { - node_t *n = xzalloc(sizeof *n); + node_t *n = xzalloc(sizeof(*n)); n->edge_tree = new_edge_tree(); n->mtu = MTU; diff --git a/src/prf.c b/src/prf.c index 354eb42d..526c8706 100644 --- a/src/prf.c +++ b/src/prf.c @@ -60,7 +60,7 @@ static bool hmac_sha512(const char *key, size_t keylen, const char *msg, size_t // opad memxor(tmp, 0x36 ^ 0x5c, mdlen); - if(sha512(tmp, sizeof tmp, out) != 0) + if(sha512(tmp, sizeof(tmp), out) != 0) return false; return true; @@ -83,17 +83,17 @@ bool prf(const char *secret, size_t secretlen, char *seed, size_t seedlen, char while(outlen > 0) { /* Inner HMAC */ - if(!hmac_sha512(data, sizeof data, secret, secretlen, data)) + if(!hmac_sha512(data, sizeof(data), secret, secretlen, data)) return false; /* Outer HMAC */ if(outlen >= mdlen) { - if(!hmac_sha512(data, sizeof data, secret, secretlen, out)) + if(!hmac_sha512(data, sizeof(data), secret, secretlen, out)) return false; out += mdlen; outlen -= mdlen; } else { - if(!hmac_sha512(data, sizeof data, secret, secretlen, hash)) + if(!hmac_sha512(data, sizeof(data), secret, secretlen, hash)) return false; memcpy(out, hash, outlen); out += outlen; diff --git a/src/protocol.c b/src/protocol.c index b51c66b7..61a01b57 100644 --- a/src/protocol.c +++ b/src/protocol.c @@ -101,7 +101,7 @@ void forward_request(meshlink_handle_t *mesh, connection_t *from, const char *re char tmp[len + 1]; memcpy(tmp, request, len); tmp[len] = '\n'; - broadcast_meta(mesh, from, tmp, sizeof tmp); + broadcast_meta(mesh, from, tmp, sizeof(tmp)); } bool receive_request(meshlink_handle_t *mesh, connection_t *c, const char *request) { @@ -187,7 +187,7 @@ bool seen_request(meshlink_handle_t *mesh, const char *request) { logger(mesh, MESHLINK_DEBUG, "Already seen request"); return true; } else { - new = xmalloc(sizeof *new); + new = xmalloc(sizeof(*new)); new->request = xstrdup(request); new->firstseen = mesh->loop.now.tv_sec; splay_insert(mesh->past_request_tree, new); diff --git a/src/protocol_auth.c b/src/protocol_auth.c index c748ead7..829e9b31 100644 --- a/src/protocol_auth.c +++ b/src/protocol_auth.c @@ -65,9 +65,9 @@ static bool send_proxyrequest(meshlink_handle_t *mesh, connection_t *c) { memcpy(s4req + 4, &c->address.in.sin_addr, 4); if(mesh->proxyuser) memcpy(s4req + 8, mesh->proxyuser, strlen(mesh->proxyuser)); - s4req[sizeof s4req - 1] = 0; + s4req[sizeof(s4req) - 1] = 0; c->tcplen = 8; - return send_meta(mesh, c, s4req, sizeof s4req); + return send_meta(mesh, c, s4req, sizeof(s4req)); } case PROXY_SOCKS5: { int len = 3 + 6 + (c->address.sa.sa_family == AF_INET ? 4 : 16); @@ -113,7 +113,7 @@ static bool send_proxyrequest(meshlink_handle_t *mesh, connection_t *c) { } if(i > len) abort(); - return send_meta(mesh, c, s5req, sizeof s5req); + return send_meta(mesh, c, s5req, sizeof(s5req)); } case PROXY_SOCKS4A: logger(mesh, MESHLINK_ERROR, "Proxy type not implemented yet"); @@ -145,7 +145,7 @@ static bool finalize_invitation(meshlink_handle_t *mesh, connection_t *c, const // Create a new host config file char filename[PATH_MAX]; - snprintf(filename, sizeof filename, "%s" SLASH "hosts" SLASH "%s", mesh->confbase, c->name); + snprintf(filename, sizeof(filename), "%s" SLASH "hosts" SLASH "%s", mesh->confbase, c->name); if(!access(filename, F_OK)) { logger(mesh, MESHLINK_ERROR, "Host config file for %s (%s) already exists!\n", c->name, c->hostname); return false; @@ -190,14 +190,14 @@ static bool receive_invitation_sptps(void *handle, uint8_t type, const void *dat char hashbuf[18 + strlen(fingerprint)]; char cookie[25]; memcpy(hashbuf, data, 18); - memcpy(hashbuf + 18, fingerprint, sizeof hashbuf - 18); - sha512(hashbuf, sizeof hashbuf, hash); + memcpy(hashbuf + 18, fingerprint, sizeof(hashbuf) - 18); + sha512(hashbuf, sizeof(hashbuf), hash); b64encode_urlsafe(hash, cookie, 18); free(fingerprint); char filename[PATH_MAX], usedname[PATH_MAX]; - snprintf(filename, sizeof filename, "%s" SLASH "invitations" SLASH "%s", mesh->confbase, cookie); - snprintf(usedname, sizeof usedname, "%s" SLASH "invitations" SLASH "%s.used", mesh->confbase, cookie); + snprintf(filename, sizeof(filename), "%s" SLASH "invitations" SLASH "%s", mesh->confbase, cookie); + snprintf(usedname, sizeof(usedname), "%s" SLASH "invitations" SLASH "%s.used", mesh->confbase, cookie); // Atomically rename the invitation file if(rename(filename, usedname)) { @@ -217,7 +217,7 @@ static bool receive_invitation_sptps(void *handle, uint8_t type, const void *dat // Read the new node's Name from the file char buf[1024]; - fgets(buf, sizeof buf, f); + fgets(buf, sizeof(buf), f); if(*buf) buf[strlen(buf) - 1] = 0; @@ -242,7 +242,7 @@ static bool receive_invitation_sptps(void *handle, uint8_t type, const void *dat // Send the node the contents of the invitation file rewind(f); size_t result; - while((result = fread(buf, 1, sizeof buf, f))) + while((result = fread(buf, 1, sizeof(buf), f))) sptps_send_record(&c->sptps, 0, buf, result); sptps_send_record(&c->sptps, 1, buf, 0); fclose(f); @@ -288,7 +288,7 @@ bool id_h(meshlink_handle_t *mesh, connection_t *c, const char *request) { c->protocol_minor = 2; c->allow_request = 1; - return sptps_start(&c->sptps, c, false, false, mesh->invitation_key, c->ecdsa, meshlink_invitation_label, sizeof meshlink_invitation_label, send_meta_sptps, receive_invitation_sptps); + return sptps_start(&c->sptps, c, false, false, mesh->invitation_key, c->ecdsa, meshlink_invitation_label, sizeof(meshlink_invitation_label), send_meta_sptps, receive_invitation_sptps); } /* Check if identity is a valid name */ @@ -353,14 +353,14 @@ bool id_h(meshlink_handle_t *mesh, connection_t *c, const char *request) { } c->allow_request = ACK; - char label[sizeof meshlink_tcp_label + strlen(mesh->self->name) + strlen(c->name) + 2]; + char label[sizeof(meshlink_tcp_label) + strlen(mesh->self->name) + strlen(c->name) + 2]; if(c->outgoing) - snprintf(label, sizeof label, "%s %s %s", meshlink_tcp_label, mesh->self->name, c->name); + snprintf(label, sizeof(label), "%s %s %s", meshlink_tcp_label, mesh->self->name, c->name); else - snprintf(label, sizeof label, "%s %s %s", meshlink_tcp_label, c->name, mesh->self->name); + snprintf(label, sizeof(label), "%s %s %s", meshlink_tcp_label, c->name, mesh->self->name); - return sptps_start(&c->sptps, c, c->outgoing, false, mesh->self->connection->ecdsa, c->ecdsa, label, sizeof label - 1, send_meta_sptps, receive_meta_sptps); + return sptps_start(&c->sptps, c, c->outgoing, false, mesh->self->connection->ecdsa, c->ecdsa, label, sizeof(label) - 1, send_meta_sptps, receive_meta_sptps); } bool send_ack(meshlink_handle_t *mesh, connection_t *c) { diff --git a/src/protocol_key.c b/src/protocol_key.c index ccaf9e28..6d1e8ffe 100644 --- a/src/protocol_key.c +++ b/src/protocol_key.c @@ -88,14 +88,14 @@ bool send_req_key(meshlink_handle_t *mesh, node_t *to) { if(to->sptps.label) logger(mesh, MESHLINK_DEBUG, "send_req_key(%s) called while sptps->label != NULL!", to->name); - char label[sizeof meshlink_udp_label + strlen(mesh->self->name) + strlen(to->name) + 2]; - snprintf(label, sizeof label, "%s %s %s", meshlink_udp_label, mesh->self->name, to->name); + char label[sizeof(meshlink_udp_label) + strlen(mesh->self->name) + strlen(to->name) + 2]; + snprintf(label, sizeof(label), "%s %s %s", meshlink_udp_label, mesh->self->name, to->name); sptps_stop(&to->sptps); to->status.validkey = false; to->status.waitingforkey = true; to->last_req_key = mesh->loop.now.tv_sec; to->incompression = mesh->self->incompression; - return sptps_start(&to->sptps, to, true, true, mesh->self->connection->ecdsa, to->ecdsa, label, sizeof label - 1, send_initial_sptps_data, receive_sptps_record); + return sptps_start(&to->sptps, to, true, true, mesh->self->connection->ecdsa, to->ecdsa, label, sizeof(label) - 1, send_initial_sptps_data, receive_sptps_record); } /* REQ_KEY is overloaded to allow arbitrary requests to be routed between two nodes. */ @@ -149,13 +149,13 @@ static bool req_key_ext_h(meshlink_handle_t *mesh, connection_t *c, const char * return true; } - char label[sizeof meshlink_udp_label + strlen(from->name) + strlen(mesh->self->name) + 2]; - snprintf(label, sizeof label, "%s %s %s", meshlink_udp_label, from->name, mesh->self->name); + char label[sizeof(meshlink_udp_label) + strlen(from->name) + strlen(mesh->self->name) + 2]; + snprintf(label, sizeof(label), "%s %s %s", meshlink_udp_label, from->name, mesh->self->name); sptps_stop(&from->sptps); from->status.validkey = false; from->status.waitingforkey = true; from->last_req_key = mesh->loop.now.tv_sec; - sptps_start(&from->sptps, from, false, true, mesh->self->connection->ecdsa, from->ecdsa, label, sizeof label - 1, send_sptps_data, receive_sptps_record); + sptps_start(&from->sptps, from, false, true, mesh->self->connection->ecdsa, from->ecdsa, label, sizeof(label) - 1, send_sptps_data, receive_sptps_record); sptps_receive_data(&from->sptps, buf, len); return true; } diff --git a/src/route.c b/src/route.c index 941f3aa0..9ab0688d 100644 --- a/src/route.c +++ b/src/route.c @@ -45,7 +45,7 @@ void route(meshlink_handle_t *mesh, node_t *source, vpn_packet_t *packet) { logger(mesh, MESHLINK_DEBUG, "Routing packet from \"%s\" to \"%s\"\n", hdr->source, hdr->destination); //Check Lenght - if(!checklength(source, packet, sizeof *hdr)) + if(!checklength(source, packet, sizeof(*hdr))) return; if(owner == NULL) { @@ -56,8 +56,8 @@ void route(meshlink_handle_t *mesh, node_t *source, vpn_packet_t *packet) { } if(owner == mesh->self) { - const void *payload = packet->data + sizeof *hdr; - size_t len = packet->len - sizeof *hdr; + const void *payload = packet->data + sizeof(*hdr); + size_t len = packet->len - sizeof(*hdr); char hex[len*2 + 1]; if(mesh->log_level >= MESHLINK_DEBUG) diff --git a/src/sptps.c b/src/sptps.c index 258d8b88..50958ec6 100644 --- a/src/sptps.c +++ b/src/sptps.c @@ -177,11 +177,11 @@ static bool send_sig(sptps_t *s) { memcpy(msg + 1 + 2 * (33 + keylen), s->label, s->labellen); // Sign the result. - if(!ecdsa_sign(s->mykey, msg, sizeof msg, sig)) + if(!ecdsa_sign(s->mykey, msg, sizeof(msg), sig)) return error(s, EINVAL, "Failed to sign SIG record"); // Send the SIG exchange record. - return send_record_priv(s, SPTPS_HANDSHAKE, sig, sizeof sig); + return send_record_priv(s, SPTPS_HANDSHAKE, sig, sizeof(sig)); } // Generate key material from the shared secret created from the ECDHE key exchange. @@ -283,7 +283,7 @@ static bool receive_sig(sptps_t *s, const char *data, uint16_t len) { memcpy(msg + 1 + 2 * (33 + keylen), s->label, s->labellen); // Verify signature. - if(!ecdsa_verify(s->hiskey, msg, sizeof msg, data)) + if(!ecdsa_verify(s->hiskey, msg, sizeof(msg), data)) return error(s, EIO, "Failed to verify SIG record"); // Compute shared secret. @@ -293,7 +293,7 @@ static bool receive_sig(sptps_t *s, const char *data, uint16_t len) { s->ecdh = NULL; // Generate key material from shared secret. - if(!generate_key_material(s, shared, sizeof shared)) + if(!generate_key_material(s, shared, sizeof(shared))) return false; free(s->mykex); @@ -567,7 +567,7 @@ bool sptps_start(sptps_t *s, void *handle, bool initiator, bool datagram, ecdsa_ return error(s, EINVAL, "Invalid argument to sptps_start()"); // Initialise struct sptps - memset(s, 0, sizeof *s); + memset(s, 0, sizeof(*s)); s->handle = handle; s->initiator = initiator; @@ -616,6 +616,6 @@ bool sptps_stop(sptps_t *s) { free(s->key); free(s->label); free(s->late); - memset(s, 0, sizeof *s); + memset(s, 0, sizeof(*s)); return true; } diff --git a/src/sptps_speed.c b/src/sptps_speed.c index 673efd25..e91fba62 100644 --- a/src/sptps_speed.c +++ b/src/sptps_speed.c @@ -53,7 +53,7 @@ static bool receive_record(void *handle, uint8_t type, const void *data, uint16_ static void receive_data(sptps_t *sptps) { char buf[4096]; int fd = *(int *)sptps->handle; - size_t len = recv(fd, buf, sizeof buf, 0); + size_t len = recv(fd, buf, sizeof(buf), 0); if(!sptps_receive_data(sptps, buf, len)) abort(); } @@ -88,9 +88,9 @@ int main(int argc, char *argv[]) { crypto_init(); - randomize(buf1, sizeof buf1); - randomize(buf2, sizeof buf2); - randomize(buf3, sizeof buf3); + randomize(buf1, sizeof(buf1)); + randomize(buf2, sizeof(buf2)); + randomize(buf3, sizeof(buf3)); // Key generation diff --git a/src/sptps_test.c b/src/sptps_test.c index b38711de..a456bb86 100644 --- a/src/sptps_test.c +++ b/src/sptps_test.c @@ -210,7 +210,7 @@ int main(int argc, char *argv[]) { #endif struct addrinfo *ai, hint; - memset(&hint, 0, sizeof hint); + memset(&hint, 0, sizeof(hint)); hint.ai_family = AF_UNSPEC; hint.ai_socktype = datagram ? SOCK_DGRAM : SOCK_STREAM; @@ -229,7 +229,7 @@ int main(int argc, char *argv[]) { } int one = 1; - setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void *)&one, sizeof one); + setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void *)&one, sizeof(one)); if(initiator) { if(connect(sock, ai->ai_addr, ai->ai_addrlen)) { @@ -260,9 +260,9 @@ int main(int argc, char *argv[]) { char buf[65536]; struct sockaddr addr; - socklen_t addrlen = sizeof addr; + socklen_t addrlen = sizeof(addr); - if(recvfrom(sock, buf, sizeof buf, MSG_PEEK, &addr, &addrlen) <= 0) { + if(recvfrom(sock, buf, sizeof(buf), MSG_PEEK, &addr, &addrlen) <= 0) { fprintf(stderr, "Could not read from socket: %s\n", strerror(errno)); return 1; } @@ -312,7 +312,7 @@ int main(int argc, char *argv[]) { return 1; if(FD_ISSET(in, &fds)) { - ssize_t len = read(in, buf, sizeof buf); + ssize_t len = read(in, buf, sizeof(buf)); if(len < 0) { fprintf(stderr, "Could not read from stdin: %s\n", strerror(errno)); return 1; @@ -331,12 +331,12 @@ int main(int argc, char *argv[]) { sptps_force_kex(&s); if(len > 1) sptps_send_record(&s, 0, buf, len); - } else if(!sptps_send_record(&s, buf[0] == '!' ? 1 : 0, buf, (len == 1 && buf[0] == '\n') ? 0 : buf[0] == '*' ? sizeof buf : len)) + } else if(!sptps_send_record(&s, buf[0] == '!' ? 1 : 0, buf, (len == 1 && buf[0] == '\n') ? 0 : buf[0] == '*' ? sizeof(buf) : len)) return 1; } if(FD_ISSET(sock, &fds)) { - ssize_t len = recv(sock, buf, sizeof buf, 0); + ssize_t len = recv(sock, buf, sizeof(buf), 0); if(len < 0) { fprintf(stderr, "Could not read from socket: %s\n", strerror(errno)); return 1; diff --git a/src/utils.c b/src/utils.c index 71fe3dea..9ec22524 100644 --- a/src/utils.c +++ b/src/utils.c @@ -181,8 +181,8 @@ const char *winerror(int err) { unsigned int bitfield_to_int(const void *bitfield, size_t size) { unsigned int value = 0; - if(size > sizeof value) - size = sizeof value; + if(size > sizeof(value)) + size = sizeof(value); memcpy(&value, bitfield, size); return value; } diff --git a/src/xalloc.h b/src/xalloc.h index f6b895a5..e521d64a 100644 --- a/src/xalloc.h +++ b/src/xalloc.h @@ -54,7 +54,7 @@ static inline char *xstrdup(const char *s) { static inline int xvasprintf(char **strp, const char *fmt, va_list ap) { #ifdef HAVE_MINGW char buf[1024]; - int result = vsnprintf(buf, sizeof buf, fmt, ap); + int result = vsnprintf(buf, sizeof(buf), fmt, ap); if(result < 0) abort(); *strp = xstrdup(buf); diff --git a/test/channels-fork.c b/test/channels-fork.c index a95efbe2..673760e2 100644 --- a/test/channels-fork.c +++ b/test/channels-fork.c @@ -69,11 +69,11 @@ int main1(int rfd, int wfd) { } size_t len = strlen(data); - write(wfd, &len, sizeof len); + write(wfd, &len, sizeof(len)); write(wfd, data, len); free(data); - read(rfd, &len, sizeof len); + read(rfd, &len, sizeof(len)); char indata[len + 1]; read(rfd, indata, len); indata[len] = 0; @@ -153,11 +153,11 @@ int main2(int rfd, int wfd) { } size_t len = strlen(data); - if(write(wfd, &len, sizeof len) <= 0) abort(); + if(write(wfd, &len, sizeof(len)) <= 0) abort(); if(write(wfd, data, len) <= 0) abort(); free(data); - read(rfd, &len, sizeof len); + read(rfd, &len, sizeof(len)); char indata[len + 1]; read(rfd, indata, len); indata[len] = 0; diff --git a/test/sign-verify.c b/test/sign-verify.c index 028d8021..bd05c112 100644 --- a/test/sign-verify.c +++ b/test/sign-verify.c @@ -49,9 +49,9 @@ int main(int argc, char *argv[]) { static const char testdata2[] = "Test data 2."; char sig[MESHLINK_SIGLEN * 2]; - size_t siglen = sizeof sig * 2; + size_t siglen = sizeof(sig) * 2; - if(!meshlink_sign(mesh1, testdata1, sizeof testdata1, sig, &siglen)) { + if(!meshlink_sign(mesh1, testdata1, sizeof(testdata1), sig, &siglen)) { fprintf(stderr, "Signing failed\n"); return 1; } @@ -72,26 +72,26 @@ int main(int argc, char *argv[]) { return 1; } - if(!meshlink_verify(mesh2, foo, testdata1, sizeof testdata1, sig, siglen)) { + if(!meshlink_verify(mesh2, foo, testdata1, sizeof(testdata1), sig, siglen)) { fprintf(stderr, "False negative verification\n"); return 1; } // Check that bad signatures are revoked - if(meshlink_verify(mesh2, foo, testdata1, sizeof testdata1, sig, siglen / 2)) { + if(meshlink_verify(mesh2, foo, testdata1, sizeof(testdata1), sig, siglen / 2)) { fprintf(stderr, "False positive verification with half sized signature\n"); return 1; } - if(meshlink_verify(mesh2, foo, testdata1, sizeof testdata1, sig, siglen * 2)) { + if(meshlink_verify(mesh2, foo, testdata1, sizeof(testdata1), sig, siglen * 2)) { fprintf(stderr, "False positive verification with double sized signature\n"); return 1; } - if(meshlink_verify(mesh2, foo, testdata2, sizeof testdata2, sig, siglen)) { + if(meshlink_verify(mesh2, foo, testdata2, sizeof(testdata2), sig, siglen)) { fprintf(stderr, "False positive verification with wrong data\n"); return 1; } - if(meshlink_verify(mesh2, bar, testdata1, sizeof testdata1, sig, siglen)) { + if(meshlink_verify(mesh2, bar, testdata1, sizeof(testdata1), sig, siglen)) { fprintf(stderr, "False positive verification with wrong signer\n"); return 1; } -- 2.39.5