Bug#848231: bugs in scandir example program

2022-01-26 Thread Florian Ernst
On Thu, Dec 15, 2016 at 04:59:38PM +0100, Michael Kerrisk (man-pages) wrote:
> [...]
> All fixed now. This is the current code:
> 
>#define _DEFAULT_SOURCE
>#include 
>#include 
>#include 
> 
>int
>main(void)
>{
>struct dirent **namelist;
>int n;
> 
>n = scandir(".", , NULL, alphasort);
>if (n == -1) {
>perror("scandir");
>exit(EXIT_FAILURE);
>}
> 
>while (n--) {
>printf("%s\n", namelist[n]->d_name);
>free(namelist[n]);
>}
>free(namelist);
> 
>exit(EXIT_SUCCESS);
>}

Confirmed to be present in man-pages-4.10 (first packaged in Debian in
4.10-1) and also confirmed working, hence this bug could be closed
accordingly.

Cheers,
Flo


signature.asc
Description: PGP signature


Bug#848231: bugs in scandir example program

2016-12-15 Thread Ian Jackson
Michael Kerrisk (man-pages) writes ("Re: Bug#848231: bugs in scandir example 
program"):
> We've misunderstood each other. Here's how I think i is:
> 
> 1. You sent me a patch against a reasonably old version of the program
> (pre 2012 upstream).
> 
> 2. In 2012, I made this change in the code:
> 
> -   n = scandir(".", , 0, alphasort);
> +   n = scandir(".", , NULL, alphasort);

Oh, right.  Jolly good.

Thanks,
Ian.

-- 
Ian Jackson <ijack...@chiark.greenend.org.uk>   These opinions are my own.

If I emailed you from an address @fyvzl.net or @evade.org.uk, that is
a private address which bypasses my fierce spamfilter.



Bug#848231: bugs in scandir example program

2016-12-15 Thread Michael Kerrisk (man-pages)
On 15 December 2016 at 14:56, Ian Jackson
<ijack...@chiark.greenend.org.uk> wrote:
> Michael Kerrisk (man-pages) writes ("Re: Bug#848231: bugs in scandir example 
> program"):
>> tags 848231 fixed-upstream
>> thanks
> ...>
>> On 15 December 2016 at 13:55, Ian Jackson
>> > int
>> > main(void)
>> > @@ -18,4 +20,5 @@
>> > }
>> > free(namelist);
>> > }
>> > +   return 0;
>> > }
>> >
>>
>> Thanks, Ian. Fixed pretty much as you suggest. The program doesn't
>> even compile as it was given! Looks like I injected the error after a
>> user report 4 years ago (changed "0" to "NULL").
>
> Thanks.  But, 0 should be 0, not NULL.
>
> NULL is a null pointer constant but main returns int.
>
> (Even if main returned a pointer, 0 would be legal, because 0 is a
> valid nll pointer constant, but I guess for pedagogic reasons using
> NULL is probaby better in manpages where pointers are meant.)

We've misunderstood each other. Here's how I think i is:

1. You sent me a patch against a reasonably old version of the program
(pre 2012 upstream).

2. In 2012, I made this change in the code:

-   n = scandir(".", , 0, alphasort);
+   n = scandir(".", , NULL, alphasort);

3. In that preceding change, I did not add a #include for ,
so the code would not even compile.

All fixed now. This is the current code:

   #define _DEFAULT_SOURCE
   #include 
   #include 
   #include 

   int
   main(void)
   {
   struct dirent **namelist;
   int n;

   n = scandir(".", , NULL, alphasort);
   if (n == -1) {
   perror("scandir");
   exit(EXIT_FAILURE);
   }

   while (n--) {
   printf("%s\n", namelist[n]->d_name);
   free(namelist[n]);
   }
   free(namelist);

   exit(EXIT_SUCCESS);
   }

Cheers,

Michael

-- 
Michael Kerrisk
Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/
Linux/UNIX System Programming Training: http://man7.org/training/


-- 
Michael Kerrisk
Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/
Linux/UNIX System Programming Training: http://man7.org/training/



Bug#848231: bugs in scandir example program

2016-12-15 Thread Ian Jackson
Michael Kerrisk (man-pages) writes ("Re: Bug#848231: bugs in scandir example 
program"):
> tags 848231 fixed-upstream
> thanks
...> 
> On 15 December 2016 at 13:55, Ian Jackson
> > int
> > main(void)
> > @@ -18,4 +20,5 @@
> > }
> > free(namelist);
> > }
> > +   return 0;
> > }
> >
> 
> Thanks, Ian. Fixed pretty much as you suggest. The program doesn't
> even compile as it was given! Looks like I injected the error after a
> user report 4 years ago (changed "0" to "NULL").

Thanks.  But, 0 should be 0, not NULL.

NULL is a null pointer constant but main returns int.

(Even if main returned a pointer, 0 would be legal, because 0 is a
valid nll pointer constant, but I guess for pedagogic reasons using
NULL is probaby better in manpages where pointers are meant.)

Ian.

-- 
Ian Jackson <ijack...@chiark.greenend.org.uk>   These opinions are my own.

If I emailed you from an address @fyvzl.net or @evade.org.uk, that is
a private address which bypasses my fierce spamfilter.



Bug#848231: bugs in scandir example program

2016-12-15 Thread Michael Kerrisk (man-pages)
tags 848231 fixed-upstream
thanks

On 15 December 2016 at 13:55, Ian Jackson
 wrote:
> Package: manpages-dev
> Version: 3.74-1
> Severity: minor
>
> I cut and pasted the example program from scandir(3) and found that it
> had some bugs which caused compiler warnings with -Wall.  Here is a
> patch (to the example program, not to the nroff) to fix them.
>
> Thanks,
> Ian.
>
> --- t.c.orig2016-12-15 12:52:33.181859142 +
> +++ t.c 2016-12-15 12:55:08.939521298 +
> @@ -1,6 +1,8 @@
> #define _SVID_SOURCE
> /* print files in current directory in reverse order */
> #include 
> +   #include 
> +   #include 
>
> int
> main(void)
> @@ -18,4 +20,5 @@
> }
> free(namelist);
> }
> +   return 0;
> }
>

Thanks, Ian. Fixed pretty much as you suggest. The program doesn't
even compile as it was given! Looks like I injected the error after a
user report 4 years ago (changed "0" to "NULL").

Cheers,

Michael


-- 
Michael Kerrisk
Linux man-pages maintainer; http://www.kernel.org/doc/man-pages/
Linux/UNIX System Programming Training: http://man7.org/training/



Bug#848231: bugs in scandir example program

2016-12-15 Thread Ian Jackson
Package: manpages-dev
Version: 3.74-1
Severity: minor

I cut and pasted the example program from scandir(3) and found that it
had some bugs which caused compiler warnings with -Wall.  Here is a
patch (to the example program, not to the nroff) to fix them.

Thanks,
Ian.

--- t.c.orig2016-12-15 12:52:33.181859142 +
+++ t.c 2016-12-15 12:55:08.939521298 +
@@ -1,6 +1,8 @@
#define _SVID_SOURCE
/* print files in current directory in reverse order */
#include 
+   #include 
+   #include 
 
int
main(void)
@@ -18,4 +20,5 @@
}
free(namelist);
}
+   return 0;
}


-- 
Ian Jackson    These opinions are my own.

If I emailed you from an address @fyvzl.net or @evade.org.uk, that is
a private address which bypasses my fierce spamfilter.