Skip to content

Commit

Permalink
Check whether source file is not a directory
Browse files Browse the repository at this point in the history
Prevent directory.
Accept symbolic link.

テストで失敗する
  • Loading branch information
tyfkda committed Sep 2, 2023
1 parent de015fc commit c6e1d0e
Show file tree
Hide file tree
Showing 10 changed files with 24 additions and 18 deletions.
5 changes: 3 additions & 2 deletions src/as/as.c
Original file line number Diff line number Diff line change
Expand Up @@ -450,8 +450,9 @@ int main(int argc, char *argv[]) {

if (iarg < argc) {
for (int i = iarg; i < argc; ++i) {
FILE *fp = fopen(argv[i], "r");
if (fp == NULL)
const char *filename = argv[i];
FILE *fp;
if (!is_file(filename) || (fp = fopen(filename, "r")) == NULL)
error("Cannot open %s\n", argv[i]);
parse_file(fp, argv[i], section_irs, &label_table);
fclose(fp);
Expand Down
4 changes: 2 additions & 2 deletions src/cc/cc1.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ int main(int argc, char *argv[]) {
if (iarg < argc) {
for (int i = iarg; i < argc; ++i) {
const char *filename = argv[i];
FILE *ifp = fopen(filename, "r");
if (ifp == NULL)
FILE *ifp;
if (!is_file(filename) || (ifp = fopen(filename, "r")) == NULL)
error("Cannot open file: %s\n", filename);
compile1(ifp, filename, toplevel);
fclose(ifp);
Expand Down
4 changes: 2 additions & 2 deletions src/cpp/cpp.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ int main(int argc, char *argv[]) {
if (iarg < argc) {
for (int i = iarg; i < argc; ++i) {
const char *filename = argv[i];
FILE *fp = fopen(filename, "r");
if (fp == NULL)
FILE *fp;
if (!is_file(filename) || (fp = fopen(filename, "r")) == NULL)
error("Cannot open file: %s\n", filename);
fprintf(ofp, "# 1 \"%s\" 1\n", filename);
preprocess(fp, filename);
Expand Down
5 changes: 3 additions & 2 deletions src/cpp/preprocessor.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ static FILE *search_sysinc_next(const char *dir, const char *path, char **pfn) {
FILE *fp = NULL;
char *fn = cat_path_cwd(v->data[idx], path);
if (registered_pragma_once(fn) ||
(fp = fopen(fn, "r")) != NULL) {
(is_file(fn) && (fp = fopen(fn, "r")) != NULL)) {
*pfn = fn;
return fp;
}
Expand Down Expand Up @@ -176,7 +176,8 @@ static void handle_include(const char *p, Stream *stream, bool next) {
fn = cat_path_cwd(dir, path);
if (registered_pragma_once(fn))
return;
fp = fopen(fn, "r");
if (is_file(fn))
fp = fopen(fn, "r");
}
if (fp == NULL) {
if (next) fp = search_sysinc_next(dir, path, &fn);
Expand Down
4 changes: 2 additions & 2 deletions src/ld/archive.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ static uint32_t read4be(FILE *fp) {
}

Archive *load_archive(const char *filename) {
FILE *fp = fopen(filename, "r");
if (fp == NULL)
FILE *fp;
if (!is_file(filename) || (fp = fopen(filename, "r")) == NULL)
return NULL;

Archive *ar = malloc_or_die(sizeof(*ar));
Expand Down
4 changes: 2 additions & 2 deletions src/ld/elfobj.c
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,8 @@ void elfobj_init(ElfObj *elfobj) {
}

bool open_elf(const char *fn, ElfObj *elfobj) {
FILE *fp = fopen(fn, "r");
if (fp == NULL) {
FILE *fp;
if (!is_file(fn) || (fp = fopen(fn, "r")) == NULL) {
fprintf(stderr, "cannot open: %s\n", fn);
} else {
if (read_elf(elfobj, fp, fn))
Expand Down
6 changes: 6 additions & 0 deletions src/util/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <stdbool.h>
#include <stdlib.h> // malloc
#include <string.h> // strcmp
#include <sys/stat.h>

#include "../version.h"
#include "table.h"
Expand Down Expand Up @@ -241,6 +242,11 @@ void put_padding(FILE *fp, uintptr_t start) {
}
}

bool is_file(const char *path) {
struct stat st;
return stat(path, &st) == 0 && S_ISREG(st.st_mode);
}

void show_version(const char *exe) {
printf("%s %s\n", exe, VERSION);
}
Expand Down
1 change: 1 addition & 0 deletions src/util/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ char *join_paths(const char *paths[]);
char *get_ext(const char *filename);
char *change_ext(const char *path, const char *ext);
void put_padding(FILE *fp, uintptr_t start);
bool is_file(const char *path);

void show_version(const char *exe);

Expand Down
3 changes: 1 addition & 2 deletions src/wcc/wcc.c
Original file line number Diff line number Diff line change
Expand Up @@ -1001,8 +1001,7 @@ static void preprocess_and_compile(FILE *ppout, Vector *sources) {
const char *filename = sources->data[i];
FILE *ifp;
if (filename != NULL) {
ifp = fopen(filename, "r");
if (ifp == NULL)
if (!is_file(filename) || (ifp = fopen(filename, "r")) == NULL)
error("Cannot open file: %s\n", filename);
} else {
ifp = stdin;
Expand Down
6 changes: 2 additions & 4 deletions src/xcc/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -515,10 +515,8 @@ int main(int argc, char *argv[]) {
}
#endif

enum SourceType st = UnknownSource;
if (src == NULL) {
st = src_type;
} else {
enum SourceType st = src_type;
if (src != NULL) {
char *ext = get_ext(src);
if (strcasecmp(ext, "c") == 0) st = Clanguage;
else if (strcasecmp(ext, "s") == 0) st = Assembly;
Expand Down

0 comments on commit c6e1d0e

Please sign in to comment.