shithub: rgbds

Download patch

ref: ecf44c784c27928457e740b80096a03bc4e2ec86
parent: 5bca1172ffb910566ccdf97fcd8d6a4c98ade456
author: ISSOtm <eldredhabert0@gmail.com>
date: Thu Mar 19 21:24:53 EDT 2020

Reject including directories

--- a/src/asm/fstack.c
+++ b/src/asm/fstack.c
@@ -10,6 +10,8 @@
  * FileStack routines
  */
 
+#include <sys/types.h>
+#include <sys/stat.h>
 #include <errno.h>
 #include <limits.h>
 #include <stdint.h>
@@ -16,6 +18,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <unistd.h>
 
 #include "asm/fstack.h"
 #include "asm/lexer.h"
@@ -349,23 +352,34 @@
 	}
 }
 
-FILE *fstk_FindFile(char const *fname, char **incPathUsed)
+static FILE *getFile(char const *pathname)
 {
-	char path[_MAX_PATH];
-	int32_t i;
-	FILE *f;
+	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)
+{
 	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 @@
 		if (fullpathlen >= (int)sizeof(path))
 			continue;
 
-		f = fopen(path, "rb");
-
-		if (f != NULL || errno != ENOENT) {
+		f = getFile(path);
+		if (f) {
 			printdep(path);
 
 			if (incPathUsed)