pipewire: utils: make_random(): do not use errno

The function already returns `ssize_t`, so do not use `errno`
to communicate the reason for failure, instead, return the
negative errno.

`pw_getrandom()` was inconsistent in this regard because
sometimes it simply returned a negative errno without
setting `errno`. This change fixes that as well.
This commit is contained in:
Barnabás Pőcze 2023-07-03 19:02:18 +02:00
parent 5c0a60af27
commit 1bb714b95e
2 changed files with 26 additions and 19 deletions

View file

@ -972,6 +972,7 @@ static int rtsp_setup_reply(void *data, int status, const struct spa_dict *heade
size_t len; size_t len;
uint64_t ntp; uint64_t ntp;
uint16_t control_port, timing_port; uint16_t control_port, timing_port;
int res;
pw_log_info("reply %d", status); pw_log_info("reply %d", status);
@ -1003,9 +1004,9 @@ static int rtsp_setup_reply(void *data, int status, const struct spa_dict *heade
return 0; return 0;
} }
if (pw_getrandom(&impl->seq, sizeof(impl->seq), 0) < 0 || if ((res = pw_getrandom(&impl->seq, sizeof(impl->seq), 0)) < 0 ||
pw_getrandom(&impl->rtptime, sizeof(impl->rtptime), 0) < 0) { (res = pw_getrandom(&impl->rtptime, sizeof(impl->rtptime), 0)) < 0) {
pw_log_error("error generating random seq and rtptime: %m"); pw_log_error("error generating random seq and rtptime: %s", spa_strerror(res));
return 0; return 0;
} }
@ -1258,9 +1259,9 @@ static int rtsp_do_announce(struct impl *impl)
break; break;
case CRYPTO_RSA: case CRYPTO_RSA:
if (pw_getrandom(impl->key, sizeof(impl->key), 0) < 0 || if ((res = pw_getrandom(impl->key, sizeof(impl->key), 0)) < 0 ||
pw_getrandom(impl->iv, sizeof(impl->iv), 0) < 0) (res = pw_getrandom(impl->iv, sizeof(impl->iv), 0)) < 0)
return -errno; return res;
rsa_len = rsa_encrypt(impl->key, 16, rsakey); rsa_len = rsa_encrypt(impl->key, 16, rsakey);
if (rsa_len < 0) if (rsa_len < 0)
@ -1420,14 +1421,15 @@ static void rtsp_connected(void *data)
uint32_t sci[2]; uint32_t sci[2];
uint8_t rac[16]; uint8_t rac[16];
char sac[16*4]; char sac[16*4];
int res;
pw_log_info("connected"); pw_log_info("connected");
impl->connected = true; impl->connected = true;
if (pw_getrandom(sci, sizeof(sci), 0) < 0 || if ((res = pw_getrandom(sci, sizeof(sci), 0)) < 0 ||
pw_getrandom(rac, sizeof(rac), 0) < 0) { (res = pw_getrandom(rac, sizeof(rac), 0)) < 0) {
pw_log_error("error generating random data: %m"); pw_log_error("error generating random data: %s", spa_strerror(res));
return; return;
} }
@ -1533,6 +1535,7 @@ static int rtsp_do_connect(struct impl *impl)
{ {
const char *hostname, *port; const char *hostname, *port;
uint32_t session_id; uint32_t session_id;
int res;
if (impl->connected) { if (impl->connected) {
if (!impl->ready) if (!impl->ready)
@ -1545,8 +1548,8 @@ static int rtsp_do_connect(struct impl *impl)
if (hostname == NULL || port == NULL) if (hostname == NULL || port == NULL)
return -EINVAL; return -EINVAL;
if (pw_getrandom(&session_id, sizeof(session_id), 0) < 0) if ((res = pw_getrandom(&session_id, sizeof(session_id), 0)) < 0)
return -errno; return res;
spa_scnprintf(impl->session_id, sizeof(impl->session_id), "%u", session_id); spa_scnprintf(impl->session_id, sizeof(impl->session_id), "%u", session_id);

View file

@ -158,21 +158,25 @@ char *pw_strip(char *str, const char *whitespace)
static inline ssize_t make_random(void *buf, size_t buflen, unsigned int flags) static inline ssize_t make_random(void *buf, size_t buflen, unsigned int flags)
{ {
ssize_t bytes; ssize_t bytes;
int read_errno;
#ifdef HAVE_GETRANDOM #ifdef HAVE_GETRANDOM
bytes = getrandom(buf, buflen, flags); bytes = getrandom(buf, buflen, flags);
if (!(bytes == -1 && errno == ENOSYS)) if (bytes < 0)
bytes = -errno;
if (bytes != -ENOSYS)
return bytes; return bytes;
#endif #endif
int fd = open("/dev/urandom", O_CLOEXEC); int fd = open("/dev/urandom", O_CLOEXEC);
if (fd < 0) if (fd < 0)
return -1; return -errno;
bytes = read(fd, buf, buflen); bytes = read(fd, buf, buflen);
read_errno = errno; if (bytes < 0)
bytes = -errno;
close(fd); close(fd);
errno = read_errno;
return bytes; return bytes;
} }
@ -190,9 +194,9 @@ ssize_t pw_getrandom(void *buf, size_t buflen, unsigned int flags)
ssize_t res; ssize_t res;
do { do {
res = make_random(buf, buflen, flags); res = make_random(buf, buflen, flags);
} while ((res == -1) && (errno == EINTR)); } while (res == -EINTR);
if (res == -1) if (res < 0)
return -errno; return res;
if ((size_t)res != buflen) if ((size_t)res != buflen)
return -ENODATA; return -ENODATA;
return res; return res;