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

fionread, used in poll_oneoff on Unix, may overflow for large files #578

Closed
marmistrz opened this issue Nov 15, 2019 · 0 comments · Fixed by #881
Closed

fionread, used in poll_oneoff on Unix, may overflow for large files #578

marmistrz opened this issue Nov 15, 2019 · 0 comments · Fixed by #881
Labels
wasi:impl Issues pertaining to WASI implementation in Wasmtime

Comments

@marmistrz
Copy link
Collaborator

marmistrz commented Nov 15, 2019

ioctl_tty(2) says that fionread accepts an int*, which may overflow for large files.
Incidentally, the correct size will be returned if we pass a pointer to int64_t, but this something we probably shouldn't rely on.

We probably should use a different syscall (fstat?) to query file size for regular files instead of fionread. Relevant discussion occurs in #552.
See this example C code which exhibits this behavior.

#include <stdio.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <stdint.h>

int main(int argc, char** argv) {
	if (argc != 2) {
		fprintf(stderr, "Usage: %s [file path]\n", argv[0]);
		exit(1);
	}
	FILE* file = fopen(argv[1], "r");
	if (file == NULL) {
		perror("opening file");
		exit(1);
	}
	int fd = fileno(file);
	int avail = 0;
	//int64_t avail = 0;
	int ret = ioctl(fd, FIONREAD, &avail);
	if (ret == -1) {
		perror("ioctl");
		exit(1);
	}

	printf("Bytes available: %d\n", avail);
	//printf("Bytes available: %ld\n", avail);
	return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
wasi:impl Issues pertaining to WASI implementation in Wasmtime
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants