Skip to content

Commit aaf3d85

Browse files
committed
fixes memory leak issue-AB#37606
1 parent 7b91e8f commit aaf3d85

1 file changed

Lines changed: 9 additions & 4 deletions

File tree

mssql_python/pybind/connection/connection.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -173,16 +173,16 @@ SQLRETURN Connection::setAttribute(SQLINTEGER attribute, py::object value) {
173173
LOG("Setting SQL attribute");
174174
SQLPOINTER ptr = nullptr;
175175
SQLINTEGER length = 0;
176+
std::string buffer; // to hold sensitive data temporarily
176177

177178
if (py::isinstance<py::int_>(value)) {
178179
int intValue = value.cast<int>();
179180
ptr = reinterpret_cast<SQLPOINTER>(static_cast<uintptr_t>(intValue));
180181
length = SQL_IS_INTEGER;
181182
} else if (py::isinstance<py::bytes>(value) || py::isinstance<py::bytearray>(value)) {
182-
static std::vector<std::string> buffers;
183-
buffers.emplace_back(value.cast<std::string>());
184-
ptr = const_cast<char*>(buffers.back().c_str());
185-
length = static_cast<SQLINTEGER>(buffers.back().size());
183+
buffer = value.cast<std::string>(); // stack buffer
184+
ptr = const_cast<char*>(buffer.c_str());
185+
length = static_cast<SQLINTEGER>(buffer.size());
186186
} else {
187187
LOG("Unsupported attribute value type");
188188
return SQL_ERROR;
@@ -195,6 +195,11 @@ SQLRETURN Connection::setAttribute(SQLINTEGER attribute, py::object value) {
195195
else {
196196
LOG("Set attribute successfully");
197197
}
198+
199+
// Zero out sensitive data if used
200+
if (!buffer.empty()) {
201+
std::fill(buffer.begin(), buffer.end(), static_cast<char>(0));
202+
}
198203
return ret;
199204
}
200205

0 commit comments

Comments
 (0)