Re: rm -rf "" # prints error

2016-06-28 Thread Todd C. Miller
On Tue, 28 Jun 2016 10:20:43 -0700, patrick keshishian wrote:

> Since POSIX was mentioned, "" is not a valid filename[1]. So
> it isn't the case of a file does not exist (ENOENT), it is a case
> of an invalid filename (EINVAL?).

I don't think it really matters.  Furthermore, callers of fts(3)
are more likely to be expecting ENOENT for non-existent files.  I
dropped the zero-length check in what I committed so we'll just get
the error return by fstatat(2), which is ENOENT.

 - todd



Re: rm -rf "" # prints error

2016-06-28 Thread patrick keshishian
Since POSIX was mentioned, "" is not a valid filename[1]. So
it isn't the case of a file does not exist (ENOENT), it is a case
of an invalid filename (EINVAL?).

--patrick

[1]
pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap03.html#tag_03_170

--- 8< -
 3.170 Filename

A sequence of bytes consisting of 1 to {NAME_MAX} bytes used to name a
file. The bytes composing the name shall not contain the  or
 characters. In the context of a pathname, each filename shall
be followed by a  or a  character; elsewhere, a filename
followed by a  character forms a string (but not necessarily a
character string). The filenames dot and dot-dot have special meaning.
A filename is sometimes referred to as a "pathname component". See
also Pathname.

Note:
Pathname Resolution is defined in detail in Pathname Resolution .
- >8 ---


On 6/28/16, Philip Guenther  wrote:
> On Tue, 28 Jun 2016, Todd C. Miller wrote:
>> I think this needs to be fixed in fts(3) instead.  The following diff
>> fixes it for me but has only been lightly tested.
>
> As I noted in icb, first chunk looks sufficient to me; the latter chunk is
> just optimizing the error case, as fstatat() will already fail the ""
> case.
>
> I think we should also include this check from FreeBSD:
>
> --- gen/fts.c   16 Jan 2015 16:48:51 -
> +++ gen/fts.c   5 Feb 2015 05:01:08 -
> @@ -83,6 +83,12 @@ fts_open(char * const *argv, int options
> return (NULL);
> }
>
> +   /* fts_open() requires at least one path */
> +   if (*argv == NULL) {
> +   errno = EINVAL;
> +   return (NULL);
> +   }
> +
> /* Allocate/initialize the stream */
> if ((sp = calloc(1, sizeof(FTS))) == NULL)
> return (NULL);
>
>



Re: rm -rf "" # prints error

2016-06-28 Thread Todd C. Miller
On Tue, 28 Jun 2016 10:03:00 -0700, Philip Guenther wrote:

> On Tue, 28 Jun 2016, Todd C. Miller wrote:
> > I think this needs to be fixed in fts(3) instead.  The following diff 
> > fixes it for me but has only been lightly tested.
> 
> As I noted in icb, first chunk looks sufficient to me; the latter chunk is 
> just optimizing the error case, as fstatat() will already fail the "" 
> case.
> 
> I think we should also include this check from FreeBSD:

OK millert@ but please include the man page change from rev 197793
too.

 - todd



Re: rm -rf "" # prints error

2016-06-28 Thread Philip Guenther
On Tue, 28 Jun 2016, Todd C. Miller wrote:
> I think this needs to be fixed in fts(3) instead.  The following diff 
> fixes it for me but has only been lightly tested.

As I noted in icb, first chunk looks sufficient to me; the latter chunk is 
just optimizing the error case, as fstatat() will already fail the "" 
case.

I think we should also include this check from FreeBSD:

--- gen/fts.c   16 Jan 2015 16:48:51 -
+++ gen/fts.c   5 Feb 2015 05:01:08 -
@@ -83,6 +83,12 @@ fts_open(char * const *argv, int options
return (NULL);
}
 
+   /* fts_open() requires at least one path */
+   if (*argv == NULL) {
+   errno = EINVAL;
+   return (NULL);
+   }
+
/* Allocate/initialize the stream */
if ((sp = calloc(1, sizeof(FTS))) == NULL)
return (NULL);



Re: rm -rf "" # prints error

2016-06-28 Thread Todd C. Miller
On Tue, 28 Jun 2016 15:24:43 -, Florian Obser wrote:

> There is still a bug:
> 
> $ mkdir foo; cd foo; touch bar; rm -rf "" bar; echo $?; ls -la; \
> rm -rf nonexistend bar; echo $?; ls -la
> 0
> total 8
> drwxr-xr-x  2 florian  wheel  512 Jun 28 17:20 ./
> drwxrwxrwt  9 root wheel  512 Jun 28 17:20 ../
> -rw-r--r--  1 florian  wheel0 Jun 28 17:20 bar
> 0
> total 8
> drwxr-xr-x  2 florian  wheel  512 Jun 28 17:20 ./
> drwxrwxrwt  9 root wheel  512 Jun 28 17:20 ../
> 
> i.e. rm does not continue when it fails on "", this only happens when
> specifying -r (because fts_open fails)

I think this needs to be fixed in fts(3) instead.  The following
diff fixes it for me but has only been lightly tested.

 - todd

Index: lib/libc/gen/fts.c
===
RCS file: /cvs/src/lib/libc/gen/fts.c,v
retrieving revision 1.53
diff -u -p -u -r1.53 fts.c
--- lib/libc/gen/fts.c  1 Nov 2015 03:45:29 -   1.53
+++ lib/libc/gen/fts.c  28 Jun 2016 15:40:28 -
@@ -107,12 +107,7 @@ fts_open(char * const *argv, int options
 
/* Allocate/initialize root(s). */
for (root = NULL, nitems = 0; *argv; ++argv, ++nitems) {
-   /* Don't allow zero-length paths. */
-   if ((len = strlen(*argv)) == 0) {
-   errno = ENOENT;
-   goto mem3;
-   }
-
+   len = strlen(*argv);
if ((p = fts_alloc(sp, *argv, len)) == NULL)
goto mem3;
p->fts_level = FTS_ROOTLEVEL;
@@ -810,6 +805,12 @@ fts_stat(FTS *sp, FTSENT *p, int follow,
 
/* If user needs stat info, stat buffer already allocated. */
sbp = ISSET(FTS_NOSTAT) ?  : p->fts_statp;
+
+   /* Skip zero-length paths. */
+   if (*path == '\0') {
+   p->fts_errno = ENOENT;
+   goto err;
+   }
 
/*
 * If doing a logical walk, or application requested FTS_FOLLOW, do



Re: rm -rf "" # prints error

2016-06-28 Thread Florian Obser
On Tue, Jun 28, 2016 at 10:50:48AM -0400, Ted Unangst wrote:
> rkito...@gmail.com wrote:
> > >Synopsis:  rm -rf "" # prints error message but should be silent
> > >Category:  system
> > >Environment:
> > System  : OpenBSD 5.9
> > Details : OpenBSD 5.9 (GENERIC.MP) #1888: Fri Feb 26 01:20:19 MST 
> > 2016
> >  
> > dera...@amd64.openbsd.org:/usr/src/sys/arch/amd64/compile/GENERIC.MP
> > 
> > Architecture: OpenBSD.amd64
> > Machine : amd64
> > >Description:
> > The command:
> > 
> > rm -rf ""
> > 
> > prints to STDERR:
> > 
> > rm: No such file or directory
> > 
> > but it should print nothing because of the -f flag.
> 
> This fixes it.

There is still a bug:

$ mkdir foo; cd foo; touch bar; rm -rf "" bar; echo $?; ls -la; \
rm -rf nonexistend bar; echo $?; ls -la
0
total 8
drwxr-xr-x  2 florian  wheel  512 Jun 28 17:20 ./
drwxrwxrwt  9 root wheel  512 Jun 28 17:20 ../
-rw-r--r--  1 florian  wheel0 Jun 28 17:20 bar
0
total 8
drwxr-xr-x  2 florian  wheel  512 Jun 28 17:20 ./
drwxrwxrwt  9 root wheel  512 Jun 28 17:20 ../

i.e. rm does not continue when it fails on "", this only happens when
specifying -r (because fts_open fails)

Posix says this:

1. If the file does not exist:

a. If the -f option is not specified, rm shall write a diagnostic message 
to standard error.

b. Go on to any remaining files.

> 
> 
> Index: rm.c
> ===
> RCS file: /cvs/src/bin/rm/rm.c,v
> retrieving revision 1.37
> diff -u -p -r1.37 rm.c
> --- rm.c  15 Apr 2016 23:09:57 -  1.37
> +++ rm.c  28 Jun 2016 14:49:42 -
> @@ -150,8 +150,11 @@ rm_tree(char **argv)
>   flags = FTS_PHYSICAL;
>   if (!needstat)
>   flags |= FTS_NOSTAT;
> - if (!(fts = fts_open(argv, flags, NULL)))
> - err(1, NULL);
> + if (!(fts = fts_open(argv, flags, NULL))) {
> + if (!fflag || errno != ENOENT)
> + err(1, NULL);
> + return;
> + }
>   while ((p = fts_read(fts)) != NULL) {
>   switch (p->fts_info) {
>   case FTS_DNR:
> 


-- 
I'm not entirely sure you are real.



Re: rm -rf "" # prints error

2016-06-28 Thread Sebastian Benoit
Ted Unangst(t...@tedunangst.com) on 2016.06.28 10:50:48 -0400:
> rkito...@gmail.com wrote:
> > >Synopsis:  rm -rf "" # prints error message but should be silent
> > >Category:  system
> > >Environment:
> > System  : OpenBSD 5.9
> > Details : OpenBSD 5.9 (GENERIC.MP) #1888: Fri Feb 26 01:20:19 MST 
> > 2016
> >  
> > dera...@amd64.openbsd.org:/usr/src/sys/arch/amd64/compile/GENERIC.MP
> > 
> > Architecture: OpenBSD.amd64
> > Machine : amd64
> > >Description:
> > The command:
> > 
> > rm -rf ""
> > 
> > prints to STDERR:
> > 
> > rm: No such file or directory
> > 
> > but it should print nothing because of the -f flag.
> 
> This fixes it.

ok

> Index: rm.c
> ===
> RCS file: /cvs/src/bin/rm/rm.c,v
> retrieving revision 1.37
> diff -u -p -r1.37 rm.c
> --- rm.c  15 Apr 2016 23:09:57 -  1.37
> +++ rm.c  28 Jun 2016 14:49:42 -
> @@ -150,8 +150,11 @@ rm_tree(char **argv)
>   flags = FTS_PHYSICAL;
>   if (!needstat)
>   flags |= FTS_NOSTAT;
> - if (!(fts = fts_open(argv, flags, NULL)))
> - err(1, NULL);
> + if (!(fts = fts_open(argv, flags, NULL))) {
> + if (!fflag || errno != ENOENT)
> + err(1, NULL);
> + return;
> + }
>   while ((p = fts_read(fts)) != NULL) {
>   switch (p->fts_info) {
>   case FTS_DNR:
> 

-- 



Re: rm -rf "" # prints error

2016-06-28 Thread Ted Unangst
rkito...@gmail.com wrote:
> >Synopsis:    rm -rf "" # prints error message but should be silent
> >Category:system
> >Environment:
>   System  : OpenBSD 5.9
>   Details : OpenBSD 5.9 (GENERIC.MP) #1888: Fri Feb 26 01:20:19 MST 
> 2016
>
> dera...@amd64.openbsd.org:/usr/src/sys/arch/amd64/compile/GENERIC.MP
> 
>   Architecture: OpenBSD.amd64
>   Machine : amd64
> >Description:
>   The command:
> 
>   rm -rf ""
> 
>   prints to STDERR:
> 
>   rm: No such file or directory
> 
>   but it should print nothing because of the -f flag.

This fixes it.


Index: rm.c
===
RCS file: /cvs/src/bin/rm/rm.c,v
retrieving revision 1.37
diff -u -p -r1.37 rm.c
--- rm.c15 Apr 2016 23:09:57 -  1.37
+++ rm.c28 Jun 2016 14:49:42 -
@@ -150,8 +150,11 @@ rm_tree(char **argv)
flags = FTS_PHYSICAL;
if (!needstat)
flags |= FTS_NOSTAT;
-   if (!(fts = fts_open(argv, flags, NULL)))
-   err(1, NULL);
+   if (!(fts = fts_open(argv, flags, NULL))) {
+   if (!fflag || errno != ENOENT)
+   err(1, NULL);
+   return;
+   }
while ((p = fts_read(fts)) != NULL) {
switch (p->fts_info) {
case FTS_DNR:



rm -rf "" # prints error

2016-06-28 Thread rkitover
>Synopsis:      rm -rf "" # prints error message but should be silent
>Category:  system
>Environment:
System  : OpenBSD 5.9
Details : OpenBSD 5.9 (GENERIC.MP) #1888: Fri Feb 26 01:20:19 MST 
2016
 
dera...@amd64.openbsd.org:/usr/src/sys/arch/amd64/compile/GENERIC.MP

Architecture: OpenBSD.amd64
Machine : amd64
>Description:
The command:

rm -rf ""

prints to STDERR:

rm: No such file or directory

but it should print nothing because of the -f flag.

Note that the command:

rm -rf nonexistant

does not print anything to STDERR.

This only happens with the empty arg "".

I compiled /usr/src/bin/rm from CURRENT and tested it, and it exhibits 
the same issue.
>How-To-Repeat:
Run the command:

rm -rf ""
>Fix:


dmesg:
OpenBSD 5.9 (GENERIC.MP) #1888: Fri Feb 26 01:20:19 MST 2016
dera...@amd64.openbsd.org:/usr/src/sys/arch/amd64/compile/GENERIC.MP
real mem = 3162427392 (3015MB)
avail mem = 3062382592 (2920MB)
mpath0 at root
scsibus0 at mpath0: 256 targets
mainbus0 at root
bios0 at mainbus0: SMBIOS rev. 2.7 @ 0xaffed000 (24 entries)
bios0: vendor Parallels Software International Inc. version "11.2.0 (32581)" 
date 04/27/2016
bios0: Parallels Software International Inc. Parallels Virtual Platform
acpi0 at bios0: rev 2
acpi0: sleep states S0 S3 S4 S5
acpi0: tables DSDT FACP APIC HPET OEM1
acpi0: wakeup devices EXP1(S4) EXP2(S4) EXP3(S4) EXP4(S4) EXP5(S4) EXP6(S4)
acpitimer0 at acpi0: 3579545 Hz, 24 bits
acpimadt0 at acpi0 addr 0xfee0: PC-AT compat
cpu0 at mainbus0: apid 0 (boot processor)
cpu0: Intel(R) Core(TM) i7-2635QM CPU @ 2.00GHz, 1995.71 MHz
cpu0: 
FPU,VME,DE,PSE,TSC,MSR,PAE,MCE,CX8,APIC,SEP,MTRR,PGE,MCA,CMOV,PAT,PSE36,CFLUSH,MMX,FXSR,SSE,SSE2,SS,HTT,SSE3,PCLMUL,SSSE3,CX16,PCID,SSE4.1,SSE4.2,x2APIC,POPCNT,DEADLINE,AES,XSAVE,AVX,HV,NXE,LONG,LAHF,ITSC,SENSOR,ARAT
cpu0: 256KB 64b/line 8-way L2 cache
cpu0: smt 0, core 0, package 0
mtrr: Pentium Pro MTRR support, 8 var ranges, 88 fixed ranges
cpu0: apic clock running at 99MHz
cpu1 at mainbus0: apid 1 (application processor)
cpu1: Intel(R) Core(TM) i7-2635QM CPU @ 2.00GHz, 1995.31 MHz
cpu1: 
FPU,VME,DE,PSE,TSC,MSR,PAE,MCE,CX8,APIC,SEP,MTRR,PGE,MCA,CMOV,PAT,PSE36,CFLUSH,MMX,FXSR,SSE,SSE2,SS,HTT,SSE3,PCLMUL,SSSE3,CX16,PCID,SSE4.1,SSE4.2,x2APIC,POPCNT,DEADLINE,AES,XSAVE,AVX,HV,NXE,LONG,LAHF,ITSC,SENSOR,ARAT
cpu1: 256KB 64b/line 8-way L2 cache
cpu1: smt 0, core 1, package 0
cpu2 at mainbus0: apid 2 (application processor)
cpu2: Intel(R) Core(TM) i7-2635QM CPU @ 2.00GHz, 1995.33 MHz
cpu2: 
FPU,VME,DE,PSE,TSC,MSR,PAE,MCE,CX8,APIC,SEP,MTRR,PGE,MCA,CMOV,PAT,PSE36,CFLUSH,MMX,FXSR,SSE,SSE2,SS,HTT,SSE3,PCLMUL,SSSE3,CX16,PCID,SSE4.1,SSE4.2,x2APIC,POPCNT,DEADLINE,AES,XSAVE,AVX,HV,NXE,LONG,LAHF,ITSC,SENSOR,ARAT
cpu2: 256KB 64b/line 8-way L2 cache
cpu2: smt 0, core 2, package 0
cpu3 at mainbus0: apid 3 (application processor)
cpu3: Intel(R) Core(TM) i7-2635QM CPU @ 2.00GHz, 1995.31 MHz
cpu3: 
FPU,VME,DE,PSE,TSC,MSR,PAE,MCE,CX8,APIC,SEP,MTRR,PGE,MCA,CMOV,PAT,PSE36,CFLUSH,MMX,FXSR,SSE,SSE2,SS,HTT,SSE3,PCLMUL,SSSE3,CX16,PCID,SSE4.1,SSE4.2,x2APIC,POPCNT,DEADLINE,AES,XSAVE,AVX,HV,NXE,LONG,LAHF,ITSC,SENSOR,ARAT
cpu3: 256KB 64b/line 8-way L2 cache
cpu3: smt 0, core 3, package 0
cpu4 at mainbus0: apid 4 (application processor)
cpu4: Intel(R) Core(TM) i7-2635QM CPU @ 2.00GHz, 1995.33 MHz
cpu4: 
FPU,VME,DE,PSE,TSC,MSR,PAE,MCE,CX8,APIC,SEP,MTRR,PGE,MCA,CMOV,PAT,PSE36,CFLUSH,MMX,FXSR,SSE,SSE2,SS,HTT,SSE3,PCLMUL,SSSE3,CX16,PCID,SSE4.1,SSE4.2,x2APIC,POPCNT,DEADLINE,AES,XSAVE,AVX,HV,NXE,LONG,LAHF,ITSC,SENSOR,ARAT
cpu4: 256KB 64b/line 8-way L2 cache
cpu4: smt 0, core 4, package 0
cpu5 at mainbus0: apid 5 (application processor)
cpu5: Intel(R) Core(TM) i7-2635QM CPU @ 2.00GHz, 1995.35 MHz
cpu5: 
FPU,VME,DE,PSE,TSC,MSR,PAE,MCE,CX8,APIC,SEP,MTRR,PGE,MCA,CMOV,PAT,PSE36,CFLUSH,MMX,FXSR,SSE,SSE2,SS,HTT,SSE3,PCLMUL,SSSE3,CX16,PCID,SSE4.1,SSE4.2,x2APIC,POPCNT,DEADLINE,AES,XSAVE,AVX,HV,NXE,LONG,LAHF,ITSC,SENSOR,ARAT
cpu5: 256KB 64b/line 8-way L2 cache
cpu5: smt 0, core 5, package 0
cpu6 at mainbus0: apid 6 (application processor)
cpu6: Intel(R) Core(TM) i7-2635QM CPU @ 2.00GHz, 1995.35 MHz
cpu6: 
FPU,VME,DE,PSE,TSC,MSR,PAE,MCE,CX8,APIC,SEP,MTRR,PGE,MCA,CMOV,PAT,PSE36,CFLUSH,MMX,FXSR,SSE,SSE2,SS,HTT,SSE3,PCLMUL,SSSE3,CX16,PCID,SSE4.1,SSE4.2,x2APIC,POPCNT,DEADLINE,AES,XSAVE,AVX,HV,NXE,LONG,LAHF,ITSC,SENSOR,ARAT
cpu6: 256KB 64b/line 8-way L2 cache
cpu6: smt 0, core 6, package 0
cpu7 at mainbus0: apid 7 (application processor)
cpu7: Intel(R) Core(TM) i7-2635QM CPU @ 2.00GHz, 2030.15 MHz
cpu7: 
FPU,VME,DE,PSE,TSC,MSR,PAE,MCE,CX8,APIC,SEP,MTRR,PGE,MCA,CMOV,PAT,PSE36,CFLUSH,MMX,FXSR,SSE,SSE2,SS,HTT,SSE3,PCLMUL,SSSE3,CX16,PCID,SSE4.1,SSE4.2,x2APIC,POPCNT,DEADLINE,AES,XSAVE,AVX,HV,NXE,LONG,LAHF,ITSC,SENSOR,ARAT
cpu7: 256KB 64b/line 8-way L2 cache
cpu7: smt 0, core 7, pac