From 7065a7af196bb09f80beddce82fa728c8a6dfcba Mon Sep 17 00:00:00 2001 From: Heikki Linnakangas Date: Thu, 20 Feb 2014 09:09:47 +0200 Subject: [PATCH] Add locking to SQLFreeStmt and SQLFreeHandle. This fixes a race condition, where SQLFreeStmt is called while the connection is busy executing another statement. SC_set_prepared would see that the connection is busy (CONN_EXECUTING), and not issue a DEALLOCATE statement to free the prepared statement in the backend, leaking it. --- odbcapi.c | 27 +++++++++++++++++++++++++++ odbcapi30.c | 17 +++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/odbcapi.c b/odbcapi.c index fc124d5..efcd480 100644 --- a/odbcapi.c +++ b/odbcapi.c @@ -455,9 +455,36 @@ SQLFreeStmt(HSTMT StatementHandle, SQLUSMALLINT Option) { RETCODE ret; + StatementClass *stmt = (StatementClass *) StatementHandle; + ConnectionClass *conn = NULL; mylog("[SQLFreeStmt]"); + + if (stmt) + { + if (Option == SQL_DROP) + { + conn = stmt->hdbc; + if (conn) + ENTER_CONN_CS(conn); + } + else + ENTER_STMT_CS(stmt); + } + ret = PGAPI_FreeStmt(StatementHandle, Option); + + if (stmt) + { + if (Option == SQL_DROP) + { + if (conn) + LEAVE_CONN_CS(conn); + } + else + LEAVE_STMT_CS(stmt); + } + return ret; } diff --git a/odbcapi30.c b/odbcapi30.c index 16288bb..4a99a4e 100644 --- a/odbcapi30.c +++ b/odbcapi30.c @@ -239,7 +239,11 @@ SQLFreeHandle(SQLSMALLINT HandleType, SQLHANDLE Handle) { CSTR func = "SQLFreeHandle"; RETCODE ret; + StatementClass *stmt; + ConnectionClass *conn = NULL; + mylog("[[%s]]", func); + switch (HandleType) { case SQL_HANDLE_ENV: @@ -249,7 +253,20 @@ SQLFreeHandle(SQLSMALLINT HandleType, SQLHANDLE Handle) ret = PGAPI_FreeConnect(Handle); break; case SQL_HANDLE_STMT: + stmt = (StatementClass *) Handle; + + if (stmt) + { + conn = stmt->hdbc; + if (conn) + ENTER_CONN_CS(conn); + } + ret = PGAPI_FreeStmt(Handle, SQL_DROP); + + if (conn) + LEAVE_CONN_CS(conn); + break; case SQL_HANDLE_DESC: ret = PGAPI_FreeDesc(Handle); -- 2.39.5