On Thu, Apr 23, 2026 at 09:40:13PM -0400, Kurt Hackenberg wrote:
On Fri, Apr 24, 2026 at 09:28 +0800, Kevin J. McCarthy wrote:

if (res < length_requested)
{
  length_requested -= res;
  random_bytes += res;
}

I hope that code is not executed if the return value is -1.

Yup. That's exactly what I meant by making the code clearer. If res is type size_t, then it can't be -1 in that comparison.

I'm asking: is that code executed when the return value is -1? If not, then we don't have to care whether it works in that error case.

Ah, sorry.  Yes, the code is executed.  The whole snippet is:

  if ((res == (size_t) -1) || (res < length_requested))
  {
    if (res < length_requested)
    {
      length_requested -= res;
      random_bytes += res;
    }
    prng_random_bytes(random_bytes, length_requested);
  }

So, when res was size_t, I explicitly checked for the -1 retval via a cast. For the inner check "if (res < length_requested)", because it's a size_t, the res will be the maximum positive value, and can't be < length_requested. So that inner check will only execute when the number of bytes returned was truncated.

IMO, I think the sacrifice of the (size_t)-1 cast checks make the rest of the code clearer...

--
Kevin J. McCarthy
GPG Fingerprint: 8975 A9B3 3AA3 7910 385C  5308 ADEF 7684 8031 6BDA

Attachment: signature.asc
Description: PGP signature

Reply via email to