comparison python/pydc/pydc.c @ 62:4a9f6c7c09c1

- fix inccorect overflow errors for int (and long on LLP64 systems)
author Tassilo Philipp
date Sat, 18 May 2024 15:33:54 +0200
parents c5a69c454963
children 9b6cdffd30dd
comparison
equal deleted inserted replaced
61:c5a69c454963 62:4a9f6c7c09c1
234 return PyErr_Format( PyExc_RuntimeError, "arg %d out of range - expecting 0 <= arg <= %d, got %ld", pos, USHRT_MAX, l ); 234 return PyErr_Format( PyExc_RuntimeError, "arg %d out of range - expecting 0 <= arg <= %d, got %ld", pos, USHRT_MAX, l );
235 if (!u && (l < SHRT_MIN || l > SHRT_MAX)) 235 if (!u && (l < SHRT_MIN || l > SHRT_MAX))
236 return PyErr_Format( PyExc_RuntimeError, "arg %d out of range - expecting %d <= arg <= %d, got %ld", pos, SHRT_MIN, SHRT_MAX, l ); 236 return PyErr_Format( PyExc_RuntimeError, "arg %d out of range - expecting %d <= arg <= %d, got %ld", pos, SHRT_MIN, SHRT_MAX, l );
237 237
238 *s = (DCshort)l; 238 *s = (DCshort)l;
239 return po;
240 }
241
242 static inline PyObject* py2dcint(DCint* i, PyObject* po, int u, int pos)
243 {
244 long long ll;
245 if ( !DcPyInt_Check(po) )
246 return PyErr_Format( PyExc_RuntimeError, "arg %d - expecting an int", pos );
247 ll = (DClonglong) PyLong_AsLongLong(po);
248 if (u && (ll < 0 || ll > UINT_MAX))
249 return PyErr_Format( PyExc_RuntimeError, "arg %lld out of range - expecting 0 <= arg <= %ld, got %lld", pos, UINT_MAX, ll );
250 if (!u && (ll < INT_MIN || ll > INT_MAX))
251 return PyErr_Format( PyExc_RuntimeError, "arg %lld out of range - expecting %ld <= arg <= %ld, got %lld", pos, INT_MIN, INT_MAX, ll );
252
253 *i = (DCint)ll;
254 return po;
255 }
256
257 static inline PyObject* py2dclong(DClong* l, PyObject* po, int u, int pos)
258 {
259 long long ll;
260 if ( !DcPyInt_Check(po) )
261 return PyErr_Format( PyExc_RuntimeError, "arg %d - expecting an int", pos );
262 ll = (DClonglong) PyLong_AsLongLong(po);
263 if (u && (ll < 0 || ll > ULONG_MAX)) //@@@ on lp64, this is a bad comparison
264 return PyErr_Format( PyExc_RuntimeError, "arg %lld out of range - expecting 0 <= arg <= %ld, got %lld", pos, ULONG_MAX, ll );
265 if (!u && (ll < LONG_MIN || ll > LONG_MAX))
266 return PyErr_Format( PyExc_RuntimeError, "arg %lld out of range - expecting %ld <= arg <= %ld, got %lld", pos, LONG_MIN, LONG_MAX, ll );
267
268 *l = (DClong)ll;
239 return po; 269 return po;
240 } 270 }
241 271
242 static inline PyObject* py2dclonglong(DClonglong* ll, PyObject* po, int pos) 272 static inline PyObject* py2dclonglong(DClonglong* ll, PyObject* po, int pos)
243 { 273 {
388 } 418 }
389 break; 419 break;
390 420
391 case DC_SIGCHAR_INT: 421 case DC_SIGCHAR_INT:
392 case DC_SIGCHAR_UINT: 422 case DC_SIGCHAR_UINT:
393 if ( !DcPyInt_Check(po) ) 423 {
394 return PyErr_Format( PyExc_RuntimeError, "arg %d - expecting an int", pos ); 424 DCint i;
395 dcArgInt(gpCall, (DCint) DcPyInt_AS_LONG(po)); 425 if(!py2dcint(&i, po, ch == DC_SIGCHAR_UINT, pos))
396 break; 426 return NULL;
427 dcArgInt(gpCall, i);
428 }
397 429
398 case DC_SIGCHAR_LONG: 430 case DC_SIGCHAR_LONG:
399 case DC_SIGCHAR_ULONG: 431 case DC_SIGCHAR_ULONG:
400 if ( !DcPyInt_Check(po) ) 432 {
401 return PyErr_Format( PyExc_RuntimeError, "arg %d - expecting an int", pos ); 433 DClong l;
402 dcArgLong(gpCall, (DClong) PyLong_AsLong(po)); 434 if(!py2dclong(&l, po, ch == DC_SIGCHAR_ULONG, pos))
403 break; 435 return NULL;
436 dcArgLong(gpCall, l);
437 }
404 438
405 case DC_SIGCHAR_LONGLONG: 439 case DC_SIGCHAR_LONGLONG:
406 case DC_SIGCHAR_ULONGLONG: 440 case DC_SIGCHAR_ULONGLONG:
407 { 441 {
408 DClonglong ll; 442 DClonglong ll;