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 415c23772dcb09f6238839f92d2640fe352fd05c..3102e4160378a5303a95d72868ddb90fd0d7c24f 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 399404e192f01ba639b5743d120a41405cda8262..fde862278597b92b1c07f4e5754b40e37091d625 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 d03246c51d3204ae7fca2a9fcb516db773bdb074..4cb1221b771cd8836c04b5b3e03282cabe4fc317 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 c9d8c67c98e3ed6a91ad8c48008ace6af1119f4d..00757c47a57b96d7c3f258acd92652be66e9cac1 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);