]> the.earth.li Git - onak.git/blobdiff - wotsap.c
Fix potential memory leak in wotsap tool
[onak.git] / wotsap.c
index 3aefc42012a4059324bfbad3ad7e080fc548ea81..7c71535e762a19d40a9aa0ac72ff1dd2b581a037 100644 (file)
--- a/wotsap.c
+++ b/wotsap.c
 #include <string.h>
 #include <arpa/inet.h>
 
+#include "build-config.h"
+
 #include "hash.h"
 #include "log.h"
 #include "onak-conf.h"
 #include "stats.h"
-#include "version.h"
 
 static struct ll *sortkeyll(struct ll *keys)
 {
@@ -83,12 +84,16 @@ static void wotsap(struct onak_dbctx *dbctx, uint64_t keyid, char *dir)
 
        /* Length of dir + "/" + "signatures" + NUL */
        tmppath = malloc(strlen(dir) + 12);
+       if (tmppath == NULL) {
+               fprintf(stderr, "Couldn't allocate memory for directory\n");
+               goto err;
+       }
 
        sprintf(tmppath, "%s/WOTVERSION", dir);
        file = fopen(tmppath, "w");
        if (file == NULL) {
                fprintf(stderr, "Couldn't open %s\n", tmppath);
-               return;
+               goto err;
        }
        fprintf(file, "0.2\n");
        fclose(file);
@@ -97,7 +102,7 @@ static void wotsap(struct onak_dbctx *dbctx, uint64_t keyid, char *dir)
        file = fopen(tmppath, "w");
        if (file == NULL) {
                fprintf(stderr, "Couldn't open %s\n", tmppath);
-               return;
+               goto err;
        }
        fprintf(file, "This is a Web of Trust archive.\n");
        fprintf(file, "The file format is documented at:\n");
@@ -109,21 +114,20 @@ static void wotsap(struct onak_dbctx *dbctx, uint64_t keyid, char *dir)
        names = fopen(tmppath, "w");
        if (names == NULL) {
                fprintf(stderr, "Couldn't open %s\n", tmppath);
-               return;
+               goto err;
        }
        sprintf(tmppath, "%s/keys", dir);
        keys = fopen(tmppath, "wb");
        if (keys == NULL) {
                fprintf(stderr, "Couldn't open %s\n", tmppath);
-               return;
+               goto err;
        }
        sprintf(tmppath, "%s/signatures", dir);
        sigs = fopen(tmppath, "wb");
        if (sigs == NULL) {
                fprintf(stderr, "Couldn't open %s\n", tmppath);
-               return;
+               goto err;
        }
-       free(tmppath);
 
        dbctx->cached_getkeysigs(dbctx, keyid);
        curkey = findinhash(keyid);
@@ -179,6 +183,8 @@ static void wotsap(struct onak_dbctx *dbctx, uint64_t keyid, char *dir)
        fclose(sigs);
        fclose(keys);
        fclose(names);
+err:
+       free(tmppath);
 }
 
 int main(int argc, char *argv[])