git: ee83710bc492 - main - bhyve: Address compiler warnings in audio.c

From: Mark Johnston <markj_at_FreeBSD.org>
Date: Thu, 29 Sep 2022 16:37:43 UTC
The branch main has been updated by markj:

URL: https://cgit.FreeBSD.org/src/commit/?id=ee83710bc492c0b2e7d66dd61f45fa657a14d169

commit ee83710bc492c0b2e7d66dd61f45fa657a14d169
Author:     Mark Johnston <markj@FreeBSD.org>
AuthorDate: 2022-09-08 22:48:53 +0000
Commit:     Mark Johnston <markj@FreeBSD.org>
CommitDate: 2022-09-29 16:36:44 +0000

    bhyve: Address compiler warnings in audio.c
    
    - Avoid arithmetic on void pointers.
    - Avoid a signed/unsigned comparison in loops which write or fill audio
      data buffers.
    
    Convert while loops to for loops while here.
    
    MFC after:      2 weeks
---
 usr.sbin/bhyve/audio.c | 28 ++++++++++++----------------
 usr.sbin/bhyve/audio.h |  4 ++--
 2 files changed, 14 insertions(+), 18 deletions(-)

diff --git a/usr.sbin/bhyve/audio.c b/usr.sbin/bhyve/audio.c
index ee6bdabc541c..165face5862c 100644
--- a/usr.sbin/bhyve/audio.c
+++ b/usr.sbin/bhyve/audio.c
@@ -221,10 +221,11 @@ audio_set_params(struct audio *aud, struct audio_params *params)
  * @count - the number of bytes in buffer
  */
 int
-audio_playback(struct audio *aud, const void *buf, size_t count)
+audio_playback(struct audio *aud, const uint8_t *buf, size_t count)
 {
-	int audio_fd = -1;
-	ssize_t len = 0, total = 0;
+	ssize_t len;
+	size_t total;
+	int audio_fd;
 
 	assert(aud);
 	assert(aud->dir);
@@ -233,16 +234,13 @@ audio_playback(struct audio *aud, const void *buf, size_t count)
 	audio_fd = aud->fd;
 	assert(audio_fd != -1);
 
-	total = 0;
-	while (total < count) {
+	for (total = 0; total < count; total += len) {
 		len = write(audio_fd, buf + total, count - total);
-		if (len == -1) {
+		if (len < 0) {
 			DPRINTF("Fail to write to fd: %d, errno: %d",
 			    audio_fd, errno);
 			return -1;
 		}
-
-		total += len;
 	}
 
 	return 0;
@@ -257,10 +255,11 @@ audio_playback(struct audio *aud, const void *buf, size_t count)
  * Returns -1 on error and 0 on success
  */
 int
-audio_record(struct audio *aud, void *buf, size_t count)
+audio_record(struct audio *aud, uint8_t *buf, size_t count)
 {
-	int audio_fd = -1;
-	ssize_t len = 0, total = 0;
+	ssize_t len;
+	size_t total;
+	int audio_fd;
 
 	assert(aud);
 	assert(!aud->dir);
@@ -269,16 +268,13 @@ audio_record(struct audio *aud, void *buf, size_t count)
 	audio_fd = aud->fd;
 	assert(audio_fd != -1);
 
-	total = 0;
-	while (total < count) {
+	for (total = 0; total < count; total += len) {
 		len = read(audio_fd, buf + total, count - total);
-		if (len == -1) {
+		if (len < 0) {
 			DPRINTF("Fail to write to fd: %d, errno: %d",
 			    audio_fd, errno);
 			return -1;
 		}
-
-		total += len;
 	}
 
 	return 0;
diff --git a/usr.sbin/bhyve/audio.h b/usr.sbin/bhyve/audio.h
index 88f4dc8709c4..143030b29ae7 100644
--- a/usr.sbin/bhyve/audio.h
+++ b/usr.sbin/bhyve/audio.h
@@ -73,7 +73,7 @@ int audio_set_params(struct audio *aud, struct audio_params *params);
  * @count - the number of bytes in buffer
  * Returns -1 on error and 0 on success
  */
-int audio_playback(struct audio *aud, const void *buf, size_t count);
+int audio_playback(struct audio *aud, const uint8_t *buf, size_t count);
 
 /*
  * audio_record - records samples from the sound device using blocking
@@ -83,6 +83,6 @@ int audio_playback(struct audio *aud, const void *buf, size_t count);
  * @count - the number of bytes to capture in buffer
  * Returns -1 on error and 0 on success
  */
-int audio_record(struct audio *aud, void *buf, size_t count);
+int audio_record(struct audio *aud, uint8_t *buf, size_t count);
 
 #endif  /* _AUDIO_EMUL_H_ */