]> err.no Git - yubikey-server-c/commitdiff
Better error handling in get_data_for_uid
authorTollef Fog Heen <tfheen@err.no>
Mon, 12 Oct 2009 06:52:44 +0000 (08:52 +0200)
committerTollef Fog Heen <tfheen@err.no>
Mon, 12 Oct 2009 06:52:44 +0000 (08:52 +0200)
src/main.c

index 59a498ecda22fc7ee14d5a2f800c32a05c274376..765be2a2a6d2901fbfce1454c5d0b6f914a45b56 100644 (file)
@@ -258,6 +258,7 @@ static int get_data_for_uid(char *uid, struct ykc_stats *stats)
        PGresult *res;
        const char *paramValues[1];
        char *tmp;
+       int r = 0;
 
        paramValues[0] = uid;
        res = PQexecParams(db_conn,
@@ -274,24 +275,35 @@ static int get_data_for_uid(char *uid, struct ykc_stats *stats)
        if (PQresultStatus(res) != PGRES_TUPLES_OK)
        {
                fprintf(stderr, "SELECT failed: %s", PQerrorMessage(db_conn));
-               PQclear(res);
-               return -1;
-               /* XXX Better error handling*/
+               r = -1;
+               goto free_mem;
        }
        assert(PQgetlength(res, 0, PQfnumber(res, "secret_key")) == YUBIKEY_KEY_SIZE);
        stats->secret_key = ysc_memdup(
                PQgetvalue(res, 0, PQfnumber(res, "secret_key")),
                PQgetlength(res, 0, PQfnumber(res, "secret_key")));
+       if (stats->secret_key == NULL) {
+               r = -1;
+               goto free_mem;
+       }
 
        stats->secret_uid = ysc_memdup(
                PQgetvalue(res, 0, PQfnumber(res, "secret_uid")),
                PQgetlength(res, 0, PQfnumber(res, "secret_uid")));
+       if (stats->secret_uid == NULL) {
+               r = -1;
+               goto free_mem;
+       }
 
        tmp = PQgetvalue(res, 0, PQfnumber(res, "session_counter"));
+       assert(tmp != NULL); /* DB schema should enforce this */
        stats->session_counter = ntohl(*((uint32_t *) tmp));
        tmp = PQgetvalue(res, 0, PQfnumber(res, "session_use"));
+       assert(tmp != NULL); /* DB schema should enforce this */
        stats->session_use = ntohl(*((uint32_t *) tmp));
        stats->public_id = NULL;
+
+free_mem:
        PQclear(res);
        return 0;
 }