utils: pw_getrandom() use errno from read() call

The errno from read is more useful than the one from close() clobbering
it. So make sure to preserve the real reason for failure for the caller.
This commit is contained in:
Thomas Weißschuh 2022-08-19 08:25:33 +02:00 committed by Wim Taymans
parent 6531bedcdb
commit 987069cc3c

View file

@ -154,6 +154,7 @@ SPA_EXPORT
ssize_t pw_getrandom(void *buf, size_t buflen, unsigned int flags)
{
ssize_t bytes;
int read_errno;
#ifdef HAVE_GETRANDOM
bytes = getrandom(buf, buflen, flags);
@ -165,7 +166,9 @@ ssize_t pw_getrandom(void *buf, size_t buflen, unsigned int flags)
if (fd < 0)
return -1;
bytes = read(fd, buf, buflen);
read_errno = errno;
close(fd);
errno = read_errno;
return bytes;
}