[RTEMS Project] #1394: scandir() fails due to MAXNAMELEN is incorrect
RTEMS trac
trac at rtems.org
Wed Feb 25 19:07:53 UTC 2015
#1394: scandir() fails due to MAXNAMELEN is incorrect
--------------------+-----------------------
Reporter: daniel | Owner: chrisj
Type: defect | Status: assigned
Priority: normal | Milestone: 4.11
Component: Newlib | Version: 4.10
Severity: normal | Resolution:
Keywords: |
--------------------+-----------------------
Old description:
> I have been trying to use scandir() however the newlib one does not work
> due to MAXNAMLEN and NAMLEN differ. scandir in libcsupport seems to have
> a fix for this, however my libcsupport_a-scandir.o is empty, I'm guessing
> because HAVE_SCANDIR is defined.
>
> It is used in scandir() (newlib-1.17.0/newlib/libc/posix/scandir.c:117)
> by the macro DIRSIZ. Where DIRSIZ uses the MAXNAMELEN define which is set
> incorrectly. It does not match the sizeof(struct dirent) which makes the
> DIRSIZ return a negative number, then malloc(DIRSIZ(d)) will try to
> allocate 4GB which fail.
>
> My guess is that MAXNAMELEN should be defined in
> newlib-1.17.0/newlib/libc/sys/rtems/sys/dirent.h or
> newlib-1.17.0/newlib/libc/sys/rtems/include/limits.h or in a new file.
>
> I tried to run the code below on my FAT filesystem, taken directly from
> the scandir(3) man page.
>
> /* print files in current directory in reverse order */
> #include <dirent.h>
> main(){
> struct dirent **namelist;
> int n;
>
> n = scandir(".", &namelist, 0, NULL);
> if (n < 0)
> perror("scandir");
> else {
> while(n--) {
> printf("%s\n", namelist[n]->d_name);
> free(namelist[n]);
> }
> free(namelist);
> }
> }
New description:
I have been trying to use scandir() however the newlib one does not work
due to MAXNAMLEN and NAMLEN differ. scandir in libcsupport seems to have a
fix for this, however my libcsupport_a-scandir.o is empty, I'm guessing
because HAVE_SCANDIR is defined.
It is used in scandir() (newlib-1.17.0/newlib/libc/posix/scandir.c:117) by
the macro DIRSIZ. Where DIRSIZ uses the MAXNAMELEN define which is set
incorrectly. It does not match the sizeof(struct dirent) which makes the
DIRSIZ return a negative number, then malloc(DIRSIZ(d)) will try to
allocate 4GB which fail.
My guess is that MAXNAMELEN should be defined in
newlib-1.17.0/newlib/libc/sys/rtems/sys/dirent.h or
newlib-1.17.0/newlib/libc/sys/rtems/include/limits.h or in a new file.
I tried to run the code below on my FAT filesystem, taken directly from
the scandir(3) man page.
/* print files in current directory in reverse order */
#include <dirent.h>
main(){
struct dirent **namelist;
int n;
n = scandir(".", &namelist, 0, NULL);
if (n < 0)
perror("scandir");
else {
while(n--) {
printf("%s\n", namelist[n]->d_name);
free(namelist[n]);
}
free(namelist);
}
}
--
Comment (by gedare):
Attached patch should work but I haven't tested it yet.
--
Ticket URL: <http://devel.rtems.org/ticket/1394#comment:5>
RTEMS Project <http://www.rtems.org/>
RTEMS Project
More information about the bugs
mailing list