bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: UBSAN error in lib/sh/random.c:79


From: Greg Wooledge
Subject: Re: UBSAN error in lib/sh/random.c:79
Date: Tue, 10 Jan 2023 11:43:16 -0500

On Sat, Jan 07, 2023 at 01:42:20PM -0500, Greg Wooledge wrote:
> Or should the code do the multiplications with unsigned
> values, store them in unsigned variables, and then replace the subtraction
> with some kind of conditional that checks which of the two is greater?

Here's a version that does just that:

static u_bits32_t
intrand32 (last)
     u_bits32_t last;
{
  u_bits32_t h, l, t1, t2, ret;

  ret = (last == 0) ? 123459876 : last;
  h = ret / 127773;
  l = ret - (127773 * h);
  t1 = 16807 * l;
  t2 = 2836 * h;
  if (t1 < t2)
    ret = 0x7fffffff - t2 + t1;
  else
    ret = t1 - t2;

  return ret;
}

It passes the implementation test in the paper (checking the seed
after 10000 iterations), and I believe it's free from any kind of
overflow.

Do with it as you like.  Feel free to replace the if/else with a
ternary ?: operator if that's your preference.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]