Re: bin/ln & WARNS=5

Giorgos Keramidas <[email protected]> Mon, 15 Jul 2002 14:14:36 +0300
Newsgroups gmane.os.freebsd.devel.audit
Message-ID <[email protected]>
On 2002-07-15 20:25 +0000, Bruce Evans wrote:
> On 15 Jul 2002, Dag-Erling Smorgrav wrote:
> 
> > Giorgos Keramidas <[email protected]> writes:
> > > The following allows me to build bin/ln with WARNS=5 on i386.
> > > Does it look OK, or have I missed something important?
> >
> > I'd rather cast sizeof to int.
> 
> That would break the possibly-intentional check for snprintf() failing.
> (size_t)-1 >= sizeof(path), but !(-1 >= (int)sizeof(path)).

My intuition was that size_t being unsigned won't require truncation
of the (int) return value...  But if one wanted to explicitly make
both a check for (-1) and the return value being less than the size of
the buffer would the following be more proper?

%%%
Index: ln.c
===================================================================
RCS file: /home/ncvs/src/bin/ln/ln.c,v
retrieving revision 1.28
diff -u -r1.28 ln.c
--- ln.c	30 Jun 2002 05:13:54 -0000	1.28
+++ ln.c	15 Jul 2002 11:12:13 -0000
@@ -163,6 +163,7 @@
 	const char *p;
 	int ch, exists, first;
 	char path[PATH_MAX];
+	int pathlen;
 
 	if (!sflag) {
 		/* If target doesn't exist, quit now. */
@@ -189,8 +190,8 @@
 			p = target;
 		else
 			++p;
-		if (snprintf(path, sizeof(path), "%s/%s", source, p) >=
-		    sizeof(path)) {
+		if ((pathlen = snprintf(path, sizeof(path), "%s/%s",
+		    source, p)) == -1 || pathlen >= (int)sizeof(path)) {
 			errno = ENAMETOOLONG;
 			warn("%s", target);
 			return (1);
%%%


To Unsubscribe: send mail to [email protected]
with "unsubscribe freebsd-audit" in the body of the message