Skip to content

Commit 815a73f

Browse files
committed
Merge branch 'rs/compat-strdup' into maint
Code cleanup. * rs/compat-strdup: compat: move strdup(3) replacement to its own file
2 parents 3d54b93 + ca2baa3 commit 815a73f

File tree

4 files changed

+33
-19
lines changed

4 files changed

+33
-19
lines changed

Makefile

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,11 @@ all::
296296
# Define USE_NED_ALLOCATOR if you want to replace the platforms default
297297
# memory allocators with the nedmalloc allocator written by Niall Douglas.
298298
#
299+
# Define OVERRIDE_STRDUP to override the libc version of strdup(3).
300+
# This is necessary when using a custom allocator in order to avoid
301+
# crashes due to allocation and free working on different 'heaps'.
302+
# It's defined automatically if USE_NED_ALLOCATOR is set.
303+
#
299304
# Define NO_REGEX if you have no or inferior regex support in your C library.
300305
#
301306
# Define HAVE_DEV_TTY if your system can open /dev/tty to interact with the
@@ -1456,8 +1461,14 @@ ifdef NATIVE_CRLF
14561461
endif
14571462

14581463
ifdef USE_NED_ALLOCATOR
1459-
COMPAT_CFLAGS += -Icompat/nedmalloc
1460-
COMPAT_OBJS += compat/nedmalloc/nedmalloc.o
1464+
COMPAT_CFLAGS += -Icompat/nedmalloc
1465+
COMPAT_OBJS += compat/nedmalloc/nedmalloc.o
1466+
OVERRIDE_STRDUP = YesPlease
1467+
endif
1468+
1469+
ifdef OVERRIDE_STRDUP
1470+
COMPAT_CFLAGS += -DOVERRIDE_STRDUP
1471+
COMPAT_OBJS += compat/strdup.o
14611472
endif
14621473

14631474
ifdef GIT_TEST_CMP_USE_COPIED_CONTEXT
@@ -2029,7 +2040,7 @@ endif
20292040

20302041
ifdef USE_NED_ALLOCATOR
20312042
compat/nedmalloc/nedmalloc.sp compat/nedmalloc/nedmalloc.o: EXTRA_CPPFLAGS = \
2032-
-DNDEBUG -DOVERRIDE_STRDUP -DREPLACE_SYSTEM_ALLOCATOR
2043+
-DNDEBUG -DREPLACE_SYSTEM_ALLOCATOR
20332044
compat/nedmalloc/nedmalloc.sp: SPARSE_FLAGS += -Wno-non-pointer-null
20342045
endif
20352046

compat/nedmalloc/nedmalloc.c

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -948,22 +948,6 @@ void **nedpindependent_comalloc(nedpool *p, size_t elems, size_t *sizes, void **
948948
return ret;
949949
}
950950

951-
#ifdef OVERRIDE_STRDUP
952-
/*
953-
* This implementation is purely there to override the libc version, to
954-
* avoid a crash due to allocation and free on different 'heaps'.
955-
*/
956-
char *strdup(const char *s1)
957-
{
958-
size_t len = strlen(s1) + 1;
959-
char *s2 = malloc(len);
960-
961-
if (s2)
962-
memcpy(s2, s1, len);
963-
return s2;
964-
}
965-
#endif
966-
967951
#if defined(__cplusplus)
968952
}
969953
#endif

compat/strdup.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#include "../git-compat-util.h"
2+
3+
char *gitstrdup(const char *s1)
4+
{
5+
size_t len = strlen(s1) + 1;
6+
char *s2 = malloc(len);
7+
8+
if (s2)
9+
memcpy(s2, s1, len);
10+
return s2;
11+
}

git-compat-util.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -664,6 +664,14 @@ void *gitmemmem(const void *haystack, size_t haystacklen,
664664
const void *needle, size_t needlelen);
665665
#endif
666666

667+
#ifdef OVERRIDE_STRDUP
668+
#ifdef strdup
669+
#undef strdup
670+
#endif
671+
#define strdup gitstrdup
672+
char *gitstrdup(const char *s);
673+
#endif
674+
667675
#ifdef NO_GETPAGESIZE
668676
#define getpagesize() sysconf(_SC_PAGESIZE)
669677
#endif

0 commit comments

Comments
 (0)