s4:libcli: Fix conversion from HRESULT and WERROR to Python objects
authorJoseph Sutton <josephsutton@catalyst.net.nz>
Fri, 8 Dec 2023 02:58:32 +0000 (15:58 +1300)
committerAndrew Bartlett <abartlet@samba.org>
Thu, 21 Dec 2023 20:21:34 +0000 (20:21 +0000)
The inner values of HRESULT and WERROR are 32‐bit unsigned integers,
which might not be representable in type ‘int’. We must then use the ‘k’
format specifier, which corresponds to ‘unsigned long’, a type
guaranteed to be at least 32 bits in size.

Commit c81aff362fe99a65385c6f8337ffcb47c9456829 fixed
PyErr_FromNTSTATUS(), but it did not attempt to fix the other cases.

PyErr_FromHRESULT() might return a tuple like this:
(-2147024809, 'One or more arguments are invalid.')

which, after this commit, will become this:
(2147942487, 'One or more arguments are invalid.')

Signed-off-by: Joseph Sutton <josephsutton@catalyst.net.nz>
Reviewed-by: Andrew Bartlett <abartlet@samba.org>
source4/libcli/util/pyerrors.h

index 2eec8c8293f9270958fba2b2af3855fe4c958132..69110ca27322cdde9942349ba720ad3a5ee7b4b0 100644 (file)
 #ifndef __PYERRORS_H__
 #define __PYERRORS_H__
 
-#define PyErr_FromWERROR(err) Py_BuildValue("(i,s)", W_ERROR_V(err), discard_const_p(char, win_errstr(err)))
+#define PyErr_FromWERROR(err) Py_BuildValue("(k,s)", (unsigned long)(W_ERROR_V(err)), discard_const_p(char, win_errstr(err)))
 
-#define PyErr_FromHRESULT(err) Py_BuildValue("(i,s)", HRES_ERROR_V(err), discard_const_p(char, hresult_errstr_const(err)))
+#define PyErr_FromHRESULT(err) Py_BuildValue("(k,s)", (unsigned long)(HRES_ERROR_V(err)), discard_const_p(char, hresult_errstr_const(err)))
 
-#define PyErr_FromNTSTATUS(status) Py_BuildValue("(I,s)", NT_STATUS_V(status), discard_const_p(char, get_friendly_nt_error_msg(status)))
+#define PyErr_FromNTSTATUS(status) Py_BuildValue("(k,s)", (unsigned long)(NT_STATUS_V(status)), discard_const_p(char, get_friendly_nt_error_msg(status)))
 
 #define PyErr_FromString(str) Py_BuildValue("(s)", discard_const_p(char, str))
 
 #define PyErr_SetWERROR_and_string(werr, string) \
         PyErr_SetObject(PyObject_GetAttrString(PyImport_ImportModule("samba"),\
                                               "WERRORError"),  \
-                       Py_BuildValue("(i,s)", W_ERROR_V(werr), string))
+                       Py_BuildValue("(k,s)", (unsigned long)(W_ERROR_V(werr)), string))
 
 #define PyErr_SetHRESULT_and_string(hresult, string) \
         PyErr_SetObject(PyObject_GetAttrString(PyImport_ImportModule("samba"),\
                                               "HRESULTError"), \
-                       Py_BuildValue("(i,s)", HRES_ERROR_V(hresult), string))
+                       Py_BuildValue("(k,s)", (unsigned long)(HRES_ERROR_V(hresult)), string))
 
 #define PyErr_SetNTSTATUS_and_string(status, string)                           \
         PyErr_SetObject(PyObject_GetAttrString(PyImport_ImportModule("samba"),\
                                               "NTSTATUSError"),        \
-                       Py_BuildValue("(i,s)", NT_STATUS_V(status), string))
+                       Py_BuildValue("(k,s)", (unsigned long)(NT_STATUS_V(status)), string))
 
 #define PyErr_NTSTATUS_IS_ERR_RAISE(status) \
        if (NT_STATUS_IS_ERR(status)) { \