Skip to content

Commit

Permalink
Bind 32-bit ints correctly on big endian LP64 systems (#611)
Browse files Browse the repository at this point in the history
* Bind 32-bit ints correctly on big endian LP64 systems

The size of a long on 64-bit Linux, Unix, and other LP64 platforms
is 64-bit, so passing the address of it and saying it is a 32-bit
integer will result in only the top 4 bytes being read. While this
does work on little endian systems, on big endian systems these bytes
will always be 0 for values < INT_MAX.

Instead, change the type to be an int, which should be 32 bits on
all supported platforms.

* Fix compiler warnings
  • Loading branch information
kadler authored and mkleehammer committed Dec 21, 2019
1 parent 49a9032 commit c3067c7
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/cursor.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ struct ParamInfo
union
{
unsigned char ch;
long l;
int i32;
INT64 i64;
double dbl;
TIMESTAMP_STRUCT timestamp;
Expand Down
8 changes: 4 additions & 4 deletions src/params.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -880,11 +880,11 @@ static bool GetIntInfo(Cursor* cur, Py_ssize_t index, PyObject* param, ParamInfo
}
else
{
info.Data.l = PyLong_AsLong(param);
info.Data.i32 = (int)PyLong_AsLong(param);

info.ValueType = SQL_C_LONG;
info.ParameterType = SQL_INTEGER;
info.ParameterValuePtr = &info.Data.l;
info.ParameterValuePtr = &info.Data.i32;
info.StrLen_or_Ind = 4;
}

Expand All @@ -909,11 +909,11 @@ static bool GetLongInfo(Cursor* cur, Py_ssize_t index, PyObject* param, ParamInf
}
else
{
info.Data.l = PyLong_AsLong(param);
info.Data.i32 = (int)PyLong_AsLong(param);

info.ValueType = SQL_C_LONG;
info.ParameterType = SQL_INTEGER;
info.ParameterValuePtr = &info.Data.l;
info.ParameterValuePtr = &info.Data.i32;
info.StrLen_or_Ind = 4;
}

Expand Down

0 comments on commit c3067c7

Please sign in to comment.