bin/111712: cat(1) misbehaves with directories as parameters

Ricardo Nabinger Sanchez rnsanchez at wait4.org
Mon Apr 16 18:00:10 UTC 2007


>Number:         111712
>Category:       bin
>Synopsis:       cat(1) misbehaves with directories as parameters
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    freebsd-bugs
>State:          open
>Quarter:        
>Keywords:       
>Date-Required:
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Mon Apr 16 18:00:09 GMT 2007
>Closed-Date:
>Last-Modified:
>Originator:     Ricardo Nabinger Sanchez
>Release:        6.1 RELEASE
>Organization:
>Environment:
FreeBSD sauron.lan.box 6.1-RELEASE FreeBSD 6.1-RELEASE #0: Sun May  7 04:32:43 UTC 2006     root at opus.cse.buffalo.edu:/usr/obj/usr/src/sys/GENERIC  i386
>Description:
If a directory is passed to cat, it outputs its contents, (sometimes) mangles the terminal, and may misbehave, like in the second run with ". Makefile" as parameters.

rnsanchez at sauron:~...src/usr.bin/cat% cat -n Makefile .
     1  #       @(#)Makefile    8.1 (Berkeley) 5/31/93
     2  # $FreeBSD: src/bin/cat/Makefile,v 1.7 2001/12/04 01:57:37 obrien Exp $
     3
     4  PROG=   cat
     5
     6  .include <bsd.prog.mk>
     1  !g
          .cat.1.gzÙåø.Øö/~...src/usr.bin/cat% cat -n . Makefile 
     1  !g
          .cat.1.gzÙåø.Øö/~...src/usr.bin/cat%


The patch supplied fixes this behavior, adding a check to skip directories:

rnsanchez at sauron:~...src/usr.bin/cat% ./cat -n Makefile .
     1  #       @(#)Makefile    8.1 (Berkeley) 5/31/93
     2  # $FreeBSD: src/bin/cat/Makefile,v 1.7 2001/12/04 01:57:37 obrien Exp $
     3
     4  PROG=   cat
     5
     6  .include <bsd.prog.mk>
rnsanchez at sauron:~...src/usr.bin/cat% ./cat -n . Makefile 
     1  #       @(#)Makefile    8.1 (Berkeley) 5/31/93
     2  # $FreeBSD: src/bin/cat/Makefile,v 1.7 2001/12/04 01:57:37 obrien Exp $
     3
     4  PROG=   cat
     5
     6  .include <bsd.prog.mk>
rnsanchez at sauron:~...src/usr.bin/cat%
>How-To-Repeat:
Just cat(1) a directory.
>Fix:
Diff against -CURRENT sources attached.  Works OK on a 6.1-RELEASE.

Patch attached with submission follows:

Index: usr.bin/cat/cat.c
===================================================================
RCS file: /home/ncvs/src/bin/cat/cat.c,v
retrieving revision 1.32
diff -u -p -r1.32 cat.c
--- usr.bin/cat/cat.c	10 Jan 2005 08:39:20 -0000	1.32
+++ usr.bin/cat/cat.c	16 Apr 2007 17:32:00 -0000
@@ -136,6 +136,7 @@ scanfiles(char *argv[], int cooked)
 	int i = 0;
 	char *path;
 	FILE *fp;
+	struct stat sbuf;
 
 	while ((path = argv[i]) != NULL || i == 0) {
 		int fd;
@@ -145,6 +146,19 @@ scanfiles(char *argv[], int cooked)
 			fd = STDIN_FILENO;
 		} else {
 			filename = path;
+			/*
+			 * Check if path refers to a directory, in which case
+			 * it should not be opened.
+			 */
+			if (stat(path, &sbuf))
+				err(1, "%s", filename);
+			if (S_ISDIR(sbuf.st_mode)) {
+				/*
+				 * Silently skip this directory.
+				 */
+				++i;
+				continue;
+			}
 			fd = open(path, O_RDONLY);
 #ifndef NO_UDOM_SUPPORT
 			if (fd < 0 && errno == EOPNOTSUPP)

>Release-Note:
>Audit-Trail:
>Unformatted:


More information about the freebsd-bugs mailing list