Skip to content

FIX: validate numeric data for range (upper and lower bound) #77

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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: 23 additions & 0 deletions mssql_python/pybind/ddbc_bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,11 @@ SQLRETURN BindParameters(SQLHANDLE hStmt, const py::list& params,
if (!py::isinstance<py::int_>(param)) {
ThrowStdException(MakeParamMismatchErrorStr(paramInfo.paramCType, paramIndex));
}
int value = param.cast<int>();
// Range validation for signed 16-bit integer
if (value < std::numeric_limits<short>::min() || value > std::numeric_limits<short>::max()) {
ThrowStdException("Signed short integer parameter out of range at paramIndex " + std::to_string(paramIndex));
}
dataPtr =
static_cast<void*>(AllocateParamBuffer<int>(paramBuffers, param.cast<int>()));
break;
Expand All @@ -274,6 +279,10 @@ SQLRETURN BindParameters(SQLHANDLE hStmt, const py::list& params,
if (!py::isinstance<py::int_>(param)) {
ThrowStdException(MakeParamMismatchErrorStr(paramInfo.paramCType, paramIndex));
}
unsigned int value = param.cast<unsigned int>();
if (value > std::numeric_limits<unsigned short>::max()) {
ThrowStdException("Unsigned short integer parameter out of range at paramIndex " + std::to_string(paramIndex));
}
dataPtr = static_cast<void*>(
AllocateParamBuffer<unsigned int>(paramBuffers, param.cast<unsigned int>()));
break;
Expand All @@ -284,6 +293,11 @@ SQLRETURN BindParameters(SQLHANDLE hStmt, const py::list& params,
if (!py::isinstance<py::int_>(param)) {
ThrowStdException(MakeParamMismatchErrorStr(paramInfo.paramCType, paramIndex));
}
int64_t value = param.cast<int64_t>();
// Range validation for signed 64-bit integer
if (value < std::numeric_limits<int64_t>::min() || value > std::numeric_limits<int64_t>::max()) {
ThrowStdException("Signed 64-bit integer parameter out of range at paramIndex " + std::to_string(paramIndex));
}
dataPtr = static_cast<void*>(
AllocateParamBuffer<int64_t>(paramBuffers, param.cast<int64_t>()));
break;
Expand All @@ -293,6 +307,11 @@ SQLRETURN BindParameters(SQLHANDLE hStmt, const py::list& params,
if (!py::isinstance<py::int_>(param)) {
ThrowStdException(MakeParamMismatchErrorStr(paramInfo.paramCType, paramIndex));
}
uint64_t value = param.cast<uint64_t>();
// Range validation for unsigned 64-bit integer
if (value > std::numeric_limits<uint64_t>::max()) {
ThrowStdException("Unsigned 64-bit integer parameter out of range at paramIndex " + std::to_string(paramIndex));
}
dataPtr = static_cast<void*>(
AllocateParamBuffer<uint64_t>(paramBuffers, param.cast<uint64_t>()));
break;
Expand All @@ -318,6 +337,10 @@ SQLRETURN BindParameters(SQLHANDLE hStmt, const py::list& params,
if (!py::isinstance(param, dateType)) {
ThrowStdException(MakeParamMismatchErrorStr(paramInfo.paramCType, paramIndex));
}
int year = param.attr("year").cast<int>();
if (year < 1753 || year > 9999) {
ThrowStdException("Date out of range for SQL Server (1753-9999) at paramIndex " + std::to_string(paramIndex));
}
// TODO: can be moved to python by registering SQL_DATE_STRUCT in pybind
SQL_DATE_STRUCT* sqlDatePtr = AllocateParamBuffer<SQL_DATE_STRUCT>(paramBuffers);
sqlDatePtr->year = param.attr("year").cast<int>();
Expand Down