From: Tollef Fog Heen Date: Mon, 12 Oct 2009 06:44:21 +0000 (+0200) Subject: Error handling in get_shared_secret; free memory on failure X-Git-Url: https://err.no/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=70fb1c47a65b2fd40ffa4ffaae953a7f8727b9a6;p=yubikey-server-c Error handling in get_shared_secret; free memory on failure --- diff --git a/src/main.c b/src/main.c index 1c7e9cb..6c5b996 100644 --- a/src/main.c +++ b/src/main.c @@ -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)