Skip to content
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
13 changes: 9 additions & 4 deletions mssql_python/pybind/connection/connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,16 +173,16 @@ SQLRETURN Connection::setAttribute(SQLINTEGER attribute, py::object value) {
LOG("Setting SQL attribute");
SQLPOINTER ptr = nullptr;
SQLINTEGER length = 0;
std::string buffer; // to hold sensitive data temporarily

if (py::isinstance<py::int_>(value)) {
int intValue = value.cast<int>();
ptr = reinterpret_cast<SQLPOINTER>(static_cast<uintptr_t>(intValue));
length = SQL_IS_INTEGER;
} else if (py::isinstance<py::bytes>(value) || py::isinstance<py::bytearray>(value)) {
static std::vector<std::string> buffers;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removing Static keyword and defining the vector outside the loop may solve the issue??

buffers.emplace_back(value.cast<std::string>());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line supports multiple string inside a vector. But new change is only for one string. Did we validate the impact for all scenario?

ptr = const_cast<char*>(buffers.back().c_str());
length = static_cast<SQLINTEGER>(buffers.back().size());
buffer = value.cast<std::string>(); // stack buffer
ptr = buffer.data();
length = static_cast<SQLINTEGER>(buffer.size());
} else {
LOG("Unsupported attribute value type");
return SQL_ERROR;
Expand All @@ -195,6 +195,11 @@ SQLRETURN Connection::setAttribute(SQLINTEGER attribute, py::object value) {
else {
LOG("Set attribute successfully");
}

// Zero out sensitive data if used
if (!buffer.empty()) {
std::fill(buffer.begin(), buffer.end(), static_cast<char>(0));
Copy link
Contributor

@subrata-ms subrata-ms Oct 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why fill? buffer is not allocated via "new". buffer is also a local variable.

}
return ret;
}

Expand Down
Loading