summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTodd C. Miller <millert@cvs.openbsd.org>2009-08-27 16:19:28 +0000
committerTodd C. Miller <millert@cvs.openbsd.org>2009-08-27 16:19:28 +0000
commitb7f5ad085124a99744ae40715274e883f8461659 (patch)
treef194ede5673cdb211dba854b336abc74df3abad7
parentbf2e754350c1fb7e3b93a2dc42e445c7055f1d1e (diff)
Don't stop traversing a directory hierarchy if we reach SHRT_MAX,
just stop updating fts_level so we don't overflow it. This allows rm, find, etc to operate on very deep hierarchies. Consumers of fts(3) do need to be aware that the actual level may be larger than fts_level. During the next libc major bump we will make fts_level an int instead of a short. OK deraadt@
-rw-r--r--include/fts.h3
-rw-r--r--lib/libc/gen/fts.323
-rw-r--r--lib/libc/gen/fts.c18
-rw-r--r--usr.bin/find/function.c9
4 files changed, 35 insertions, 18 deletions
diff --git a/include/fts.h b/include/fts.h
index 825fc5abfe8..da26a8823ee 100644
--- a/include/fts.h
+++ b/include/fts.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: fts.h,v 1.11 2005/06/17 20:36:55 millert Exp $ */
+/* $OpenBSD: fts.h,v 1.12 2009/08/27 16:19:27 millert Exp $ */
/* $NetBSD: fts.h,v 1.5 1994/12/28 01:41:50 mycroft Exp $ */
/*
@@ -79,6 +79,7 @@ typedef struct _ftsent {
#define FTS_ROOTPARENTLEVEL -1
#define FTS_ROOTLEVEL 0
+#define FTS_MAXLEVEL 0x7fff
short fts_level; /* depth (-1 to N) */
#define FTS_D 1 /* preorder directory */
diff --git a/lib/libc/gen/fts.3 b/lib/libc/gen/fts.3
index cd6bdb78e5d..967346efcd4 100644
--- a/lib/libc/gen/fts.3
+++ b/lib/libc/gen/fts.3
@@ -1,4 +1,4 @@
-.\" $OpenBSD: fts.3,v 1.25 2009/03/23 22:57:36 sobrado Exp $
+.\" $OpenBSD: fts.3,v 1.26 2009/08/27 16:19:27 millert Exp $
.\"
.\" Copyright (c) 1989, 1991, 1993, 1994
.\" The Regents of the University of California. All rights reserved.
@@ -29,7 +29,7 @@
.\"
.\" @(#)fts.3 8.5 (Berkeley) 4/16/94
.\"
-.Dd $Mdocdate: March 23 2009 $
+.Dd $Mdocdate: August 27 2009 $
.Dt FTS 3
.Os
.Sh NAME
@@ -230,10 +230,25 @@ was found.
The
.Li FTSENT
structure representing the parent of the starting point (or root)
-of the traversal is numbered \-1, and the
+of the traversal is numbered
+.Ev FTS_PARENTLEVEL
+(\-1), and the
.Li FTSENT
structure for the root
-itself is numbered 0.
+itself is numbered
+.Ev FTS_ROOTLEVEL
+(0).
+Note that while
+.Fa fts_level
+cannot hold a number of levels greater than
+.Ev FTS_MAXLEVEL ,
+the
+.Nm
+functions themselves are not limited to a fixed number
+of levels.
+Application code that inspects
+.Fa fts_level
+should be written with this in mind.
.It Fa fts_errno
Upon return of an
.Li FTSENT
diff --git a/lib/libc/gen/fts.c b/lib/libc/gen/fts.c
index 62b815fd2f0..bbc1dc7475b 100644
--- a/lib/libc/gen/fts.c
+++ b/lib/libc/gen/fts.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: fts.c,v 1.42 2009/02/11 13:24:05 otto Exp $ */
+/* $OpenBSD: fts.c,v 1.43 2009/08/27 16:19:27 millert Exp $ */
/*-
* Copyright (c) 1990, 1993, 1994
@@ -633,15 +633,13 @@ fts_build(FTS *sp, int type)
len++;
maxlen = sp->fts_pathlen - len;
- if (cur->fts_level == SHRT_MAX) {
- (void)closedir(dirp);
- cur->fts_info = FTS_ERR;
- SET(FTS_STOP);
- errno = ENAMETOOLONG;
- return (NULL);
- }
-
- level = cur->fts_level + 1;
+ /*
+ * fts_level is a short so we must prevent it from wrapping
+ * around to FTS_ROOTLEVEL and FTS_ROOTPARENTLEVEL.
+ */
+ level = cur->fts_level;
+ if (level < FTS_MAXLEVEL)
+ level++;
/* Read the directory, attaching each entry to the `link' pointer. */
doadjust = 0;
diff --git a/usr.bin/find/function.c b/usr.bin/find/function.c
index 5f1dac45386..9b928eaf12b 100644
--- a/usr.bin/find/function.c
+++ b/usr.bin/find/function.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: function.c,v 1.31 2005/06/15 14:19:45 millert Exp $ */
+/* $OpenBSD: function.c,v 1.32 2009/08/27 16:19:27 millert Exp $ */
/*-
* Copyright (c) 1990, 1993
@@ -34,7 +34,7 @@
#ifndef lint
/*static char sccsid[] = "from: @(#)function.c 8.1 (Berkeley) 6/6/93";*/
-static char rcsid[] = "$OpenBSD: function.c,v 1.31 2005/06/15 14:19:45 millert Exp $";
+static char rcsid[] = "$OpenBSD: function.c,v 1.32 2009/08/27 16:19:27 millert Exp $";
#endif /* not lint */
#include <sys/param.h>
@@ -833,9 +833,12 @@ PLAN *
c_maxdepth(char *arg, char ***ignored, int unused)
{
PLAN *new;
+ const char *errstr = NULL;
new = palloc(N_MAXDEPTH, f_maxdepth);
- new->max_data = atoi(arg);
+ new->max_data = strtonum(arg, 1, FTS_MAXLEVEL, &errstr);
+ if (errstr)
+ errx(1, "%s: maxdepth value %s", arg, errstr);
return (new);
}