Re: [PATCH] Improve mutt's random data generator.

"Kevin J. McCarthy" <[email protected]>
Newsgroups gmane.mail.mutt.devel
Message-ID <aerL72UYu5Rg9tXo@qinghai>
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
signature.asc (application/pgp-signature, 833 B)
-----BEGIN PGP SIGNATURE-----

iQIzBAEBCgAdFiEEiXWpszqjeRA4XFMIre92hIAxa9oFAmnqy+8ACgkQre92hIAx
a9qEnxAA54RQuPVHTfEdje8BdDEUZt+b5/T0cX8kHqyYzfCjmhF1MybTUB2glXae
aETdwX0++qtDA8B+IMLRWevEgEmPBzCtgVqkpt3VvVNcBe9KPIUDf75SF0q8hys2
ihT6vx3gy6GS3eR7OYTjKfQM+xthyUmYFlGrZkq97Fbf1+Ifk5cISlvZgDpBiF2i
8q5ejTJQ18VFVHfTuLtSsNx6ejNy0ocNW6oj5HkBhYikx9NyO8zAqpedsW0iH1pG
bXl07MSrGS4BdLZfNCbQxeVGQgUgui834lIWyhAUafB4EDC73fSFYWAWAUV1vg/h
ikO6Volp8JCAde0ItrBF8t7TPjrv8KhV8ko6eeRdih/wjdBdQOxybicIA26cMlOB
5LrRIU3kTu+oBIwgnWoEdDN8ouABQiwlz3eturV1/6/X1dchEcTprZT5mgnummxI
jUVNd5UtQntnp1WXo97LG1qBVvxwqhzsur8oCvNC0dGvQ2SGYyj8ssiyIGJFnOJK
MhHs3ve7WvEf9Lbs5ic40HRRYynMM9nmbMK5rAplw49th7yAo765tVXpgzTSEecI
VU9LHO1u5ce4bKqE+jYzpxyGcE106vbA/3DUKxdQA7k12QuO9O3JQIcZzm9YuTLo
xioqzBqe3dkyjYZDK6H59RiomOP9M/6rovvOdwx/H4bXlQ10Op4=
=TMKs
-----END PGP SIGNATURE-----
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.