🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: news.software.nntp
Article #19671

strlen of strncpy (1.6b2)

#19671
From: Francis C. Swasey
Date: Fri, 25 Jul 1997 00:00
255 lines
10212 bytes
It was pointed out by Forrest J. Cavalier III yesterday afternoon that
code such as

p += strlen(strncpy(p, "running", sizeof(buff) - strlen(buff) - 1));

is dangerous.  Since strncpy is not guaranteed to null terminate the string it returns, you cannot be sure that (in the above case) you are doing the equivalent of:

(void)strncpy(p, "running", sizeof(buff) - strlen(buff) - 1);
p += strlne("running");

So, here's a patch for the four .c files that have this problem:

-------------------8<--------------------8<-------------------8<-----------
--- innd/art.c.orig	Wed Jul 23 13:09:28 1997
+++ innd/art.c	Fri Jul 25 18:28:44 1997
@@ -2652,7 +2652,8 @@
 		ARTreject(buff, article);
 		return buff;
 	    }
-	    p += strlen(strncpy(p, Data.Name, Files.Size - 1));
+	    (void)strncpy(p, Data.Name, Files.Size - 1);
+	    p += strlen(Data.Name);
 	    Files.Data[Files.Size - 1] = '\0';
 	    Data.NameLength = strlen(Data.Name);
 	}
--- innd/cc.c.orig	Wed Jul 23 13:09:28 1997
+++ innd/cc.c	Fri Jul 25 18:29:51 1997
@@ -801,32 +801,40 @@
 	p += strlen(p);
 	break;
     case OMrunning:
-	p += strlen(strncpy(p, "running", sizeof(buff) - strlen(buff) - 1));
+	(void)strncpy(p, "running", sizeof(buff) - strlen(buff) - 1);
+	p += strlen("running");
 	buff[sizeof(buff) - 1] = '\0';
 	break;
     case OMpaused:
-	p += strlen(strncpy(p, "paused ", sizeof(buff) - strlen(buff) - 1));
+	(void)strncpy(p, "paused ", sizeof(buff) - strlen(buff) - 1);
+	p += strlen("paused ");
 	buff[sizeof(buff) - 1] = '\0';
-	p += strlen(strncpy(p, ModeReason, sizeof(buff) - strlen(buff) - 1));
+	(void)strncpy(p, ModeReason, sizeof(buff) - strlen(buff) - 1);
+	p += strlen(ModeReason);
 	buff[sizeof(buff) - 1] = '\0';
 	break;
     case OMthrottled:
-	p += strlen(strncpy(p, "throttled ", sizeof(buff) - strlen(buff) - 1));
+	(void)strncpy(p, "throttled ", sizeof(buff) - strlen(buff) - 1);
+	p += strlen("throttled ");
 	buff[sizeof(buff) - 1] = '\0';
-	p += strlen(strncpy(p, ModeReason, sizeof(buff) - strlen(buff) - 1));
+	(void)strncpy(p, ModeReason, sizeof(buff) - strlen(buff) - 1);
+	p += strlen(ModeReason);
 	buff[sizeof(buff) - 1] = '\0';
 	break;
     }
     *p++ = '\n';
     if (RejectReason) {
-	p += strlen(strncpy(p, "Rejecting ", sizeof(buff) - strlen(buff) - 1));
+	(void)strncpy(p, "Rejecting ", sizeof(buff) - strlen(buff) - 1);
+	p += strlen("Rejecting ");
 	buff[sizeof(buff) - 1] = '\0';
-	p += strlen(strncpy(p, RejectReason, sizeof(buff) - strlen(buff) - 1));
+	(void)strncpy(p, RejectReason, sizeof(buff) - strlen(buff) - 1);
+	p += strlen(RejectReason);
 	buff[sizeof(buff) - 1] = '\0';
     }
     else {
-	p += strlen(strncpy(p, "Allowing remote connections",
-				sizeof(buff) - strlen(buff) - 1));
+	(void)strncpy(p, "Allowing remote connections",
+				sizeof(buff) - strlen(buff) - 1);
+	p += strlen("Allowing remote connections");
 	buff[sizeof(buff) - 1] = '\0';
     }
 
@@ -852,23 +860,28 @@
 	p += strlen(p);
     }
     else {
-	p += strlen(strncpy(p, "Not reserved", sizeof(buff)-strlen(buff)-1));
+	(void)strncpy(p, "Not reserved", sizeof(buff)-strlen(buff)-1);
+	p += strlen("Not reserved");
 	buff[sizeof(buff) - 1] = '\0';
     }
 
     /* Newsreaders. */
     *p++ = '\n';
-    p += strlen(strncpy(p, "Readers ", sizeof(buff) - strlen(buff) - 1));
+    (void)strncpy(p, "Readers ", sizeof(buff) - strlen(buff) - 1);
+    p += strlen("Readers ");
     buff[sizeof(buff) - 1] = '\0';
     if (NNRPFollows) {
-	p += strlen(strncpy(p, "follow ", sizeof(buff) - strlen(buff) - 1));
+	(void)strncpy(p, "follow ", sizeof(buff) - strlen(buff) - 1);
+	p += strlen("follow ");
 	buff[sizeof(buff) - 1] = '\0';
     } else {
-	p += strlen(strncpy(p, "separate ", sizeof(buff) - strlen(buff) - 1));
+	(void)strncpy(p, "separate ", sizeof(buff) - strlen(buff) - 1);
+	p += strlen("separate ");
 	buff[sizeof(buff) - 1] = '\0';
     }
     if (NNRPReason == NULL) {
-	p += strlen(strncpy(p, "enabled", sizeof(buff) - strlen(buff) - 1));
+	(void)strncpy(p, "enabled", sizeof(buff) - strlen(buff) - 1);
+	p += strlen("enabled");
 	buff[sizeof(buff) - 1] = '\0';
     } else {
 	(void)snprintf(p, sizeof(buff) - strlen(buff),
@@ -878,25 +891,31 @@
 
 #if defined(DO_TCL)
     *p++ = '\n';
-    p += strlen(strncpy(p, "Tcl filtering ", sizeof(buff) - strlen(buff) - 1));
+    (void)strncpy(p, "Tcl filtering ", sizeof(buff) - strlen(buff) - 1);
+    p += strlen("Tcl filtering ");
     buff[sizeof(buff) - 1] = '\0';
     if (TCLFilterActive) {
-	p += strlen(strncpy(p, "enabled", sizeof(buff) - strlen(buff) - 1));
+	(void)strncpy(p, "enabled", sizeof(buff) - strlen(buff) - 1);
+	p += strlen("enabled");
 	buff[sizeof(buff) - 1] = '\0';
     } else {
-	p += strlen(strncpy(p, "disabled", sizeof(buff) - strlen(buff) - 1));
+	(void)strncpy(p, "disabled", sizeof(buff) - strlen(buff) - 1);
+	p += strlen("disabled");
 	buff[sizeof(buff) - 1] = '\0';
     }
 #endif /* defined(DO_TCL) */
 #if defined(DO_PERL)
     *p++ = '\n';
-    p += strlen(strncpy(p, "Perl filtering ", sizeof(buff) - strlen(buff) - 1));
+    (void)strncpy(p, "Perl filtering ", sizeof(buff) - strlen(buff) - 1);
+    p += strlen("Perl filtering ");
     buff[sizeof(buff) - 1] = '\0';
     if (PerlFilterActive) {
-        p += strlen(strncpy(p, "enabled", sizeof(buff) - strlen(buff) - 1));
+        (void)strncpy(p, "enabled", sizeof(buff) - strlen(buff) - 1);
+        p += strlen("enabled");
 	buff[sizeof(buff) - 1] = '\0';
     } else {
-        p += strlen(strncpy(p, "disabled", sizeof(buff) - strlen(buff) - 1));
+        (void)strncpy(p, "disabled", sizeof(buff) - strlen(buff) - 1);
+        p += strlen("disabled");
 	buff[sizeof(buff) - 1] = '\0';
     }
 #endif /* defined(DO_PERL) */
--- innd/site.c.orig	Wed Jul 23 13:09:28 1997
+++ innd/site.c	Fri Jul 25 18:24:55 1997
@@ -1173,7 +1173,8 @@
 	break;
     case FTerror:
     case FTfile:
-	p += strlen(strncpy(p, "file", sizeof(buff) - strlen(buff) - 1));
+	(void)strncpy(p, "file", sizeof(buff) - strlen(buff) - 1);
+	p += strlen("file");
 	buff[sizeof(buff) - 1] = '\0';
 	if (sp->Buffered) {
 	    (void)snprintf(p, sizeof(buff) - strlen(buff),
@@ -1181,8 +1182,8 @@
 	    p += strlen(p);
 	}
 	else if ((cp = sp->Channel) == NULL) {
-	    p += strlen(strncpy(p, " no channel?",
-			sizeof(buff) - strlen(buff) - 1));
+	    (void)strncpy(p, " no channel?", sizeof(buff) - strlen(buff) - 1);
+	    p += strlen(" no channel?");
 	    buff[sizeof(buff) - 1] = '\0';
 	} else {
 	    (void)snprintf(p, sizeof(buff) - strlen(buff),
@@ -1191,11 +1192,13 @@
 	}
 	break;
     case FTchannel:
-	p += strlen(strncpy(p, "channel", sizeof(buff) - strlen(buff) - 1));
+	(void)strncpy(p, "channel", sizeof(buff) - strlen(buff) - 1);
+	p += strlen("channel");
 	buff[sizeof(buff) - 1] = '\0';
 	goto Common;
     case FTexploder:
-	p += strlen(strncpy(p, "exploder", sizeof(buff) - strlen(buff) - 1));
+	(void)strncpy(p, "exploder", sizeof(buff) - strlen(buff) - 1);
+	p += strlen("exploder");
 	buff[sizeof(buff) - 1] = '\0';
 Common:
 	if (sp->Process >= 0) {
@@ -1204,12 +1207,13 @@
 	    p += strlen(p);
 	}
 	if (sp->Spooling) {
-	    p += strlen(strncpy(p, " spooling", sizeof(buff)-strlen(buff)-1));
+	    (void)strncpy(p, " spooling", sizeof(buff)-strlen(buff)-1);
+	    p += strlen(" spooling");
 	    buff[sizeof(buff) - 1] = '\0';
 	}
 	if ((cp = sp->Channel) == NULL) {
-	    p += strlen(strncpy(p, " no channel?",
-			sizeof(buff) - strlen(buff) - 1));
+	    (void)strncpy(p, " no channel?", sizeof(buff) - strlen(buff) - 1);
+	    p += strlen(" no channel?");
 	    buff[sizeof(buff) - 1] = '\0';
 	} else {
 	    (void)snprintf(p, sizeof(buff) - strlen(buff),
@@ -1218,19 +1222,22 @@
 	}
 	break;
     case FTfunnel:
-	p += strlen(strncpy(p,"recursive funnel",sizeof(buff)-strlen(buff)-1));
+	(void)strncpy(p,"recursive funnel",sizeof(buff)-strlen(buff)-1);
+	p += strlen("recursive funnel");
 	buff[sizeof(buff) - 1] = '\0';
 	break;
     case FTlogonly:
-	p += strlen(strncpy(p, "log only", sizeof(buff) - strlen(buff) - 1));
+	(void)strncpy(p, "log only", sizeof(buff) - strlen(buff) - 1);
+	p += strlen("log only");
 	buff[sizeof(buff) - 1] = '\0';
 	break;
     case FTprogram:
-	p += strlen(strncpy(p, "program", sizeof(buff) - strlen(buff) - 1));
+	(void)strncpy(p, "program", sizeof(buff) - strlen(buff) - 1);
+	p += strlen("program");
 	buff[sizeof(buff) - 1] = '\0';
 	if (sp->FNLwantsnames) {
-	    p += strlen(strncpy(p, " with names",
-				sizeof(buff) - strlen(buff) - 1));
+	    (void)strncpy(p, " with names", sizeof(buff) - strlen(buff) - 1);
+	    p += strlen(" with names");
 	    buff[sizeof(buff) - 1] = '\0';
 	}
 	break;
--- lib/inndcomm.c.orig	Wed Jul 23 13:09:28 1997
+++ lib/inndcomm.c	Fri Jul 25 18:25:23 1997
@@ -234,7 +234,8 @@
     for (p = buff + strlen(buff), i = 0; (q = argv[i]) != NULL; i++) {
 	*p++ = SC_SEP;
         *p = '\0'; /* make sure buff is null-terminated so strlen below will work! */
-        p += strlen(strncpy(p, q, bufsiz - strlen(buff) - 1 - HEADER_SIZE));
+        (void)strncpy(p, q, bufsiz - strlen(buff) - 1 - HEADER_SIZE);
+        p += strlen(q);
         buff[bufsiz - 1 - HEADER_SIZE] = '\0'; 
     }
 
-------------------8<--------------------8<-------------------8<-----------

Frank
-- 
e-mail: fswasey@btv.ibm.com                                   ,,,
Tel: 802-769-4011 (tie: 446), Fax: 802-769-4253 (tie: 446)   (o o)
           Speaking *from* but not *for* IBM           -oOOO--(_)--OOOo-

Technical Details
Message-ID: <5rba4b$6e2@fangorn.btv.ibm.com>
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!illumos!news.pugleaf.net!.TX!archive.pugleaf.net!archive.newsdeef.eu!mbox2nntp-news.software.nntp.mbox.zip!not-for-mail

Show Full Headers
Complete Article Headers
Message-ID: <5rba4b$6e2@fangorn.btv.ibm.com>
Subject: strlen of strncpy (1.6b2)
Date: Fri, 25 Jul 1997 00:00:00 +0000
From: fswasey@fangorn.btv.ibm.com (Francis C. Swasey)
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!illumos!news.pugleaf.net!.TX!archive.pugleaf.net!archive.newsdeef.eu!mbox2nntp-news.software.nntp.mbox.zip!not-for-mail
X-Deja-AN: 258984304
Organization: International Business Machines
Reply-To: fswasey@btv.ibm.com
Newsgroups: news.software.nntp