Handle errors when opening source file

Before this commit, opening a file for which the user didn't have
permission resulted in a "Bad file descriptor" error.
This commit is contained in:
Jakub Kądziołka
2021-03-30 23:24:15 +02:00
committed by Eldred Habert
parent aaa92659ea
commit d08bcc455d

View File

@@ -473,6 +473,11 @@ struct LexerState *lexer_OpenFile(char const *path)
state->path = path; state->path = path;
state->isFile = true; state->isFile = true;
state->fd = isStdin ? STDIN_FILENO : open(path, O_RDONLY); state->fd = isStdin ? STDIN_FILENO : open(path, O_RDONLY);
if (state->fd < 0) {
error("Failed to open file \"%s\": %s\n", path, strerror(errno));
free(state);
return NULL;
}
state->isMmapped = false; /* By default, assume it won't be mmap()ed */ state->isMmapped = false; /* By default, assume it won't be mmap()ed */
if (!isStdin && fileInfo.st_size > 0) { if (!isStdin && fileInfo.st_size > 0) {
/* Try using `mmap` for better performance */ /* Try using `mmap` for better performance */