Skip to content

Commit ca2baa3

Browse files
rscharfegitster
authored andcommitted
compat: move strdup(3) replacement to its own file
Move our implementation of strdup(3) out of compat/nedmalloc/ and allow it to be used independently from USE_NED_ALLOCATOR. The original nedmalloc doesn't come with strdup() and doesn't need it. Only _users_ of nedmalloc need it, which was added when we imported it to our compat/ hierarchy. This reduces the difference of our copy of nedmalloc from the original, making it easier to update, and allows for easier testing and reusing of our version of strdup(). Signed-off-by: Rene Scharfe <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent e0c1cea commit ca2baa3

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
@@ -1442,8 +1447,14 @@ ifdef NATIVE_CRLF
14421447
endif
14431448

14441449
ifdef USE_NED_ALLOCATOR
1445-
COMPAT_CFLAGS += -Icompat/nedmalloc
1446-
COMPAT_OBJS += compat/nedmalloc/nedmalloc.o
1450+
COMPAT_CFLAGS += -Icompat/nedmalloc
1451+
COMPAT_OBJS += compat/nedmalloc/nedmalloc.o
1452+
OVERRIDE_STRDUP = YesPlease
1453+
endif
1454+
1455+
ifdef OVERRIDE_STRDUP
1456+
COMPAT_CFLAGS += -DOVERRIDE_STRDUP
1457+
COMPAT_OBJS += compat/strdup.o
14471458
endif
14481459

14491460
ifdef GIT_TEST_CMP_USE_COPIED_CONTEXT
@@ -1993,7 +2004,7 @@ endif
19932004

19942005
ifdef USE_NED_ALLOCATOR
19952006
compat/nedmalloc/nedmalloc.sp compat/nedmalloc/nedmalloc.o: EXTRA_CPPFLAGS = \
1996-
-DNDEBUG -DOVERRIDE_STRDUP -DREPLACE_SYSTEM_ALLOCATOR
2007+
-DNDEBUG -DREPLACE_SYSTEM_ALLOCATOR
19972008
compat/nedmalloc/nedmalloc.sp: SPARSE_FLAGS += -Wno-non-pointer-null
19982009
endif
19992010

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
@@ -646,6 +646,14 @@ void *gitmemmem(const void *haystack, size_t haystacklen,
646646
const void *needle, size_t needlelen);
647647
#endif
648648

649+
#ifdef OVERRIDE_STRDUP
650+
#ifdef strdup
651+
#undef strdup
652+
#endif
653+
#define strdup gitstrdup
654+
char *gitstrdup(const char *s);
655+
#endif
656+
649657
#ifdef NO_GETPAGESIZE
650658
#define getpagesize() sysconf(_SC_PAGESIZE)
651659
#endif

0 commit comments

Comments
 (0)