git: 8f0498208db6 - main - bhyve/audio.c: avoid re-calculating the length of dev_name
- Go to: [ bottom of page ] [ top of archives ] [ this month ]
Date: Tue, 26 Dec 2023 17:48:21 UTC
The branch main has been updated by imp:
URL: https://cgit.FreeBSD.org/src/commit/?id=8f0498208db67786d4a60982c0e9ae42e8ebd26b
commit 8f0498208db67786d4a60982c0e9ae42e8ebd26b
Author: rilysh <nightquick@proton.me>
AuthorDate: 2023-12-26 16:24:04 +0000
Commit: Warner Losh <imp@FreeBSD.org>
CommitDate: 2023-12-26 16:36:31 +0000
bhyve/audio.c: avoid re-calculating the length of dev_name
In the function audio_init(), strlen() is being called two times,
first to get the length of dev_name and second to use in memcpy().
Creating a local variable and keeping the length avoids this
re-calculation.
Signed-off-by: rilysh <nightquick@proton.me>
Reviewed by: imp, zlei
Pull Request: https://github.com/freebsd/freebsd-src/pull/945
---
usr.sbin/bhyve/audio.c | 6 ++++--
1 file changed, 4 insertions(+), 2 deletions(-)
diff --git a/usr.sbin/bhyve/audio.c b/usr.sbin/bhyve/audio.c
index fc9ded37044a..4613f3583116 100644
--- a/usr.sbin/bhyve/audio.c
+++ b/usr.sbin/bhyve/audio.c
@@ -80,6 +80,7 @@ audio_init(const char *dev_name, uint8_t dir)
#endif
};
#endif
+ size_t nlen;
assert(dev_name);
@@ -87,8 +88,9 @@ audio_init(const char *dev_name, uint8_t dir)
if (!aud)
return NULL;
- if (strlen(dev_name) < sizeof(aud->dev_name))
- memcpy(aud->dev_name, dev_name, strlen(dev_name) + 1);
+ nlen = strlen(dev_name);
+ if (nlen < sizeof(aud->dev_name))
+ memcpy(aud->dev_name, dev_name, nlen + 1);
else {
DPRINTF("dev_name too big");
free(aud);