jv_file.c: check to see if the file is a directory and fail
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.
This commit is contained in:
1 parent
7fd9e86ea6
commit
90bc29c1b5
1 file changed
+18
-1
+18
-1
@@ -1,16 +1,33 @@
|
|||||||
|
#include <sys/stat.h>
|
||||||
|
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
|
#include <fcntl.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <unistd.h>
|
||||||
#include "jv.h"
|
#include "jv.h"
|
||||||
#include "jv_unicode.h"
|
#include "jv_unicode.h"
|
||||||
|
|
||||||
jv jv_load_file(const char* filename, int raw) {
|
jv jv_load_file(const char* filename, int raw) {
|
||||||
FILE* file = fopen(filename, "r");
|
struct stat sb;
|
||||||
|
int fd = open(filename, O_RDONLY);
|
||||||
|
if (fd == -1) {
|
||||||
|
return jv_invalid_with_msg(jv_string_fmt("Could not open %s: %s",
|
||||||
|
filename,
|
||||||
|
strerror(errno)));
|
||||||
|
}
|
||||||
|
if (fstat(fd, &sb) == -1 || S_ISDIR(sb.st_mode)) {
|
||||||
|
close(fd);
|
||||||
|
return jv_invalid_with_msg(jv_string_fmt("Could not open %s: %s",
|
||||||
|
filename,
|
||||||
|
"It's a directory"));
|
||||||
|
}
|
||||||
|
FILE* file = fdopen(fd, "r");
|
||||||
struct jv_parser* parser = NULL;
|
struct jv_parser* parser = NULL;
|
||||||
jv data;
|
jv data;
|
||||||
if (!file) {
|
if (!file) {
|
||||||
|
close(fd);
|
||||||
return jv_invalid_with_msg(jv_string_fmt("Could not open %s: %s",
|
return jv_invalid_with_msg(jv_string_fmt("Could not open %s: %s",
|
||||||
filename,
|
filename,
|
||||||
strerror(errno)));
|
strerror(errno)));
|
||||||
|
|||||||
Reference in new issue
Block a user