From 0bd4c448ec1dfdc2300a6cacca42e1fc7c4d8f14 Mon Sep 17 00:00:00 2001 From: Kirk McKusick Date: Wed, 25 Jan 2023 16:57:26 -0800 Subject: [PATCH] Rewrite to avoid Coverity false positive. MFC after: 1 week Reported by: Coverity (CID 1502669) Sponsored by: The FreeBSD Foundation Differential Revision: https://reviews.freebsd.org/D37907 --- sbin/mount/getmntopts.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/sbin/mount/getmntopts.c b/sbin/mount/getmntopts.c index e6607c38534..7702da90374 100644 --- a/sbin/mount/getmntopts.c +++ b/sbin/mount/getmntopts.c @@ -166,6 +166,7 @@ getmntpoint(const char *name) char *ddevname; struct statfs *mntbuf, *statfsp; int i, mntsize, isdev; + u_long len; if (stat(name, &devstat) != 0) return (NULL); @@ -178,12 +179,13 @@ getmntpoint(const char *name) statfsp = &mntbuf[i]; ddevname = statfsp->f_mntfromname; if (*ddevname != '/') { - if (strlen(_PATH_DEV) + strlen(ddevname) + 1 > - sizeof(statfsp->f_mntfromname)) + if ((len = strlen(_PATH_DEV) + strlen(ddevname) + 1) > + sizeof(statfsp->f_mntfromname) || + len > sizeof(device)) continue; - strcpy(device, _PATH_DEV); - strcat(device, ddevname); - strcpy(statfsp->f_mntfromname, device); + strncpy(device, _PATH_DEV, len); + strncat(device, ddevname, len); + strncpy(statfsp->f_mntfromname, device, len); } if (isdev == 0) { if (strcmp(name, statfsp->f_mntonname))