Skip to content

Commit cba2252

Browse files
drafnelgitster
authored andcommitted
Add compat/fopen.c which returns NULL on attempt to open directory
Some systems do not fail as expected when fread et al. are called on a directory stream. Replace fopen on such systems which will fail when the supplied path is a directory. Signed-off-by: Brandon Casey <casey@nrlssc.navy.mil> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 40aab81 commit cba2252

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

Makefile

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ all::
33

44
# Define V=1 to have a more verbose compile.
55
#
6+
# Define FREAD_READS_DIRECTORIES if your are on a system which succeeds
7+
# when attempting to read from an fopen'ed directory.
8+
#
69
# Define NO_OPENSSL environment variable if you do not have OpenSSL.
710
# This also implies MOZILLA_SHA1.
811
#
@@ -618,6 +621,10 @@ endif
618621
ifdef NO_C99_FORMAT
619622
BASIC_CFLAGS += -DNO_C99_FORMAT
620623
endif
624+
ifdef FREAD_READS_DIRECTORIES
625+
COMPAT_CFLAGS += -DFREAD_READS_DIRECTORIES
626+
COMPAT_OBJS += compat/fopen.o
627+
endif
621628
ifdef NO_SYMLINK_HEAD
622629
BASIC_CFLAGS += -DNO_SYMLINK_HEAD
623630
endif

compat/fopen.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include "../git-compat-util.h"
2+
#undef fopen
3+
FILE *git_fopen(const char *path, const char *mode)
4+
{
5+
FILE *fp;
6+
struct stat st;
7+
8+
if (mode[0] == 'w' || mode[0] == 'a')
9+
return fopen(path, mode);
10+
11+
if (!(fp = fopen(path, mode)))
12+
return NULL;
13+
14+
if (fstat(fileno(fp), &st)) {
15+
fclose(fp);
16+
return NULL;
17+
}
18+
19+
if (S_ISDIR(st.st_mode)) {
20+
fclose(fp);
21+
errno = EISDIR;
22+
return NULL;
23+
}
24+
25+
return fp;
26+
}

git-compat-util.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,11 @@ void *gitmemmem(const void *haystack, size_t haystacklen,
204204
const void *needle, size_t needlelen);
205205
#endif
206206

207+
#ifdef FREAD_READS_DIRECTORIES
208+
#define fopen(a,b) git_fopen(a,b)
209+
extern FILE *git_fopen(const char*, const char*);
210+
#endif
211+
207212
#ifdef __GLIBC_PREREQ
208213
#if __GLIBC_PREREQ(2, 1)
209214
#define HAVE_STRCHRNUL

0 commit comments

Comments
 (0)