Index | Thread | Search

From:
"Omar Polo" <op@omarpolo.com>
Subject:
Re: replace 2 last instances of strncpy with strlcpy
To:
Han Boetes <hboetes@gmail.com>
Cc:
tech@openbsd.org
Date:
Tue, 24 Feb 2026 23:36:30 +0100

Download raw body.

Thread
Hello,

Han Boetes <hboetes@gmail.com> wrote:
> I would also like to propose this minor diff.

On this one instead I'm a little bit on the fence.

I think that strncpy in this case is a bit better than strlcpy because
we're copying a string NOT until the NUL terminator at the end.  There
is also the needed bounds checking on the destination buffer.

strlcpy instead is *generally* used to copy the whole string into a
different buffer.

Now, the way you're using strlcpy() would produce the same effect, but I
think that using strncpy here is slightly more clear.

> BR
> Han
> 

(i'm reattaching the diff, as it was mangled, just for the archive)

diff /home/op/w/mg
path + /home/op/w/mg
commit - bb4e7911d276b83dbf547938ad8bc12ff94c2d0e
blob - e4a539de6caf5bcea204637f6d994b043726ef1a
file + extend.c
--- extend.c
+++ extend.c
@@ -600,10 +600,8 @@ evalbuffer(int f, int n)
 		llen = llength(lp);
 		if (llen >= BUFSIZE)
 			return (FALSE);
-		(void)strncpy(excbuf, ltext(lp), llen);
+		(void)strlcpy(excbuf, ltext(lp), llen + 1);
 
-		/* make sure the line is terminated */
-		excbuf[llen] = '\0';
 		if ((s = excline(excbuf, llen, lnum)) != TRUE) {
 			cleanup();
 			return (s);
commit - bb4e7911d276b83dbf547938ad8bc12ff94c2d0e
blob - 8c9d797334d728c729efb261637974c9f66f9a60
file + tags.c
--- tags.c
+++ tags.c
@@ -466,8 +466,7 @@ curtoken(int f, int n, char *token)
 		r = FALSE;
 		goto cleanup;
 	}
-	strncpy(token, ltext(curwp->w_dotp) + tdoto, size);
-	token[size] = '\0';
+	strlcpy(token, ltext(curwp->w_dotp) + tdoto, size + 1);
 	r = TRUE;
 
 cleanup: