Skip to content

Commit de8abbd

Browse files
committed
Resolving comments
1 parent 41e4883 commit de8abbd

File tree

1 file changed

+22
-3
lines changed

1 file changed

+22
-3
lines changed

mssql_python/pybind/ddbc_bindings.cpp

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -456,10 +456,29 @@ SQLRETURN BindParameters(SQLHANDLE hStmt, const py::list& params,
456456
ThrowStdException("Failed to allocate buffer for SQL_C_CHAR parameter at index " + std::to_string(paramIndex));
457457
}
458458

459-
std::memcpy(buffer, strValue.c_str(), strValue.length());
460-
buffer[strValue.length()] = '\0'; // Ensure null termination
459+
// SECURITY: Validate size before copying to prevent buffer overflow
460+
size_t copyLength = strValue.length();
461+
if (copyLength >= bufferSize) {
462+
ThrowStdException("Buffer overflow prevented: string length exceeds allocated buffer at index " + std::to_string(paramIndex));
463+
}
464+
465+
// Use secure copy with bounds checking
466+
#ifdef _WIN32
467+
// Windows: Use memcpy_s for secure copy
468+
errno_t err = memcpy_s(buffer, bufferSize, strValue.data(), copyLength);
469+
if (err != 0) {
470+
ThrowStdException("Secure memory copy failed with error code " + std::to_string(err) + " at index " + std::to_string(paramIndex));
471+
}
472+
#else
473+
// POSIX: Use std::copy_n with explicit bounds checking
474+
if (copyLength > 0) {
475+
std::copy_n(strValue.data(), copyLength, buffer);
476+
}
477+
#endif
478+
479+
buffer[copyLength] = '\0'; // Ensure null termination
461480

462-
paramInfo.strLenOrInd = strValue.length();
481+
paramInfo.strLenOrInd = copyLength;
463482

464483
LOG("Binding SQL_C_CHAR parameter at index {} with encoded length {}", paramIndex, strValue.length());
465484
break;

0 commit comments

Comments
 (0)