Skip to content

Commit

Permalink
Merge pull request #663 from smcv/builtin-overflow
Browse files Browse the repository at this point in the history
utils: Add a fallback version of xadd, xmul for ancient gcc
  • Loading branch information
smcv authored Oct 17, 2024
2 parents c1bfc72 + bcd9614 commit 759517f
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -959,10 +959,17 @@ mount_strerror (int errsv)
static size_t
xadd (size_t a, size_t b)
{
#if defined(__GNUC__) && __GNUC__ >= 5
size_t result;
if (__builtin_add_overflow (a, b, &result))
die_oom ();
return result;
#else
if (a > SIZE_MAX - b)
die_oom ();

return a + b;
#endif
}

/*
Expand All @@ -972,10 +979,17 @@ xadd (size_t a, size_t b)
static size_t
xmul (size_t a, size_t b)
{
#if defined(__GNUC__) && __GNUC__ >= 5
size_t result;
if (__builtin_mul_overflow (a, b, &result))
die_oom ();
return result;
#else
if (b != 0 && a > SIZE_MAX / b)
die_oom ();

return a * b;
#endif
}

void
Expand Down

0 comments on commit 759517f

Please sign in to comment.