From owner-svn-src-head@FreeBSD.ORG Fri Aug 9 19:10:57 2013 Return-Path: Delivered-To: svn-src-head@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [8.8.178.115]) (using TLSv1 with cipher ADH-AES256-SHA (256/256 bits)) (No client certificate requested) by hub.freebsd.org (Postfix) with ESMTP id 657D492B; Fri, 9 Aug 2013 19:10:57 +0000 (UTC) (envelope-from marcel@FreeBSD.org) Received: from svn.freebsd.org (svn.freebsd.org [IPv6:2001:1900:2254:2068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.freebsd.org (Postfix) with ESMTPS id 389372BA8; Fri, 9 Aug 2013 19:10:57 +0000 (UTC) Received: from svn.freebsd.org ([127.0.1.70]) by svn.freebsd.org (8.14.7/8.14.7) with ESMTP id r79JAvgI081934; Fri, 9 Aug 2013 19:10:57 GMT (envelope-from marcel@svn.freebsd.org) Received: (from marcel@localhost) by svn.freebsd.org (8.14.7/8.14.5/Submit) id r79JAvkD081933; Fri, 9 Aug 2013 19:10:57 GMT (envelope-from marcel@svn.freebsd.org) Message-Id: <201308091910.r79JAvkD081933@svn.freebsd.org> From: Marcel Moolenaar Date: Fri, 9 Aug 2013 19:10:57 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r254155 - head/sys/boot/ficl X-SVN-Group: head MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-head@freebsd.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: SVN commit messages for the src tree for head/-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 09 Aug 2013 19:10:57 -0000 Author: marcel Date: Fri Aug 9 19:10:56 2013 New Revision: 254155 URL: http://svnweb.freebsd.org/changeset/base/254155 Log: Fix the freaddir implementation for the stand-alone interpreter. Bug pointed out by: Jan Beich Modified: head/sys/boot/ficl/loader.c Modified: head/sys/boot/ficl/loader.c ============================================================================== --- head/sys/boot/ficl/loader.c Fri Aug 9 18:57:18 2013 (r254154) +++ head/sys/boot/ficl/loader.c Fri Aug 9 19:10:56 2013 (r254155) @@ -518,12 +518,12 @@ static void pfread(FICL_VM *pVM) static void pfreaddir(FICL_VM *pVM) { #ifdef TESTMAIN - static union { - struct dirent dirent; - char buf[512]; - } u; - off_t off; - int len; + static struct dirent dirent; + struct stat sb; + char *buf; + off_t off, ptr; + u_int blksz; + int bufsz; #endif struct dirent *d; int fd; @@ -539,11 +539,39 @@ static void pfreaddir(FICL_VM *pVM) * We do the best we can to make freaddir work, but it's not at * all guaranteed. */ - off = lseek(fd, 0LL, SEEK_CUR); - len = getdents(fd, u.buf, sizeof(u.buf)); - d = (len != -1) ? &u.dirent : NULL; - if (d != NULL) - lseek(fd, off + d->d_reclen, SEEK_SET); + d = NULL; + buf = NULL; + do { + if (fd == -1) + break; + if (fstat(fd, &sb) == -1) + break; + blksz = (sb.st_blksize) ? sb.st_blksize : getpagesize(); + if ((blksz & (blksz - 1)) != 0) + break; + buf = malloc(blksz); + if (buf == NULL) + break; + off = lseek(fd, 0LL, SEEK_CUR); + if (off == -1) + break; + ptr = off; + if (lseek(fd, 0, SEEK_SET) == -1) + break; + bufsz = getdents(fd, buf, blksz); + while (bufsz > 0 && bufsz <= ptr) { + ptr -= bufsz; + bufsz = getdents(fd, buf, blksz); + } + if (bufsz <= 0) + break; + d = (void *)(buf + ptr); + dirent = *d; + off += d->d_reclen; + d = (lseek(fd, off, SEEK_SET) != off) ? NULL : &dirent; + } while (0); + if (buf != NULL) + free(buf); #else d = readdirfd(fd); #endif