Bug correction: freeing memory after failed connection.
This commit is contained in:
parent
9dd086093e
commit
dac9faf47c
@ -498,30 +498,21 @@ static void notice_processor (void *arg, const char *message) {
|
|||||||
|
|
||||||
/*
|
/*
|
||||||
** Connects to a data source.
|
** Connects to a data source.
|
||||||
** This driver provides two ways to connect to a data source:
|
|
||||||
** (1) giving the connection parameters as a set of pairs separated
|
|
||||||
** by whitespaces in a string (first method parameter)
|
|
||||||
** (2) giving one string for each connection parameter, said
|
|
||||||
** datasource, username, password, host and port.
|
|
||||||
*/
|
*/
|
||||||
static int env_connect (lua_State *L) {
|
static int env_connect (lua_State *L) {
|
||||||
const char *sourcename = luaL_checkstring(L, 2);
|
const char *sourcename = luaL_checkstring(L, 2);
|
||||||
|
const char *username = luaL_optstring(L, 3, NULL);
|
||||||
|
const char *password = luaL_optstring(L, 4, NULL);
|
||||||
|
const char *pghost = luaL_optstring(L, 5, NULL);
|
||||||
|
const char *pgport = luaL_optstring(L, 6, NULL);
|
||||||
PGconn *conn;
|
PGconn *conn;
|
||||||
getenvironment (L); /* validate environment */
|
getenvironment (L); /* validate environment */
|
||||||
if ((lua_gettop (L) == 2) && (strchr (sourcename, '=') != NULL))
|
conn = PQsetdbLogin(pghost, pgport, NULL, NULL, sourcename, username, password);
|
||||||
conn = PQconnectdb (sourcename);
|
|
||||||
else {
|
|
||||||
const char *username = luaL_optstring(L, 3, NULL);
|
|
||||||
const char *password = luaL_optstring(L, 4, NULL);
|
|
||||||
const char *pghost = luaL_optstring(L, 5, NULL);
|
|
||||||
const char *pgport = luaL_optstring(L, 6, NULL);
|
|
||||||
|
|
||||||
conn = PQsetdbLogin(pghost, pgport, NULL, NULL,
|
if (PQstatus(conn) == CONNECTION_BAD) {
|
||||||
sourcename, username, password);
|
PQfinish(conn);
|
||||||
}
|
|
||||||
|
|
||||||
if (PQstatus(conn) == CONNECTION_BAD)
|
|
||||||
return luasql_failmsg(L, "error connecting to database. PostgreSQL: ", PQerrorMessage(conn));
|
return luasql_failmsg(L, "error connecting to database. PostgreSQL: ", PQerrorMessage(conn));
|
||||||
|
}
|
||||||
PQsetNoticeProcessor(conn, notice_processor, NULL);
|
PQsetNoticeProcessor(conn, notice_processor, NULL);
|
||||||
return create_connection(L, 1, conn);
|
return create_connection(L, 1, conn);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user