Skip to content

Catch possible stack overflow when preparing and compiling user statements #8255

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions src/dsql/dsql.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
#include "../common/intlobj_new.h"
#include "../jrd/jrd.h"
#include "../jrd/status.h"
#include "../jrd/ibsetjmp.h"
#include "../common/CharSet.h"
#include "../dsql/Parser.h"
#include "../dsql/ddl_proto.h"
Expand Down Expand Up @@ -85,6 +86,7 @@ using namespace Firebird;
static ULONG get_request_info(thread_db*, DsqlRequest*, ULONG, UCHAR*);
static dsql_dbb* init(Jrd::thread_db*, Jrd::Attachment*);
static DsqlRequest* prepareRequest(thread_db*, dsql_dbb*, jrd_tra*, ULONG, const TEXT*, USHORT, bool);
static DsqlRequest* safePrepareRequest(thread_db*, dsql_dbb*, jrd_tra*, ULONG, const TEXT*, USHORT, bool);
static RefPtr<DsqlStatement> prepareStatement(thread_db*, dsql_dbb*, jrd_tra*, ULONG, const TEXT*, USHORT,
bool, ntrace_result_t* traceResult);
static UCHAR* put_item(UCHAR, const USHORT, const UCHAR*, UCHAR*, const UCHAR* const);
Expand Down Expand Up @@ -260,7 +262,7 @@ DsqlRequest* DSQL_prepare(thread_db* tdbb,
{
// Allocate a new request block and then prepare the request.

dsqlRequest = prepareRequest(tdbb, database, transaction, length, string, dialect,
dsqlRequest = safePrepareRequest(tdbb, database, transaction, length, string, dialect,
isInternalRequest);

// Can not prepare a CREATE DATABASE/SCHEMA statement
Expand Down Expand Up @@ -335,7 +337,7 @@ void DSQL_execute_immediate(thread_db* tdbb, Jrd::Attachment* attachment, jrd_tr

try
{
dsqlRequest = prepareRequest(tdbb, database, *tra_handle, length, string, dialect,
dsqlRequest = safePrepareRequest(tdbb, database, *tra_handle, length, string, dialect,
isInternalRequest);

const auto dsqlStatement = dsqlRequest->getDsqlStatement();
Expand Down Expand Up @@ -439,6 +441,23 @@ static dsql_dbb* init(thread_db* tdbb, Jrd::Attachment* attachment)
return attachment->att_dsql_instance;
}

// Use SEH frame when preparing user requests to catch possible stack overflows
static DsqlRequest* safePrepareRequest(thread_db* tdbb, dsql_dbb* database, jrd_tra* transaction,
ULONG textLength, const TEXT* text, USHORT clientDialect, bool isInternalRequest)
{
if (isInternalRequest)
return prepareRequest(tdbb, database, transaction, textLength, text, clientDialect, true);

#ifdef WIN_NT
START_CHECK_FOR_EXCEPTIONS(NULL);
#endif
return prepareRequest(tdbb, database, transaction, textLength, text, clientDialect, false);

#ifdef WIN_NT
END_CHECK_FOR_EXCEPTIONS(NULL);
#endif
}


// Prepare a request for execution.
// Note: caller is responsible for pool handling.
Expand Down
Loading