Skip to content
Myr

jv_file.c: check to see if the file is a directory and fail

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Not all stdio implementations disallow one to open a directory with fopen(3) and so we specifically check for directories as it is also in the standard search path.

authored by Roland C. Dowdeswell · committed by Nico Williams · parent 7fd9e86 · Browse files · 1 file+18 added−1 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+18 added−1 removed

  • other:+18 added−1 removed

1 file+18 added−1 removed

modified:src/jv_file.c+18 added-1 removed
1#include <sys/stat.h>23#include <errno.h>4#include <fcntl.h>5#include <stdio.h>6#include <stdlib.h>7#include <string.h>8#include <unistd.h>9#include "jv.h"10#include "jv_unicode.h"1112jv jv_load_file(const char* filename, int raw) {13 struct stat sb;14 int fd = open(filename, O_RDONLY);15 if (fd == -1) {16 return jv_invalid_with_msg(jv_string_fmt("Could not open %s: %s",17 filename,18 strerror(errno)));19 }20 if (fstat(fd, &sb) == -1 || S_ISDIR(sb.st_mode)) {21 close(fd);22 return jv_invalid_with_msg(jv_string_fmt("Could not open %s: %s",23 filename,24 "It's a directory"));25 }10 FILE* file = fopen(filename, "r");26 FILE* file = fdopen(fd, "r");27 struct jv_parser* parser = NULL;28 jv data;29 if (!file) {30 close(fd);31 return jv_invalid_with_msg(jv_string_fmt("Could not open %s: %s",32 filename,33 strerror(errno)));34 }
47 unmodified lines