]> err.no Git - yubikey-server-c/commitdiff
Error handling in get_shared_secret; free memory on failure
authorTollef Fog Heen <tfheen@err.no>
Mon, 12 Oct 2009 06:44:21 +0000 (08:44 +0200)
committerTollef Fog Heen <tfheen@err.no>
Mon, 12 Oct 2009 06:44:21 +0000 (08:44 +0200)
src/main.c

index 1c7e9cb8831faa79a3ef423d213882448f1453b1..6c5b996fa1390693dd8e7e0084479f30c7cee12e 100644 (file)
@@ -149,6 +149,7 @@ static int get_shared_secret(const char *id, char **shared_secret,
 {
        const char *paramValues[1];
        PGresult   *res;
+       int r = 0;
 
        /* Do query to grab shared secret, we need this later anyway */
        paramValues[0] = id;
@@ -163,21 +164,27 @@ static int get_shared_secret(const char *id, char **shared_secret,
 
        if (PQresultStatus(res) != PGRES_TUPLES_OK) {
                fprintf(stderr, "SELECT failed: %s", PQerrorMessage(db_conn));
-               PQclear(res);
-               return -1;
+               r = -1;
+               goto free_mem;
                /* XXX Return error object */
        }
        if (PQntuples(res) == 0) {
                /* XXX Better handling */
                fprintf(stderr, "No such secrets: %s\n", id);
-               PQclear(res);
-               return -1;
+               r = -1;
+               goto free_mem;
        }
        *shared_secret_len = PQgetlength(res, 0, 0);
        *shared_secret = malloc(*shared_secret_len);
+       if (*shared_secret == NULL) {
+               *shared_secret_len = 0;
+               goto free_mem;
+       }
        memcpy(*shared_secret, PQgetvalue(res, 0, 0), *shared_secret_len);
+
+free_mem:
        PQclear(res);
-       return 0;
+       return r;
 }
 
 static int split_otp(const char *otp, char **user, char **s_otp)