Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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 this issue Apr 5, 2023 · 2 comments · May be fixed by phoenix-rtos/libphoenix#346
Assignees

Comments

@mateusz-bloch
Copy link
Member

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
Copy link
Member

nalajcie commented Apr 5, 2023

Please note that:

  • when function exits with error, output parameter might be left unchanged - IMHO You shouldn't test it unless it's explicitly stated it's also set on error paths
  • most of our FS-related functions are implemented inside particular filesystem - it's worth to check also how the function behaves on jffs2 and dummyfs (I believe it's possible to test on zynq-qemu as rootfs is jffs2 there and /dev is dummyfs ; currently it's not possible to test other FSes on ia32 due to filesystems/ext2: ext2 doesn't support mounting other filesystems #114)
  • due to above - please state on which filesystems the bug is present (preferably all fliesystems in single issue)

@mateusz-bloch mateusz-bloch changed the title stat() completion successful and set wrong errno, when a component of the path prefix is not a directory stat() completion successful and set wrong errno, when a component of the path prefix is not a directory [ext2, dummyfs, jffs2] Apr 21, 2023
@jmaksymowicz jmaksymowicz self-assigned this Feb 16, 2024
@jmaksymowicz
Copy link
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants