Article View: news.software.nntp
Article #19739Re: strlen of strncpy (1.6b2)
From: Forrest J. Cavalier III
Date: Mon, 28 Jul 1997 00:00
Date: Mon, 28 Jul 1997 00:00
49 lines
2068 bytes
2068 bytes
Posted and e-mailed.
In article <5rba4b$6e2@fangorn.btv.ibm.com>, fswasey@fangorn.btv.ibm.com
wrote...
>
>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 equi
>
>(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);
> }
[rest of patch, with similar fixes, snipped]
Your patch is equally dangerous. You advance p by the size
of Data.Name, when it is possible that fewer characters were
copied. Your patch fails with the same cases that the strlen(strncpy())
fails.
A simple solution is to place the storage of '\0' BEFORE the
strncpy().
Forrest
Technical Details
Message-ID:
<5rhtgj$rch$1@news1.epix.net>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-mailReferences:
<5rba4b$6e2@fangorn.btv.ibm.com>Show Full Headers
Complete Article Headers
Message-ID: <5rhtgj$rch$1@news1.epix.net> Subject: Re: strlen of strncpy (1.6b2) Date: Mon, 28 Jul 1997 00:00:00 +0000 From: mibsoft@epix.net (Forrest J. Cavalier III) References: <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 X-Deja-AN: 259729887 Organization: Mib Software Newsgroups: news.software.nntp