[hackers] [quark] Use compound literals and explicit initialization || Laslo Hunhold

From: <git_AT_suckless.org>
Date: Thu, 30 May 2019 23:18:01 +0200 (CEST)

commit 32223c96bdee8f94980d3a1877a643a4d59f897f
Author: Laslo Hunhold <dev_AT_frign.de>
AuthorDate: Thu May 30 23:15:47 2019 +0200
Commit: Laslo Hunhold <dev_AT_frign.de>
CommitDate: Thu May 30 23:15:47 2019 +0200

    Use compound literals and explicit initialization
    
    I didn't really like the use of a "yes"-variable for setsockopt().
    A better way is to use compound literals (part of C99).
    
    Another point are the structs. Instead of memsetting to zero we make
    use of the standard which guarantees that "unmentioned" fields
    are set to zero anyways. Just to note it here: The use of memset()
    also sets padding to zero, which is not guaranteed with the method
    of "unmentioned" fields.
    
    Signed-off-by: Laslo Hunhold <dev_AT_frign.de>

diff --git a/main.c b/main.c
index 9e7788f..d30e85d 100644
--- a/main.c
+++ b/main.c
_AT_@ -78,12 +78,11 @@ sigcleanup(int sig)
 static void
 handlesignals(void(*hdl)(int))
 {
- struct sigaction sa;
+ struct sigaction sa = {
+ .sa_handler = hdl,
+ };
 
- memset(&sa, 0, sizeof(sa));
         sigemptyset(&sa.sa_mask);
- sa.sa_handler = hdl;
-
         sigaction(SIGTERM, &sa, NULL);
         sigaction(SIGHUP, &sa, NULL);
         sigaction(SIGINT, &sa, NULL);
diff --git a/sock.c b/sock.c
index 9dc4661..7000738 100644
--- a/sock.c
+++ b/sock.c
_AT_@ -18,25 +18,25 @@
 int
 sock_get_ips(const char *host, const char* port)
 {
- struct addrinfo hints, *ai, *p;
- int ret, insock = 0, yes;
-
- memset(&hints, 0, sizeof(hints));
- hints.ai_flags = AI_NUMERICSERV;
- hints.ai_family = AF_UNSPEC;
- hints.ai_socktype = SOCK_STREAM;
+ struct addrinfo hints = {
+ .ai_flags = AI_NUMERICSERV,
+ .ai_family = AF_UNSPEC,
+ .ai_socktype = SOCK_STREAM,
+ };
+ struct addrinfo *ai, *p;
+ int ret, insock = 0;
 
         if ((ret = getaddrinfo(host, port, &hints, &ai))) {
                 die("getaddrinfo: %s", gai_strerror(ret));
         }
 
- for (yes = 1, p = ai; p; p = p->ai_next) {
+ for (p = ai; p; p = p->ai_next) {
                 if ((insock = socket(p->ai_family, p->ai_socktype,
                                      p->ai_protocol)) < 0) {
                         continue;
                 }
- if (setsockopt(insock, SOL_SOCKET, SO_REUSEADDR, &yes,
- sizeof(int)) < 0) {
+ if (setsockopt(insock, SOL_SOCKET, SO_REUSEADDR,
+ &(int){1}, sizeof(int)) < 0) {
                         die("setsockopt:");
                 }
                 if (bind(insock, p->ai_addr, p->ai_addrlen) < 0) {
_AT_@ -70,7 +70,9 @@ sock_rem_uds(const char *udsname)
 int
 sock_get_uds(const char *udsname, uid_t uid, gid_t gid)
 {
- struct sockaddr_un addr;
+ struct sockaddr_un addr = {
+ .sun_family = AF_UNIX,
+ };
         size_t udsnamelen;
         int insock, sockmode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH;
 
_AT_@ -78,9 +80,6 @@ sock_get_uds(const char *udsname, uid_t uid, gid_t gid)
                 die("socket:");
         }
 
- memset(&addr, 0, sizeof(addr));
- addr.sun_family = AF_UNIX;
-
         if ((udsnamelen = strlen(udsname)) > sizeof(addr.sun_path) - 1) {
                 die("UNIX-domain socket name truncated");
         }
Received on Thu May 30 2019 - 23:18:01 CEST

This archive was generated by hypermail 2.3.0 : Thu May 30 2019 - 23:24:22 CEST