Skip to content

Commit

Permalink
Handle minimum case separately
Browse files Browse the repository at this point in the history
  • Loading branch information
srinivasreddy committed Jan 23, 2025
1 parent d63235b commit ce8b48e
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions Objects/longobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,15 @@ maybe_small_long(PyLongObject *v)
return _PyLong_FromMedium((sdigit)(ival)); \
} \
/* Count digits (at least two - smaller cases were handled above). */ \
size_t abs_ival, t; /* Use size_t for unsigned operations */ \
size_t abs_ival; /* Use size_t for unsigned operations */ \
size_t t; \
if ((ival) < 0) { \
/* avoid signed overflow when ival = minimum value */ \
abs_ival = (size_t)(-1-(ival))+1; \
/* Handle minimum value case separately to avoid overflow */ \
if ((ival) == -(INT_TYPE)((size_t)1 << (sizeof(INT_TYPE) * 8 - 1))) { \
abs_ival = (size_t)1 << (sizeof(INT_TYPE) * 8 - 1); \
} else { \
abs_ival = (size_t)(-(ival)); \
} \
} else { \
abs_ival = (size_t)(ival); \
} \
Expand Down

0 comments on commit ce8b48e

Please sign in to comment.