mirror of
				https://gitlab.freedesktop.org/pipewire/pipewire.git
				synced 2025-11-03 09:01:54 -05:00 
			
		
		
		
	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:
		
							parent
							
								
									5c0a60af27
								
							
						
					
					
						commit
						1bb714b95e
					
				
					 2 changed files with 26 additions and 19 deletions
				
			
		| 
						 | 
				
			
			@ -972,6 +972,7 @@ static int rtsp_setup_reply(void *data, int status, const struct spa_dict *heade
 | 
			
		|||
	size_t len;
 | 
			
		||||
	uint64_t ntp;
 | 
			
		||||
	uint16_t control_port, timing_port;
 | 
			
		||||
	int res;
 | 
			
		||||
 | 
			
		||||
	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;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if (pw_getrandom(&impl->seq, sizeof(impl->seq), 0) < 0 ||
 | 
			
		||||
	    pw_getrandom(&impl->rtptime, sizeof(impl->rtptime), 0) <  0) {
 | 
			
		||||
		pw_log_error("error generating random seq and rtptime: %m");
 | 
			
		||||
	if ((res = pw_getrandom(&impl->seq, sizeof(impl->seq), 0)) < 0 ||
 | 
			
		||||
	    (res = pw_getrandom(&impl->rtptime, sizeof(impl->rtptime), 0)) <  0) {
 | 
			
		||||
		pw_log_error("error generating random seq and rtptime: %s", spa_strerror(res));
 | 
			
		||||
		return 0;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -1258,9 +1259,9 @@ static int rtsp_do_announce(struct impl *impl)
 | 
			
		|||
		break;
 | 
			
		||||
 | 
			
		||||
	case CRYPTO_RSA:
 | 
			
		||||
		if (pw_getrandom(impl->key, sizeof(impl->key), 0) < 0 ||
 | 
			
		||||
		    pw_getrandom(impl->iv, sizeof(impl->iv), 0) < 0)
 | 
			
		||||
			return -errno;
 | 
			
		||||
		if ((res = pw_getrandom(impl->key, sizeof(impl->key), 0)) < 0 ||
 | 
			
		||||
		    (res = pw_getrandom(impl->iv, sizeof(impl->iv), 0)) < 0)
 | 
			
		||||
			return res;
 | 
			
		||||
 | 
			
		||||
		rsa_len = rsa_encrypt(impl->key, 16, rsakey);
 | 
			
		||||
		if (rsa_len < 0)
 | 
			
		||||
| 
						 | 
				
			
			@ -1420,14 +1421,15 @@ static void rtsp_connected(void *data)
 | 
			
		|||
	uint32_t sci[2];
 | 
			
		||||
	uint8_t rac[16];
 | 
			
		||||
	char sac[16*4];
 | 
			
		||||
	int res;
 | 
			
		||||
 | 
			
		||||
	pw_log_info("connected");
 | 
			
		||||
 | 
			
		||||
	impl->connected = true;
 | 
			
		||||
 | 
			
		||||
	if (pw_getrandom(sci, sizeof(sci), 0) < 0 ||
 | 
			
		||||
	    pw_getrandom(rac, sizeof(rac), 0) < 0) {
 | 
			
		||||
		pw_log_error("error generating random data: %m");
 | 
			
		||||
	if ((res = pw_getrandom(sci, sizeof(sci), 0)) < 0 ||
 | 
			
		||||
	    (res = pw_getrandom(rac, sizeof(rac), 0)) < 0) {
 | 
			
		||||
		pw_log_error("error generating random data: %s", spa_strerror(res));
 | 
			
		||||
		return;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -1533,6 +1535,7 @@ static int rtsp_do_connect(struct impl *impl)
 | 
			
		|||
{
 | 
			
		||||
	const char *hostname, *port;
 | 
			
		||||
	uint32_t session_id;
 | 
			
		||||
	int res;
 | 
			
		||||
 | 
			
		||||
	if (impl->connected) {
 | 
			
		||||
		if (!impl->ready)
 | 
			
		||||
| 
						 | 
				
			
			@ -1545,8 +1548,8 @@ static int rtsp_do_connect(struct impl *impl)
 | 
			
		|||
	if (hostname == NULL || port == NULL)
 | 
			
		||||
		return -EINVAL;
 | 
			
		||||
 | 
			
		||||
	if (pw_getrandom(&session_id, sizeof(session_id), 0) < 0)
 | 
			
		||||
		return -errno;
 | 
			
		||||
	if ((res = pw_getrandom(&session_id, sizeof(session_id), 0)) < 0)
 | 
			
		||||
		return res;
 | 
			
		||||
 | 
			
		||||
	spa_scnprintf(impl->session_id, sizeof(impl->session_id), "%u", session_id);
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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)
 | 
			
		||||
{
 | 
			
		||||
	ssize_t bytes;
 | 
			
		||||
	int read_errno;
 | 
			
		||||
 | 
			
		||||
#ifdef HAVE_GETRANDOM
 | 
			
		||||
	bytes = getrandom(buf, buflen, flags);
 | 
			
		||||
	if (!(bytes == -1 && errno == ENOSYS))
 | 
			
		||||
	if (bytes < 0)
 | 
			
		||||
		bytes = -errno;
 | 
			
		||||
	if (bytes != -ENOSYS)
 | 
			
		||||
		return bytes;
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
	int fd = open("/dev/urandom", O_CLOEXEC);
 | 
			
		||||
	if (fd < 0)
 | 
			
		||||
		return -1;
 | 
			
		||||
		return -errno;
 | 
			
		||||
 | 
			
		||||
	bytes = read(fd, buf, buflen);
 | 
			
		||||
	read_errno = errno;
 | 
			
		||||
	if (bytes < 0)
 | 
			
		||||
		bytes = -errno;
 | 
			
		||||
 | 
			
		||||
	close(fd);
 | 
			
		||||
	errno = read_errno;
 | 
			
		||||
 | 
			
		||||
	return bytes;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -190,9 +194,9 @@ ssize_t pw_getrandom(void *buf, size_t buflen, unsigned int flags)
 | 
			
		|||
	ssize_t res;
 | 
			
		||||
	do {
 | 
			
		||||
		res = make_random(buf, buflen, flags);
 | 
			
		||||
	} while ((res == -1) && (errno == EINTR));
 | 
			
		||||
	if (res == -1)
 | 
			
		||||
		return -errno;
 | 
			
		||||
	} while (res == -EINTR);
 | 
			
		||||
	if (res < 0)
 | 
			
		||||
		return res;
 | 
			
		||||
	if ((size_t)res != buflen)
 | 
			
		||||
		return -ENODATA;
 | 
			
		||||
	return res;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue