Skip to content
This repository was archived by the owner on Nov 9, 2017. It is now read-only.

Commit 6c642a8

Browse files
Filipe Cabecinhasgitster
authored andcommitted
compate/clipped-write.c: large write(2) fails on Mac OS X/XNU
Due to a bug in the Darwin kernel, write(2) calls have a maximum size of INT_MAX bytes. Introduce a new compat function, clipped_write(), that only writes at most INT_MAX bytes and returns the number of bytes written, as a substitute for write(2), and allow platforms that need this to enable it from the build mechanism with NEEDS_CLIPPED_WRITE. Set it for Mac OS X by default. It may be necessary to include this function on Windows, too. Signed-off-by: Filipe Cabecinhas <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 239222f commit 6c642a8

File tree

4 files changed

+27
-0
lines changed

4 files changed

+27
-0
lines changed

Makefile

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ all::
6969
# Define NO_MSGFMT_EXTENDED_OPTIONS if your implementation of msgfmt
7070
# doesn't support GNU extensions like --check and --statistics
7171
#
72+
# Define NEEDS_CLIPPED_WRITE if your write(2) cannot write more than
73+
# INT_MAX bytes at once (e.g. MacOS X).
74+
#
7275
# Define HAVE_PATHS_H if you have paths.h and want to use the default PATH
7376
# it specifies.
7477
#
@@ -1463,6 +1466,11 @@ ifndef NO_MSGFMT_EXTENDED_OPTIONS
14631466
MSGFMT += --check --statistics
14641467
endif
14651468

1469+
ifdef NEEDS_CLIPPED_WRITE
1470+
BASIC_CFLAGS += -DNEEDS_CLIPPED_WRITE
1471+
COMPAT_OBJS += compat/clipped-write.o
1472+
endif
1473+
14661474
ifneq (,$(XDL_FAST_HASH))
14671475
BASIC_CFLAGS += -DXDL_FAST_HASH
14681476
endif

compat/clipped-write.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#include "../git-compat-util.h"
2+
#undef write
3+
4+
/*
5+
* Version of write that will write at most INT_MAX bytes.
6+
* Workaround a xnu bug on Mac OS X
7+
*/
8+
ssize_t clipped_write(int fildes, const void *buf, size_t nbyte)
9+
{
10+
if (nbyte > INT_MAX)
11+
nbyte = INT_MAX;
12+
return write(fildes, buf, nbyte);
13+
}

config.mak.uname

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ ifeq ($(uname_S),Darwin)
9595
NO_MEMMEM = YesPlease
9696
USE_ST_TIMESPEC = YesPlease
9797
HAVE_DEV_TTY = YesPlease
98+
NEEDS_CLIPPED_WRITE = YesPlease
9899
COMPAT_OBJS += compat/precompose_utf8.o
99100
BASIC_CFLAGS += -DPRECOMPOSE_UNICODE
100101
endif

git-compat-util.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,11 @@ typedef unsigned long uintptr_t;
181181
#define probe_utf8_pathname_composition(a,b)
182182
#endif
183183

184+
#ifdef NEEDS_CLIPPED_WRITE
185+
ssize_t clipped_write(int fildes, const void *buf, size_t nbyte);
186+
#define write(x,y,z) clipped_write((x),(y),(z))
187+
#endif
188+
184189
#ifdef MKDIR_WO_TRAILING_SLASH
185190
#define mkdir(a,b) compat_mkdir_wo_trailing_slash((a),(b))
186191
extern int compat_mkdir_wo_trailing_slash(const char*, mode_t);

0 commit comments

Comments
 (0)