phoenix-rtos / phoenix-rtos-project

Sample project using Phoenix-RTOS
https://phoenix-rtos.com
BSD 3-Clause "New" or "Revised" License
43 stars 32 forks source link

stat() completion successful and set wrong errno, when a component of the path prefix is not a directory [ext2, dummyfs, jffs2] #682

Open mateusz-bloch opened 1 year ago

mateusz-bloch commented 1 year ago

stat() completion successful and set wrong errno, when a component of the path is existing regular file, not a directory

Tested between ia32-generic and host-generic

Code to reproduction:

#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <errno.h>

int main(void)
{
    struct stat buf;

    FILE *fp = fopen("testfile.txt", "w");
    fclose(fp);

    errno = 0;
    stat("testfile.txt/", &buf);
    printf("Regular File: %d\n",S_ISREG(buf.st_mode));
    printf("Directory: %d\n",S_ISDIR(buf.st_mode));
    printf("Errno: %d, %s\n", errno, strerror(errno));

    remove("testfile.txt");

    return 0;
}

Outcome from ia32-generic: image

Outcome from host-generic: image

Documentation: image image

nalajcie commented 1 year ago

Please note that:

jmaksymowicz commented 6 months ago

I managed to track down the bug to the resolve_path function in libphoenix. According to Open Group it should set errno to ENOTDIR when:

[...] the file_name argument contains at least one non-\<slash> character and ends with one or more trailing \<slash> characters and the last pathname component names an existing file that is neither a directory nor a symbolic link to a directory.

Instead, the function simply ignores trailing slashes, so it cannot behave correctly.