From 7eba1d6bc6b26e2888321df4d382424f0a1e4051 Mon Sep 17 00:00:00 2001 From: Zeex Date: Tue, 31 Mar 2015 00:57:03 +0600 Subject: [PATCH] Fix freeze when include file is a directory This adds an extra check in plungequalifiedfile() that ensures that the file being includes is not a directory. It seems to be the only place where we need to check that, in other places files are opened by exact name. The bug appears to be specific to the Linux compiler. On Windows fopen() will return NULL if trying to open a directory. Fixes #41. --- source/compiler/sc2.c | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/source/compiler/sc2.c b/source/compiler/sc2.c index 64dd1b4..cc94c6d 100644 --- a/source/compiler/sc2.c +++ b/source/compiler/sc2.c @@ -26,6 +26,7 @@ #include #include #include +#include #include "lstring.h" #include "sc.h" #if defined LINUX || defined __FreeBSD__ || defined __OpenBSD__ @@ -125,24 +126,36 @@ SC_FUNC void clearstk(void) SC_FUNC int plungequalifiedfile(char *name) { static char *extensions[] = { ".inc", ".p", ".pawn" }; + int found; + struct stat st; FILE *fp; char *ext; int ext_idx; ext_idx=0; do { - fp=(FILE*)pc_opensrc(name); + found=TRUE; + stat(name, &st); + if (S_ISDIR(st.st_mode)) { + found=FALSE; /* ignore directories with the same name */ + } else { + fp=(FILE*)pc_opensrc(name); + if (fp==NULL) + found=FALSE; + } /* if */ ext=strchr(name,'\0'); /* save position */ - if (fp==NULL) { + if (!found) { /* try to append an extension */ strcpy(ext,extensions[ext_idx]); fp=(FILE*)pc_opensrc(name); - if (fp==NULL) + if (fp==NULL) { *ext='\0'; /* on failure, restore filename */ + found=FALSE; + } } /* if */ ext_idx++; - } while (fp==NULL && ext_idx<(sizeof extensions / sizeof extensions[0])); - if (fp==NULL) { + } while (!found && ext_idx<(sizeof extensions / sizeof extensions[0])); + if (!found) { *ext='\0'; /* restore filename */ return FALSE; } /* if */