From b6f2a07e6b231c2a3d3301b71230b5a83815216c Mon Sep 17 00:00:00 2001 From: Tollef Fog Heen Date: Mon, 12 Oct 2009 08:52:44 +0200 Subject: [PATCH] Better error handling in get_data_for_uid --- src/main.c | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/main.c b/src/main.c index 59a498e..765be2a 100644 --- a/src/main.c +++ b/src/main.c @@ -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; } -- 2.39.5