From ecf44c784c27928457e740b80096a03bc4e2ec86 Mon Sep 17 00:00:00 2001 From: ISSOtm Date: Fri, 20 Mar 2020 01:24:53 +0100 Subject: [PATCH] Reject including directories --- src/asm/fstack.c | 33 +++++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/src/asm/fstack.c b/src/asm/fstack.c index 2f834df4..bdd917ee 100644 --- a/src/asm/fstack.c +++ b/src/asm/fstack.c @@ -10,12 +10,15 @@ * FileStack routines */ +#include +#include #include #include #include #include #include #include +#include #include "asm/fstack.h" #include "asm/lexer.h" @@ -349,23 +352,34 @@ static void printdep(const char *fileName) } } +static FILE *getFile(char const *pathname) +{ + struct stat statbuf; + + if (stat(pathname, &statbuf) != 0) + return NULL; + + /* Reject directories */ + if (S_ISDIR(statbuf.st_mode)) + return NULL; + + return fopen(pathname, "rb"); +} + FILE *fstk_FindFile(char const *fname, char **incPathUsed) { - char path[_MAX_PATH]; - int32_t i; - FILE *f; - if (fname == NULL) return NULL; - f = fopen(fname, "rb"); + char path[_MAX_PATH]; + FILE *f = getFile(fname); - if (f != NULL || errno != ENOENT) { + if (f) { printdep(fname); return f; } - for (i = 0; i < NextIncPath; ++i) { + for (size_t i = 0; i < NextIncPath; ++i) { /* * The function snprintf() does not write more than `size` bytes * (including the terminating null byte ('\0')). If the output @@ -381,9 +395,8 @@ FILE *fstk_FindFile(char const *fname, char **incPathUsed) if (fullpathlen >= (int)sizeof(path)) continue; - f = fopen(path, "rb"); - - if (f != NULL || errno != ENOENT) { + f = getFile(path); + if (f) { printdep(path); if (incPathUsed)