Skip to content

Commit

Permalink
Better size handling in Tuple.operator *
Browse files Browse the repository at this point in the history
  • Loading branch information
GrieferAtWork committed Nov 24, 2023
1 parent 8d66d2d commit b6b9829
Showing 1 changed file with 15 additions and 11 deletions.
26 changes: 15 additions & 11 deletions src/deemon/objects/tuple.c
Original file line number Diff line number Diff line change
Expand Up @@ -2004,30 +2004,34 @@ tuple_repeat(Tuple *self, DeeObject *other) {
DREF DeeObject **dst;
if (DeeObject_AsSize(other, &count))
goto err;
if (!count)
goto return_empty;
if (count == 1)
return_reference_(self);

/* Repeat `self' `count' number of times. */
my_length = DeeTuple_SIZE(self);
if (my_length == 0)
goto return_empty;
if (OVERFLOW_UMUL(my_length, count, &total_length))
goto err_overflow;
if unlikely(total_length == 0)
goto return_empty;
result = DeeTuple_NewUninitialized(total_length);
if unlikely(!result)
goto err;

/* Create all the new references that will be contained in the new tuple. */
for (i = 0; i < my_length; ++i)
Dee_Incref_n(DeeTuple_GET(self, i), count);

/* Fill in the resulting tuple with repetitions of ourself. */
dst = DeeTuple_ELEM(result);
while (count--) {
dst = (DREF DeeObject **)mempcpyc(dst, DeeTuple_ELEM(self),
my_length, sizeof(DREF DeeObject *));
#ifndef __OPTIMIZE_SIZE__
if (my_length == 1) {
Dee_Setrefv(dst, DeeTuple_GET(self, 0), count);
} else
#endif /* !__OPTIMIZE_SIZE__ */
{
/* Create all the new references that will be contained in the new tuple. */
for (i = 0; i < my_length; ++i)
Dee_Incref_n(DeeTuple_GET(self, i), count);
while (count--) {
dst = (DREF DeeObject **)mempcpyc(dst, DeeTuple_ELEM(self),
my_length, sizeof(DREF DeeObject *));
}
}
return result;
return_empty:
Expand Down

0 comments on commit b6b9829

Please sign in to comment.