Fix warnings from the Clang static analyzer.
[tinc] / src / invitation.c
index 4a262a8..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");
@@ -953,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));