Skip to content

Commit

Permalink
Front ported CORE-3554: Server crashes during prepare or throws incor…
Browse files Browse the repository at this point in the history
…rect parsing error if the remotely passed SQL query is empty.
  • Loading branch information
dyemanov committed Jul 15, 2011
1 parent d455d15 commit ac96c3a
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 30 deletions.
36 changes: 8 additions & 28 deletions src/dsql/dsql.cpp
Expand Up @@ -574,19 +574,6 @@ void DSQL_prepare(thread_db* tdbb,

dsql_req* request = NULL;

if (string && !length)
{
length = strlen(string);
}

if (!string || !length)
{
ERRD_post(Arg::Gds(isc_sqlerr) << Arg::Num(-104) <<
// Unexpected end of command
// CVC: Nothing will be line 1, column 1 for the user.
Arg::Gds(isc_command_end_err2) << Arg::Num(1) << Arg::Num(1));
}

try
{
// Figure out which parser version to use
Expand Down Expand Up @@ -976,17 +963,6 @@ static void execute_immediate(thread_db* tdbb,
{
SET_TDBB(tdbb);

if (!string)
{
ERRD_post(Arg::Gds(isc_sqlerr) << Arg::Num(-104) <<
// Unexpected end of command
// CVC: Nothing will be line 1, column 1 for the user.
Arg::Gds(isc_command_end_err2) << Arg::Num(1) << Arg::Num(1));
}

if (length == 0)
length = strlen(string);

dsql_dbb* const database = init(tdbb, attachment);
dsql_req* request = NULL;

Expand Down Expand Up @@ -1874,17 +1850,21 @@ static dsql_req* prepareStatement(thread_db* tdbb, dsql_dbb* database, jrd_tra*
Arg::Gds(isc_wish_list));
}

if (!string)
if (string && !string_length)
{
size_t sql_length = strlen(string);
if (sql_length > MAX_USHORT)
sql_length = MAX_USHORT;
string_length = static_cast<USHORT>(sql_length);
}

if (!string || !string_length) {
ERRD_post(Arg::Gds(isc_sqlerr) << Arg::Num(-104) <<
// Unexpected end of command
// CVC: Nothing will be line 1, column 1 for the user.
Arg::Gds(isc_command_end_err2) << Arg::Num(1) << Arg::Num(1));
}

if (string_length == 0)
string_length = strlen(string);

// Get rid of the trailing ";" if there is one.

for (const TEXT* p = string + string_length; p-- > string;)
Expand Down
20 changes: 18 additions & 2 deletions src/remote/client/interface.cpp
Expand Up @@ -1812,6 +1812,14 @@ Firebird::ITransaction* Attachment::execute(IStatus* status, Firebird::ITransact
CHECK_HANDLE(transaction, isc_bad_trans_handle);
}

if (!length)
{
size_t sql_length = strlen(string);
if (sql_length > MAX_USHORT)
sql_length = MAX_USHORT;
length = static_cast<USHORT>(sql_length);
}

if (dialect > 10)
{
// dimitr: adjust dialect received after
Expand Down Expand Up @@ -1900,7 +1908,7 @@ Firebird::ITransaction* Attachment::execute(IStatus* status, Firebird::ITransact
P_SQLST* ex_now = &packet->p_sqlst;
ex_now->p_sqlst_transaction = transaction ? transaction->rtr_id : 0;
ex_now->p_sqlst_SQL_dialect = dialect;
ex_now->p_sqlst_SQL_str.cstr_length = length ? length : strlen(string);
ex_now->p_sqlst_SQL_str.cstr_length = length;
ex_now->p_sqlst_SQL_str.cstr_address = reinterpret_cast<const UCHAR*>(string);
ex_now->p_sqlst_items.cstr_length = 0;
ex_now->p_sqlst_buffer_length = 0;
Expand Down Expand Up @@ -2453,6 +2461,14 @@ void Statement::prepare(IStatus* status, Firebird::ITransaction* apiTra,
CHECK_HANDLE(transaction, isc_bad_trans_handle);
}

if (!stmtLength)
{
size_t sql_length = strlen(sqlStmt);
if (sql_length > MAX_USHORT)
sql_length = MAX_USHORT;
stmtLength = static_cast<USHORT>(sql_length);
}

if (dialect > 10)
{
// dimitr: adjust dialect received after
Expand Down Expand Up @@ -2494,7 +2510,7 @@ void Statement::prepare(IStatus* status, Firebird::ITransaction* apiTra,
prepare->p_sqlst_transaction = transaction ? transaction->rtr_id : 0;
prepare->p_sqlst_statement = statement->rsr_id;
prepare->p_sqlst_SQL_dialect = dialect;
prepare->p_sqlst_SQL_str.cstr_length = stmtLength ? stmtLength : strlen(sqlStmt);
prepare->p_sqlst_SQL_str.cstr_length = stmtLength;
prepare->p_sqlst_SQL_str.cstr_address = reinterpret_cast<const UCHAR*>(sqlStmt);
prepare->p_sqlst_items.cstr_length = items.getCount();
prepare->p_sqlst_items.cstr_address = items.begin();
Expand Down
7 changes: 7 additions & 0 deletions src/remote/protocol.cpp
Expand Up @@ -853,7 +853,14 @@ static bool alloc_cstring(XDR* xdrs, CSTRING* cstring)
**************************************/

if (!cstring->cstr_length)
{
if (cstring->cstr_allocated)
*cstring->cstr_address = '\0';
else
cstring->cstr_address = NULL;

return true;
}

if (cstring->cstr_length > cstring->cstr_allocated && cstring->cstr_allocated)
{
Expand Down

0 comments on commit ac96c3a

Please sign in to comment.