From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.org/gmane.linux.lib.musl.general/9997 Path: news.gmane.org!not-for-mail From: Joakim Sindholt Newsgroups: gmane.linux.lib.musl.general Subject: nftw miscalculates FTW.base when pathnames end in / Date: Wed, 04 May 2016 15:42:19 +0200 Message-ID: <1462369339.29574.1@mail.zhasha.com> Reply-To: musl@lists.openwall.com NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-vP9c9wUezlxxxN3yItoZ" X-Trace: ger.gmane.org 1462369372 26978 80.91.229.3 (4 May 2016 13:42:52 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Wed, 4 May 2016 13:42:52 +0000 (UTC) To: musl@lists.openwall.com Original-X-From: musl-return-10010-gllmg-musl=m.gmane.org@lists.openwall.com Wed May 04 15:42:40 2016 Return-path: Envelope-to: gllmg-musl@m.gmane.org Original-Received: from mother.openwall.net ([195.42.179.200]) by plane.gmane.org with smtp (Exim 4.69) (envelope-from ) id 1axx4Z-0004yP-R1 for gllmg-musl@m.gmane.org; Wed, 04 May 2016 15:42:39 +0200 Original-Received: (qmail 3383 invoked by uid 550); 4 May 2016 13:42:37 -0000 Mailing-List: contact musl-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-ID: Original-Received: (qmail 3306 invoked from network); 4 May 2016 13:42:31 -0000 X-Mailer: geary/0.10.0 Xref: news.gmane.org gmane.linux.lib.musl.general:9997 Archived-At: --=-vP9c9wUezlxxxN3yItoZ Content-Type: text/plain; charset=utf-8; format=flowed Running the test program from the glibc ftw man page[1]: +zhasha@wirbelwind /home/zhasha ; ./6.out test d 0 4096 test 0 test d 1 4096 test/a 5 a f 2 0 test/a/1 7 1 d 1 4096 test/b 5 b f 2 0 test/b/1 7 1 f 2 0 test/b/2 7 2 d 1 4096 test/ddd 5 ddd f 2 0 test/ddd/5 9 5 d 1 4096 test/cc 5 cc f 2 0 test/cc/3 8 3 +zhasha@wirbelwind /home/zhasha ; ./6.out test/ d 0 4096 test/ 4 / d 1 4096 test/a 6 f 2 0 test/a/1 7 1 d 1 4096 test/b 6 f 2 0 test/b/1 7 1 f 2 0 test/b/2 7 2 d 1 4096 test/ddd 6 dd f 2 0 test/ddd/5 9 5 d 1 4096 test/cc 6 c f 2 0 test/cc/3 8 3 The final 2 columns are the file name length and path + ftw->base respectively, which is off by one when the path ends in /. It also seems to confuse the initial dir name. I have attached a fix but I'm not sure it's a particularly good one. [1] http://linux.die.net/man/3/ftw --=-vP9c9wUezlxxxN3yItoZ Content-Type: text/x-patch Content-Disposition: attachment; filename=nftw.patch diff --git a/src/misc/nftw.c b/src/misc/nftw.c index efb2b89..7c2c0d3 100644 --- a/src/misc/nftw.c +++ b/src/misc/nftw.c @@ -108,11 +108,13 @@ int nftw(const char *path, int (*fn)(const char *, const struct stat *, int, str if (fd_limit <= 0) return 0; l = strlen(path); + while (l && path[l-1]=='/') --l; if (l > PATH_MAX) { errno = ENAMETOOLONG; return -1; } - memcpy(pathbuf, path, l+1); + memcpy(pathbuf, path, l); + pathbuf[l]='\0'; pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs); r = do_nftw(pathbuf, fn, fd_limit, flags, NULL); --=-vP9c9wUezlxxxN3yItoZ--