Fix memory leaks triggered by integration tests.
[tinc] / src / invitation.c
index 2976968..d615976 100644 (file)
@@ -111,6 +111,9 @@ char *get_my_hostname() {
                scan_for_hostname(tinc_conf, &hostname, &port);
        }
 
+       free(name);
+       name = NULL;
+
        if(hostname) {
                goto done;
        }
@@ -407,6 +410,7 @@ int cmd_invite(int argc, char *argv[]) {
 
                if(!f) {
                        fprintf(stderr, "Could not write %s: %s\n", filename, strerror(errno));
+                       free(key);
                        return 1;
                }
 
@@ -415,6 +419,7 @@ int cmd_invite(int argc, char *argv[]) {
                if(!ecdsa_write_pem_private_key(key, f)) {
                        fprintf(stderr, "Could not write ECDSA private key\n");
                        fclose(f);
+                       free(key);
                        return 1;
                }
 
@@ -444,6 +449,8 @@ int cmd_invite(int argc, char *argv[]) {
        sha512(fingerprint, strlen(fingerprint), hash);
        b64encode_urlsafe(hash, hash, 18);
 
+       free(key);
+
        // Create a random cookie for this invitation.
        char cookie[25];
        randomize(cookie, 18);
@@ -456,6 +463,8 @@ int cmd_invite(int argc, char *argv[]) {
        sha512(buf, sizeof(buf), cookiehash);
        b64encode_urlsafe(cookiehash, cookiehash, 18);
 
+       free(fingerprint);
+
        b64encode_urlsafe(cookie, cookie, 18);
 
        // Create a file containing the details of the invitation.
@@ -1054,7 +1063,13 @@ ask_netname:
                                }
                        }
                } else {
-                       fprintf(stderr, "A tinc-up script was generated, but has been left disabled.\n");
+                       if(force) {
+                               rename(filename, filename2);
+                               chmod(filename2, 0755);
+                               fprintf(stderr, "tinc-up enabled.\n");
+                       } else {
+                               fprintf(stderr, "A tinc-up script was generated, but has been left disabled.\n");
+                       }
                }
        } else {
                // A placeholder was generated.
@@ -1076,7 +1091,7 @@ static bool invitation_send(void *handle, uint8_t type, const void *vdata, size_
        while(len) {
                int result = send(sock, data, len, 0);
 
-               if(result == -1 && errno == EINTR) {
+               if(result == -1 && sockwouldblock(sockerrno)) {
                        continue;
                } else if(result <= 0) {
                        return false;
@@ -1231,6 +1246,7 @@ int cmd_join(int argc, char *argv[]) {
        struct addrinfo *ai = str2addrinfo(address, port, SOCK_STREAM);
 
        if(!ai) {
+               free(b64key);
                return 1;
        }
 
@@ -1244,6 +1260,7 @@ next:
 
                if(!aip) {
                        freeaddrinfo(ai);
+                       free(b64key);
                        return 1;
                }
        }
@@ -1290,6 +1307,11 @@ next:
        }
 
        freeaddrinfo(ai);
+       ai = NULL;
+       aip = NULL;
+
+       free(b64key);
+       b64key = NULL;
 
        // Check if the hash of the key he gave us matches the hash in the URL.
        char *fingerprint = line + 2;
@@ -1324,11 +1346,21 @@ next:
 
        while((len = recv(sock, line, sizeof(line), 0))) {
                if(len < 0) {
-                       if(errno == EINTR) {
+                       if(sockwouldblock(sockerrno)) {
                                continue;
                        }
 
-                       fprintf(stderr, "Error reading data from %s port %s: %s\n", address, port, strerror(errno));
+#if HAVE_MINGW
+
+                       // If socket has been shut down, recv() on Windows returns -1 and sets sockerrno
+                       // to WSAESHUTDOWN, while on UNIX-like operating systems recv() returns 0, so we
+                       // have to do an explicit check here.
+                       if(sockshutdown(sockerrno)) {
+                               break;
+                       }
+
+#endif
+                       fprintf(stderr, "Error reading data from %s port %s: %s\n", address, port, sockstrerror(sockerrno));
                        return 1;
                }