Skip to content

Commit

Permalink
[APIS-936] Handle TextLength2Ptr NULL exception in SQLNativeSql (#64)
Browse files Browse the repository at this point in the history
Handle NULL pointer exception, SQLNativeSql
  • Loading branch information
kisoo-han authored Oct 8, 2022
1 parent e31cacf commit bb3c7bb
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 3 deletions.
48 changes: 48 additions & 0 deletions UnitTest-CPP/UnitTest-CPP/UnitTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,54 @@ namespace UnitTestCPP
retcode = SQLFreeHandle(SQL_HANDLE_ENV, hEnv);
}

TEST_METHOD(APIS_936_TextLength2Ptr_NULL_CHK)
{
SQLHENV hEnv;
SQLHDBC hDbc;
SQLHSTMT hStmt;
WCHAR query2 [512] = L"select date_format(now(),'%T')";
WCHAR query [512] = L"show tables";
WCHAR converted_qry [512];
WCHAR col1[512];
WCHAR msg[512];
SQLINTEGER length;
SQLINTEGER retcode;
SQLLEN len;
int num_rows = 0;

retcode = SQLAllocEnv(&hEnv);
retcode = SQLSetEnvAttr(hEnv, SQL_ATTR_ODBC_VERSION, (void *)SQL_OV_ODBC3, 0);
retcode = SQLAllocConnect(hEnv, &hDbc);
retcode = SQLDriverConnect(hDbc, NULL, L"DRIVER=CUBRID Driver Unicode;server=test-db-server;port=33000;uid=public;pwd=;db_name=demodb;charset=utf-8;", SQL_NTS, NULL, 0, NULL, SQL_DRIVER_NOPROMPT);
Assert::AreNotEqual((int)retcode, SQL_ERROR);
retcode = SQLAllocHandle(SQL_HANDLE_STMT, hDbc, &hStmt);

// Case-1: Check if SQL_ERROR is returned when TextLength2Ptr is NULL
retcode = SQLNativeSqlW (hDbc, query, sizeof(query), converted_qry, sizeof(converted_qry), NULL);
Assert::AreEqual((int)retcode, SQL_ERROR);

// Case-2: Convert and execute with proper TextLength2Ptr
retcode = SQLNativeSqlW(hDbc, query, sizeof(query), converted_qry, sizeof(converted_qry), &length);
Assert::AreNotEqual((int)retcode, SQL_ERROR);
wsprintf(msg, L"len = %d, converted qry = '%s'", length, converted_qry);
Logger::WriteMessage(msg);

retcode = SQLPrepareW(hStmt, converted_qry, SQL_NTS);
retcode = SQLExecute(hStmt);
retcode = SQLBindCol(hStmt, 1, SQL_C_WCHAR, (SQLPOINTER)col1, sizeof(col1), &len);

memset(col1, 0, sizeof(col1));
while ((retcode = SQLFetch(hStmt)) == SQL_SUCCESS) {
wsprintf (msg, L"rows [%2d]: col1 = '%s'\n", ++num_rows, col1);
Logger::WriteMessage(msg);
memset(col1, 0, sizeof(col1));
}

retcode = SQLDisconnect(hDbc);
retcode = SQLFreeHandle(SQL_HANDLE_DBC, hDbc);
retcode = SQLFreeHandle(SQL_HANDLE_ENV, hEnv);
}

TEST_METHOD(APIS_794_QueryPlanMultiByte)
{
RETCODE retcode;
Expand Down
6 changes: 6 additions & 0 deletions odbc_interface.c
Original file line number Diff line number Diff line change
Expand Up @@ -1389,6 +1389,12 @@ SQLNativeSql (SQLHDBC ConnectionHandle,

odbc_free_diag (((ODBC_CONNECTION *) ConnectionHandle)->diag, RESET);

if (TextLength2Ptr == NULL)
{
odbc_set_diag (((ODBC_CONNECTION *) ConnectionHandle)->diag, "HY009", 0, "NULL pointer: TextLength2Ptr");
return ODBC_ERROR;
}

stInStatementText = UT_MAKE_STRING (InStatementText, TextLength1);


Expand Down
4 changes: 2 additions & 2 deletions odbc_version.i
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#define MAJOR_VERSION 11
#define MINOR_VERSION 2
#define PATCH_VERSION 0
#define PATCH_VERSION 1
#define BUILD_SERIAL_NUMBER 0001
#define VERSION_STRING "11.2.0.0001"
#define VERSION_STRING "11.2.1.0055"
3 changes: 2 additions & 1 deletion unicode.c
Original file line number Diff line number Diff line change
Expand Up @@ -333,13 +333,14 @@ SQLNativeSqlW (SQLHDBC hdbc, SQLWCHAR *in, SQLINTEGER in_len,
memset (sql_text_buffer, 0 , out_max);

ret = SQLNativeSql(hdbc, sql_state, sql_state_len, sql_text_buffer, out_max, out_len);
sql_state_len = *out_len;

if (ret == ODBC_ERROR)
{
UT_FREE (sql_text_buffer);
return ret;
}

sql_state_len = *out_len;
bytes_to_wide_char (sql_text_buffer, sql_state_len, &out, out_max, out_len, conn->charset);
UT_FREE (sql_text_buffer);
return ret;
Expand Down

0 comments on commit bb3c7bb

Please sign in to comment.