Fix warnings from the Clang static analyzer.
[tinc] / src / invitation.c
index bcb79a4..c3072cf 100644 (file)
@@ -181,6 +181,7 @@ char *get_my_hostname() {
        if(!tty) {
                if(!hostname) {
                        fprintf(stderr, "Could not determine the external address or hostname. Please set Address manually.\n");
+                       free(port);
                        return NULL;
                }
 
@@ -199,6 +200,7 @@ again:
        if(!fgets(line, sizeof(line), stdin)) {
                fprintf(stderr, "Error while reading stdin: %s\n", strerror(errno));
                free(hostname);
+               free(port);
                return NULL;
        }
 
@@ -353,7 +355,11 @@ int cmd_invite(int argc, char *argv[]) {
 
                char invname[PATH_MAX];
                struct stat st;
-               snprintf(invname, sizeof(invname), "%s" SLASH "%s", filename, ent->d_name);
+
+               if(snprintf(invname, sizeof(invname), "%s" SLASH "%s", filename, ent->d_name) >= sizeof(invname)) {
+                       fprintf(stderr, "Filename too long: %s" SLASH "%s\n", filename, ent->d_name);
+                       continue;
+               }
 
                if(!stat(invname, &st)) {
                        if(deadline < st.st_mtime) {
@@ -414,8 +420,10 @@ int cmd_invite(int argc, char *argv[]) {
 
                fclose(f);
 
-               if(connect_tincd(false)) {
+               if(connect_tincd(true)) {
                        sendline(fd, "%d %d", CONTROL, REQ_RELOAD);
+               } else {
+                       fprintf(stderr, "Could not signal the tinc daemon. Please restart or reload it manually.\n");
                }
        } else {
                key = ecdsa_read_pem_private_key(f);
@@ -638,7 +646,7 @@ static char *grep(const char *data, const char *var) {
 }
 
 static bool finalize_join(void) {
-       char *name = xstrdup(get_value(data, "Name"));
+       const char *name = get_value(data, "Name");
 
        if(!name) {
                fprintf(stderr, "No Name found in invitation!\n");
@@ -769,13 +777,19 @@ make_names:
 
                l[len] = 0;
 
+               // Ignore lines with empty variable names
+               if(!*l) {
+                       continue;
+               }
+
                // Is it a Name?
-               if(!strcasecmp(l, "Name"))
+               if(!strcasecmp(l, "Name")) {
                        if(strcmp(value, name)) {
                                break;
                        } else {
                                continue;
-                       } else if(!strcasecmp(l, "NetName")) {
+                       }
+               } else if(!strcasecmp(l, "NetName")) {
                        continue;
                }
 
@@ -947,7 +961,11 @@ ask_netname:
                line[strlen(line) - 1] = 0;
 
                char newbase[PATH_MAX];
-               snprintf(newbase, sizeof(newbase), CONFDIR SLASH "tinc" SLASH "%s", line);
+
+               if(snprintf(newbase, sizeof(newbase), CONFDIR SLASH "tinc" SLASH "%s", line) >= sizeof(newbase)) {
+                       fprintf(stderr, "Filename too long: " CONFDIR SLASH "tinc" SLASH "%s\n", line);
+                       goto ask_netname;
+               }
 
                if(rename(confbase, newbase)) {
                        fprintf(stderr, "Error trying to rename %s to %s: %s\n", confbase, newbase, strerror(errno));
@@ -1059,7 +1077,7 @@ static bool invitation_receive(void *handle, uint8_t type, const void *msg, uint
                return finalize_join();
 
        case 2:
-               fprintf(stderr, "Invitation succesfully accepted.\n");
+               fprintf(stderr, "Invitation successfully accepted.\n");
                shutdown(sock, SHUT_RDWR);
                success = true;
                break;
@@ -1195,6 +1213,7 @@ next:
                aip = aip->ai_next;
 
                if(!aip) {
+                       freeaddrinfo(ai);
                        return 1;
                }
        }
@@ -1234,12 +1253,14 @@ next:
        char hisname[4096] = "";
        int code, hismajor, hisminor = 0;
 
-       if(!recvline(sock, line, sizeof(line)) || sscanf(line, "%d %4095s %d.%d", &code, hisname, &hismajor, &hisminor) < 3 || code != 0 || hismajor != PROT_MAJOR || !check_id(hisname) || !recvline(sock, line, sizeof line) || !rstrip(line) || sscanf(line, "%d ", &code) != 1 || code != ACK || strlen(line) < 3) {
+       if(!recvline(sock, line, sizeof(line)) || sscanf(line, "%d %4095s %d.%d", &code, hisname, &hismajor, &hisminor) < 3 || code != 0 || hismajor != PROT_MAJOR || !check_id(hisname) || !recvline(sock, line, sizeof(line)) || !rstrip(line) || sscanf(line, "%d ", &code) != 1 || code != ACK || strlen(line) < 3) {
                fprintf(stderr, "Cannot read greeting from peer\n");
                closesocket(sock);
                goto next;
        }
 
+       freeaddrinfo(ai);
+
        // Check if the hash of the key he gave us matches the hash in the URL.
        char *fingerprint = line + 2;
        char hishash[64];