Fix warnings about missing return value checks.
authorGuus Sliepen <guus@tinc-vpn.org>
Sun, 7 Jun 2015 20:50:05 +0000 (22:50 +0200)
committerGuus Sliepen <guus@tinc-vpn.org>
Sun, 7 Jun 2015 20:50:05 +0000 (22:50 +0200)
In some harmless places, checks for the return value of ECDSA and RSA
key generation and verification was omitted. Add them to keep the
compiler happy and to warn end users in case something is wrong.

src/invitation.c
src/sptps_keypair.c
src/sptps_speed.c
src/tincctl.c

index 415c237..3102e41 100644 (file)
@@ -335,7 +335,11 @@ int cmd_invite(int argc, char *argv[]) {
                        return 1;
                }
                chmod(filename, 0600);
-               ecdsa_write_pem_private_key(key, f);
+               if(!ecdsa_write_pem_private_key(key, f)) {
+                       fprintf(stderr, "Could not write ECDSA private key\n");
+                       fclose(f);
+                       return 1;
+               }
                fclose(f);
 
                if(connect_tincd(false))
@@ -704,6 +708,8 @@ make_names:
 
        snprintf(filename, sizeof filename, "%s" SLASH "ed25519_key.priv", confbase);
        f = fopenmask(filename, "w", 0600);
+       if(!f)
+               return false;
 
        if(!ecdsa_write_pem_private_key(key, f)) {
                fprintf(stderr, "Error writing private key!\n");
@@ -725,10 +731,14 @@ make_names:
        snprintf(filename, sizeof filename, "%s" SLASH "rsa_key.priv", confbase);
        f = fopenmask(filename, "w", 0600);
 
-       rsa_write_pem_private_key(rsa, f);
+       if(!f || !rsa_write_pem_private_key(rsa, f)) {
+               fprintf(stderr, "Could not write private RSA key\n");
+       } else if(!rsa_write_pem_public_key(rsa, fh)) {
+               fprintf(stderr, "Could not write public RSA key\n");
+       }
+
        fclose(f);
 
-       rsa_write_pem_public_key(rsa, fh);
        fclose(fh);
 
        rsa_free(rsa);
index 399404e..fde8622 100644 (file)
@@ -88,7 +88,10 @@ int main(int argc, char *argv[]) {
        
        FILE *fp = fopen(argv[1], "w");
        if(fp) {
-               ecdsa_write_pem_private_key(key, fp);
+               if(!ecdsa_write_pem_private_key(key, fp)) {
+                       fprintf(stderr, "Could not write ECDSA private key\n");
+                       return 1;
+               }
                fclose(fp);
        } else {
                fprintf(stderr, "Could not open '%s' for writing: %s\n", argv[1], strerror(errno));
@@ -97,7 +100,8 @@ int main(int argc, char *argv[]) {
 
        fp = fopen(argv[2], "w");
        if(fp) {
-               ecdsa_write_pem_public_key(key, fp);
+               if(!ecdsa_write_pem_public_key(key, fp))
+                       fprintf(stderr, "Could not write ECDSA public key\n");
                fclose(fp);
        } else {
                fprintf(stderr, "Could not open '%s' for writing: %s\n", argv[2], strerror(errno));
index d03246c..4cb1221 100644 (file)
@@ -102,19 +102,26 @@ int main(int argc, char *argv[]) {
 
        fprintf(stderr, "Ed25519 sign for %lg seconds: ", duration);
        for(clock_start(); clock_countto(duration);)
-               ecdsa_sign(key1, buf1, 256, buf2);
+               if(!ecdsa_sign(key1, buf1, 256, buf2))
+                       return 1;
        fprintf(stderr, "%22.2lf op/s\n", rate);
 
        fprintf(stderr, "Ed25519 verify for %lg seconds: ", duration);
        for(clock_start(); clock_countto(duration);)
-               ecdsa_verify(key1, buf1, 256, buf2);
+               if(!ecdsa_verify(key1, buf1, 256, buf2)) {
+                       fprintf(stderr, "Signature verification failed\n");
+                       return 1;
+               }
        fprintf(stderr, "%20.2lf op/s\n", rate);
 
        ecdh1 = ecdh_generate_public(buf1);
        fprintf(stderr, "ECDH for %lg seconds: ", duration);
        for(clock_start(); clock_countto(duration);) {
                ecdh2 = ecdh_generate_public(buf2);
-               ecdh_compute_shared(ecdh2, buf1, buf3);
+               if(!ecdh2)
+                       return 1;
+               if(!ecdh_compute_shared(ecdh2, buf1, buf3))
+                       return 1;
        }
        fprintf(stderr, "%28.2lf op/s\n", rate);
        ecdh_free(ecdh1);
index c9d8c67..00757c4 100644 (file)
@@ -233,6 +233,12 @@ FILE *fopenmask(const char *filename, const char *mode, mode_t perms) {
        perms &= ~mask;
        umask(~perms);
        FILE *f = fopen(filename, mode);
+
+       if(!f) {
+               fprintf(stderr, "Could not open %s: %s\n", filename, strerror(errno));
+               return NULL;
+       }
+
 #ifdef HAVE_FCHMOD
        if((perms & 0444) && f)
                fchmod(fileno(f), perms);