Improve handling invitation read errors.
authorGuus Sliepen <guus@tinc-vpn.org>
Sat, 2 Apr 2022 13:39:08 +0000 (15:39 +0200)
committerGuus Sliepen <guus@tinc-vpn.org>
Sat, 2 Apr 2022 13:40:58 +0000 (15:40 +0200)
Fix a file descriptor leak when something goes wrong while reading an
invitation file. Also check that we read it in full before committing.

Found by cppcheck.

src/protocol_auth.c

index 19859b7..4d6c991 100644 (file)
@@ -296,6 +296,7 @@ static bool receive_invitation_sptps(void *handle, uint8_t type, const void *dat
 
        if(!fgets(buf, sizeof(buf), f)) {
                logger(DEBUG_ALWAYS, LOG_ERR, "Could not read invitation file %s\n", cookie);
+               fclose(f);
                return false;
        }
 
@@ -336,6 +337,12 @@ static bool receive_invitation_sptps(void *handle, uint8_t type, const void *dat
                sptps_send_record(&c->sptps, 0, buf, result);
        }
 
+       if(!feof(f)) {
+               logger(DEBUG_ALWAYS, LOG_ERR, "Could not read invitation file %s\n", cookie);
+               fclose(f);
+               return false;
+       }
+
        sptps_send_record(&c->sptps, 1, buf, 0);
        fclose(f);
        unlink(usedname);