#1394 closed defect (fixed)

scandir() fails due to MAXNAMELEN is incorrect

Reported by: Daniel Hellstrom Owned by: Chris Johns
Priority: normal Milestone: 5.1
Component: tool/newlib Version: 4.10
Severity: normal Keywords:
Cc: joel.sherrill@…, sebastian.huber@… Blocked By:
Blocking:

Description (last modified by Gedare Bloom)

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);

}

}

Attachments (2)

0001-rtems-make-MAXNAMLEN-match-with-NAME_MAX.patch (653 bytes) - added by Gedare Bloom on Feb 25, 2015 at 7:06:54 PM.
Possible Fix
0001-Add-simple-test-for-scandir-on-all-file-systems-test.patch (13.4 KB) - added by Joel Sherrill on Mar 3, 2015 at 9:17:09 PM.
fstest suite scandir() family of tests

Download all attachments as: .zip

Change History (16)

comment:1 Changed on Mar 28, 2013 at 8:31:24 AM by Sebastian Huber

Cc: Sebastian Huber added
Milestone: 2

comment:2 Changed on Mar 28, 2013 at 9:30:17 AM by Sebastian Huber

Milestone: 4.11

I just looked in the latest Newlib sources. The MAXNAMELEN vs. NAME_MAX problem still exists:

./newlib/libc/sys/rtems/sys/syslimits.h:#define NAME_MAX 255 /* max bytes in a file name */
./newlib/libc/sys/rtems/sys/dirent.h: char d_name[NAME_MAX + 1];

./newlib/libc/include/dirent.h:#if !defined(MAXNAMLEN) && !defined(_POSIX_SOURCE)
./newlib/libc/include/dirent.h:#define MAXNAMLEN 1024

0x0202acd0 in scandir (dirname=<optimized out>, namelist=0x203feb0, select=0x200149c <select1>, dcomp=0x0) at /home/sh/archive/gcc-4.6.3/newlib/libc/posix/scandir.c:117
117 p = (struct dirent *)malloc(DIRSIZ(d));
Value returned is $1 = 1
(gdb) s
malloc (size=4294966555) at /home/sh/git-rtems/c/src/../../cpukit/libcsupport/src/malloc.c:33
33 MSBUMP(malloc_calls, 1);

The psxtests/psxreaddir is quite sloppy since the scandir() return status is only printed:

scandir status: -1

comment:3 Changed on Nov 23, 2014 at 4:06:57 PM by Gedare Bloom

Description: modified (diff)
Version: unknown4.11

comment:4 Changed on Nov 23, 2014 at 4:11:19 PM by Joel Sherrill

Version: 4.114.10

This was reported in the 4.10 development cycle. 4.9 used newlib 1.16.0 and 4.10 used 1.18.0.

Changed on Feb 25, 2015 at 7:06:54 PM by Gedare Bloom

Possible Fix

comment:5 Changed on Feb 25, 2015 at 7:07:53 PM by Gedare Bloom

Description: modified (diff)

Attached patch should work but I haven't tested it yet.

comment:6 Changed on Mar 3, 2015 at 4:35:58 PM by Gedare Bloom

I don't have a test case for this.

Changed on Mar 3, 2015 at 9:17:09 PM by Joel Sherrill

fstest suite scandir() family of tests

comment:7 Changed on Mar 3, 2015 at 9:45:18 PM by Gedare Bloom

Further examination in newlib shows that this bug should no longer manifest itself. However, we might want to consider adding the tests and even upstreaming the patch for making the two defines match to avoid any future problems. I don't have a fix for 4.10.

comment:8 Changed on Mar 4, 2015 at 8:47:47 PM by Joel Sherrill

Added new set of tests fsscandir01 in fstests for code shown in this PR.

comment:9 Changed on Mar 4, 2015 at 9:18:58 PM by Gedare Bloom

Milestone: 4.114.10.3

Move to 4.10.3 in case we want to provide a fix there. This problem is resolved in newlib already.

comment:10 Changed on Jan 24, 2017 at 9:08:54 AM by Sebastian Huber <sebastian.huber@…>

In 6af2221aac489acac54c2daaf9702a926ec1e263/rtems:

fsscandir01: Check MAXNAMLEN and NAME_MAX

Update #1394.

comment:12 Changed on Jan 25, 2017 at 12:24:00 PM by Sebastian Huber

Resolution: fixed
Status: assignedclosed

Fix with next Newlib snapshot.

comment:13 Changed on May 11, 2017 at 7:31:02 AM by Sebastian Huber

Milestone: 4.124.12.0

comment:14 Changed on Nov 9, 2017 at 6:27:14 AM by Sebastian Huber

Milestone: 4.12.05.1

Milestone renamed

Note: See TracTickets for help on using tickets.