Skip to content
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
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
10 changes: 6 additions & 4 deletions mssql_python/cursor.py
Original file line number Diff line number Diff line change
Expand Up @@ -389,9 +389,9 @@ def _map_sql_type(self, param, parameters_list, i):
False,
)
return (
ddbc_sql_const.SQL_BINARY.value,
ddbc_sql_const.SQL_VARBINARY.value, # Use VARBINARY instead of BINARY
ddbc_sql_const.SQL_C_BINARY.value,
len(param),
max(len(param), 1), # Ensure minimum column size of 1
0,
False,
)
Expand All @@ -406,9 +406,9 @@ def _map_sql_type(self, param, parameters_list, i):
True,
)
return (
ddbc_sql_const.SQL_BINARY.value,
ddbc_sql_const.SQL_VARBINARY.value, # Use VARBINARY instead of BINARY
ddbc_sql_const.SQL_C_BINARY.value,
len(param),
max(len(param), 1), # Ensure minimum column size of 1
0,
False,
)
Expand Down Expand Up @@ -849,6 +849,8 @@ def _select_best_sample_value(column):
return max(non_nulls, key=lambda s: len(str(s)))
if all(isinstance(v, datetime.datetime) for v in non_nulls):
return datetime.datetime.now()
if all(isinstance(v, (bytes, bytearray)) for v in non_nulls):
return max(non_nulls, key=lambda b: len(b))
if all(isinstance(v, datetime.date) for v in non_nulls):
return datetime.date.today()
return non_nulls[0] # fallback
Expand Down
24 changes: 22 additions & 2 deletions mssql_python/pybind/ddbc_bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1356,7 +1356,13 @@
std::string offending = WideToUTF8(wstr);
ThrowStdException("Input string exceeds allowed column size at parameter index " + std::to_string(paramIndex));
}
#if defined(__APPLE__) || defined(__linux__)
auto utf16Buf = WStringToSQLWCHAR(wstr);
size_t copySize = std::min(utf16Buf.size(), static_cast<size_t>(info.columnSize + 1));
std::memcpy(wcharArray + i * (info.columnSize + 1), utf16Buf.data(), copySize * sizeof(SQLWCHAR));
#else
std::memcpy(wcharArray + i * (info.columnSize + 1), wstr.c_str(), (wstr.length() + 1) * sizeof(SQLWCHAR));
#endif
strLenOrIndArray[i] = SQL_NTS;
}
}
Expand Down Expand Up @@ -1420,7 +1426,12 @@
std::string str = columnValues[i].cast<std::string>();
if (str.size() > info.columnSize)
ThrowStdException("Input exceeds column size at index " + std::to_string(i));
std::memcpy(charArray + i * (info.columnSize + 1), str.c_str(), str.size());
// Clear the entire buffer slot first
std::memset(charArray + i * (info.columnSize + 1), 0, info.columnSize + 1);
// Then copy the actual data
if (str.size() > 0) {
std::memcpy(charArray + i * (info.columnSize + 1), str.c_str(), str.size());

Check notice

Code scanning / devskim

There are a number of conditions in which memcpy can introduce a vulnerability (mismatched buffer sizes, null pointers, etc.). More secure alternitives perform additional validation of the source and destination buffer Note

Problematic C function detected (memcpy)
}
strLenOrIndArray[i] = static_cast<SQLLEN>(str.size());
}
}
Expand Down Expand Up @@ -1778,6 +1789,9 @@
}
} else if (dataLen == SQL_NULL_DATA) {
row.append(py::none());
} else if (dataLen == 0) {
// Empty string
row.append(std::string(""));
} else {
assert(dataLen == SQL_NO_TOTAL);
LOG("SQLGetData couldn't determine the length of the data. "
Expand Down Expand Up @@ -1834,6 +1848,9 @@
}
} else if (dataLen == SQL_NULL_DATA) {
row.append(py::none());
} else if (dataLen == 0) {
// Empty string
row.append(py::str(""));
} else {
assert(dataLen == SQL_NO_TOTAL);
LOG("SQLGetData couldn't determine the length of the data. "
Expand Down Expand Up @@ -2030,6 +2047,9 @@
}
} else if (dataLen == SQL_NULL_DATA) {
row.append(py::none());
} else if (dataLen == 0) {
// Empty bytes
row.append(py::bytes(""));
} else {
assert(dataLen == SQL_NO_TOTAL);
LOG("SQLGetData couldn't determine the length of the data. "
Expand Down Expand Up @@ -2318,7 +2338,7 @@
row.append(py::none());
continue;
}
assert(dataLen > 0 && "Must be > 0 since SQL_NULL_DATA & SQL_NO_DATA is already handled");
assert(dataLen >= 0 && "Data length must be >= 0");

switch (dataType) {
case SQL_CHAR:
Expand Down
Loading
Loading