diff --git a/src/interfaces/libpq/fe-auth.c b/src/interfaces/libpq/fe-auth.c index 7e9afbbeee..bde5966f87 100644 --- a/src/interfaces/libpq/fe-auth.c +++ b/src/interfaces/libpq/fe-auth.c @@ -453,7 +453,11 @@ pg_SASL_init(PGconn *conn, int payloadlen) goto error; } if (PQExpBufferDataBroken(mechanism_buf)) - goto oom_error; + { + printfPQExpBuffer(&conn->errorMessage, + libpq_gettext("out of memory\n")); + goto error; + } /* An empty string indicates end of list */ if (mechanism_buf.data[0] == '\0') @@ -561,7 +565,11 @@ pg_SASL_init(PGconn *conn, int payloadlen) password, selected_mechanism); if (!conn->sasl_state) - goto oom_error; + { + printfPQExpBuffer(&conn->errorMessage, + libpq_gettext("out of memory\n")); + goto error; + } /* Get the mechanism-specific Initial Client Response, if any */ pg_fe_scram_exchange(conn->sasl_state, @@ -601,14 +609,7 @@ error: termPQExpBuffer(&mechanism_buf); if (initialresponse) free(initialresponse); - return STATUS_ERROR; -oom_error: - termPQExpBuffer(&mechanism_buf); - if (initialresponse) - free(initialresponse); - printfPQExpBuffer(&conn->errorMessage, - libpq_gettext("out of memory\n")); return STATUS_ERROR; } diff --git a/src/interfaces/libpq/fe-exec.c b/src/interfaces/libpq/fe-exec.c index eea0237c3a..ed8ff5b830 100644 --- a/src/interfaces/libpq/fe-exec.c +++ b/src/interfaces/libpq/fe-exec.c @@ -69,7 +69,7 @@ static bool PQexecStart(PGconn *conn); static PGresult *PQexecFinish(PGconn *conn); static int PQsendDescribe(PGconn *conn, char desc_type, const char *desc_target); -static int check_field_number(const PGresult *res, int field_num); +static bool check_field_number(const PGresult *res, int field_num); /* ---------------- @@ -225,7 +225,7 @@ PQmakeEmptyPGresult(PGconn *conn, ExecStatusType status) * function fails, it returns zero. If the function succeeds, it * returns a non-zero value. */ -int +bool PQsetResultAttrs(PGresult *res, int numAttributes, PGresAttDesc *attDescs) { int i; @@ -427,7 +427,7 @@ dupEvents(PGEvent *events, int count, size_t *memSize) * Returns a non-zero value for success and zero for failure. * (On failure, we report the specific problem via pqInternalNotice.) */ -int +bool PQsetvalue(PGresult *res, int tup_num, int field_num, char *value, int len) { PGresAttValue *attval; @@ -694,27 +694,29 @@ void PQclear(PGresult *res) { PGresult_data *block; - int i; if (!res) return; - for (i = 0; i < res->nEvents; i++) + if (res->events) { - /* only send DESTROY to successfully-initialized event procs */ - if (res->events[i].resultInitialized) - { - PGEventResultDestroy evt; + int i; - evt.result = res; - (void) res->events[i].proc(PGEVT_RESULTDESTROY, &evt, - res->events[i].passThrough); - } - free(res->events[i].name); - } + for (i = 0; i < res->nEvents; i++) + { + /* only send DESTROY to successfully-initialized event procs */ + if (res->events[i].resultInitialized) + { + PGEventResultDestroy evt; - if (res->events) + evt.result = res; + (void) res->events[i].proc(PGEVT_RESULTDESTROY, &evt, + res->events[i].passThrough); + } + free(res->events[i].name); + } free(res->events); + } /* Free all the subsidiary blocks */ while ((block = res->curBlock) != NULL) @@ -1233,9 +1235,6 @@ fail: int PQsendQuery(PGconn *conn, const char *query) { - if (!PQsendQueryStart(conn)) - return 0; - /* check the argument */ if (!query) { @@ -1243,6 +1242,8 @@ PQsendQuery(PGconn *conn, const char *query) libpq_gettext("command string is a null pointer\n")); return 0; } + if (!PQsendQueryStart(conn)) + return 0; /* construct the outgoing Query message */ if (pqPutMsgStart('Q', false, conn) < 0 || @@ -1291,9 +1292,6 @@ PQsendQueryParams(PGconn *conn, const int *paramFormats, int resultFormat) { - if (!PQsendQueryStart(conn)) - return 0; - /* check the arguments */ if (!command) { @@ -1307,6 +1305,8 @@ PQsendQueryParams(PGconn *conn, libpq_gettext("number of parameters must be between 0 and 65535\n")); return 0; } + if (!PQsendQueryStart(conn)) + return 0; return PQsendQueryGuts(conn, command, @@ -1331,9 +1331,6 @@ PQsendPrepare(PGconn *conn, const char *stmtName, const char *query, int nParams, const Oid *paramTypes) { - if (!PQsendQueryStart(conn)) - return 0; - /* check the arguments */ if (!stmtName) { @@ -1362,6 +1359,9 @@ PQsendPrepare(PGconn *conn, return 0; } + if (!PQsendQueryStart(conn)) + return 0; + /* construct the Parse message */ if (pqPutMsgStart('P', false, conn) < 0 || pqPuts(stmtName, conn) < 0 || @@ -1432,9 +1432,6 @@ PQsendQueryPrepared(PGconn *conn, const int *paramFormats, int resultFormat) { - if (!PQsendQueryStart(conn)) - return 0; - /* check the arguments */ if (!stmtName) { @@ -1449,6 +1446,9 @@ PQsendQueryPrepared(PGconn *conn, return 0; } + if (!PQsendQueryStart(conn)) + return 0; + return PQsendQueryGuts(conn, NULL, /* no command to parse */ stmtName, @@ -2219,13 +2219,6 @@ PQsendDescribePortal(PGconn *conn, const char *portal) static int PQsendDescribe(PGconn *conn, char desc_type, const char *desc_target) { - /* Treat null desc_target as empty string */ - if (!desc_target) - desc_target = ""; - - if (!PQsendQueryStart(conn)) - return 0; - /* This isn't gonna work on a 2.0 server */ if (PG_PROTOCOL_MAJOR(conn->pversion) < 3) { @@ -2234,6 +2227,13 @@ PQsendDescribe(PGconn *conn, char desc_type, const char *desc_target) return 0; } + if (!PQsendQueryStart(conn)) + return 0; + + /* Treat null desc_target as empty string */ + if (!desc_target) + desc_target = ""; + /* construct the Describe message */ if (pqPutMsgStart('D', false, conn) < 0 || pqPutc(desc_type, conn) < 0 || @@ -2794,7 +2794,7 @@ PQbinaryTuples(const PGresult *res) * Return true if OK, false if not */ -static int +static bool check_field_number(const PGresult *res, int field_num) { if (!res) @@ -2809,7 +2809,7 @@ check_field_number(const PGresult *res, int field_num) return true; } -static int +static bool check_tuple_field_number(const PGresult *res, int tup_num, int field_num) { @@ -2832,7 +2832,7 @@ check_tuple_field_number(const PGresult *res, return true; } -static int +static bool check_param_number(const PGresult *res, int param_num) { if (!res) diff --git a/src/interfaces/libpq/libpq-fe.h b/src/interfaces/libpq/libpq-fe.h index c9e6ac2b76..c7794a9a1f 100644 --- a/src/interfaces/libpq/libpq-fe.h +++ b/src/interfaces/libpq/libpq-fe.h @@ -524,10 +524,10 @@ extern void PQfreemem(void *ptr); /* Create and manipulate PGresults */ extern PGresult *PQmakeEmptyPGresult(PGconn *conn, ExecStatusType status); extern PGresult *PQcopyResult(const PGresult *src, int flags); -extern int PQsetResultAttrs(PGresult *res, int numAttributes, PGresAttDesc *attDescs); +extern bool PQsetResultAttrs(PGresult *res, int numAttributes, PGresAttDesc *attDescs); extern void *PQresultAlloc(PGresult *res, size_t nBytes); extern size_t PQresultMemorySize(const PGresult *res); -extern int PQsetvalue(PGresult *res, int tup_num, int field_num, char *value, int len); +extern bool PQsetvalue(PGresult *res, int tup_num, int field_num, char *value, int len); /* Quoting strings before inclusion in queries. */ extern size_t PQescapeStringConn(PGconn *conn,