From 7813c6d937203ae4542d8b206949b3a8c8db70dd Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Sat, 2 Jan 2010 10:01:11 +1100 Subject: [PATCH 1/5] libreplace: some systems don't have memmem() added rep_memmem() and a testsuite (cherry picked from commit fef3c910da421e890925e5e61275fc457da87f6e) --- lib/replace/libreplace.m4 | 2 ++ lib/replace/replace.c | 23 +++++++++++++++++++++++ lib/replace/replace.h | 6 ++++++ lib/replace/test/testsuite.c | 37 +++++++++++++++++++++++++++++++++++++ 4 files changed, 68 insertions(+) diff --git a/lib/replace/libreplace.m4 b/lib/replace/libreplace.m4 index af85879..1353c1f 100644 --- a/lib/replace/libreplace.m4 +++ b/lib/replace/libreplace.m4 @@ -228,6 +228,8 @@ AC_HAVE_DECL(environ, [#include ]) AC_CHECK_FUNCS(strnlen) AC_CHECK_FUNCS(strtoull __strtoull strtouq strtoll __strtoll strtoq) +AC_CHECK_FUNCS(memmem) + # this test disabled as we don't actually need __VA_ARGS__ yet AC_TRY_CPP([ #define eprintf(...) fprintf(stderr, __VA_ARGS__) diff --git a/lib/replace/replace.c b/lib/replace/replace.c index fc15717..17fd46b 100644 --- a/lib/replace/replace.c +++ b/lib/replace/replace.c @@ -681,3 +681,26 @@ char *rep_realpath(const char *path, char *resolved_path) return NULL; } #endif + + +#ifndef HAVE_MEMMEM +void *rep_memmem(const void *haystack, size_t haystacklen, + const void *needle, size_t needlelen) +{ + if (needlelen == 0) { + return discard_const(haystack); + } + while (haystacklen >= needlelen) { + char *p = memchr(haystack, *(const char *)needle, + haystacklen-(needlelen-1)); + if (!p) return NULL; + if (memcmp(p, needle, needlelen) == 0) { + return p; + } + haystack = p+1; + haystacklen -= (p - (const char *)haystack) + 1; + } + return NULL; +} +#endif + diff --git a/lib/replace/replace.h b/lib/replace/replace.h index 6424d10..baf2368 100644 --- a/lib/replace/replace.h +++ b/lib/replace/replace.h @@ -140,6 +140,12 @@ char *rep_strdup(const char *s); void *rep_memmove(void *dest,const void *src,int size); #endif +#ifndef HAVE_MEMMEM +#define memmem rep_memmem +void *rep_memmem(const void *haystack, size_t haystacklen, + const void *needle, size_t needlelen); +#endif + #ifndef HAVE_MKTIME #define mktime rep_mktime /* prototype is in "system/time.h" */ diff --git a/lib/replace/test/testsuite.c b/lib/replace/test/testsuite.c index 7929f11..caa70d6 100644 --- a/lib/replace/test/testsuite.c +++ b/lib/replace/test/testsuite.c @@ -1015,6 +1015,42 @@ static int test_utimes(void) return true; } +static int test_memmem(void) +{ + char *s; + + printf("test: memmem\n"); + + s = memmem("foo", 3, "fo", 2); + if (strcmp(s, "foo") != 0) { + printf(__location__ ": Failed memmem\n"); + return false; + } + + s = memmem("foo", 3, "", 0); + if (strcmp(s, "foo") != 0) { + printf(__location__ ": Failed memmem\n"); + return false; + } + + s = memmem("foo", 4, "o", 1); + if (strcmp(s, "oo") != 0) { + printf(__location__ ": Failed memmem\n"); + return false; + } + + s = memmem("foobarfodx", 11, "fod", 3); + if (strcmp(s, "fodx") != 0) { + printf(__location__ ": Failed memmem\n"); + return false; + } + + printf("success: memmem\n"); + + return true; +} + + struct torture_context; bool torture_local_replace(struct torture_context *ctx) { @@ -1065,6 +1101,7 @@ bool torture_local_replace(struct torture_context *ctx) ret &= test_getifaddrs(); ret &= test_utime(); ret &= test_utimes(); + ret &= test_memmem(); return ret; } -- 1.7.9.5 From 119b8c69572f4c791e551ca90976c232c4053d6f Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Thu, 11 Feb 2010 20:18:50 +1100 Subject: [PATCH 2/5] libreplace: added replacements for dprintf() and vdprintf() these are very useful for writing files with formatted writes Pair-Programmed-With: Andrew Bartlett (cherry picked from commit d6fb64c51244529388b1f79ba8220ff608e1e4de) --- lib/replace/libreplace.m4 | 2 +- lib/replace/replace.c | 31 +++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/lib/replace/libreplace.m4 b/lib/replace/libreplace.m4 index 1353c1f..7a26deb 100644 --- a/lib/replace/libreplace.m4 +++ b/lib/replace/libreplace.m4 @@ -108,7 +108,7 @@ AC_CHECK_HEADERS(unix.h) AC_CHECK_FUNCS(seteuid setresuid setegid setresgid chroot bzero strerror) AC_CHECK_FUNCS(vsyslog setlinebuf mktime ftruncate chsize rename) AC_CHECK_FUNCS(waitpid wait4 strlcpy strlcat initgroups memmove strdup) -AC_CHECK_FUNCS(pread pwrite strndup strcasestr strtok_r mkdtemp dup2) +AC_CHECK_FUNCS(pread pwrite strndup strcasestr strtok_r mkdtemp dup2 dprintf vdprintf) AC_CHECK_FUNCS(isatty chown lchown link readlink symlink realpath) AC_HAVE_DECL(setresuid, [#include ]) AC_HAVE_DECL(setresgid, [#include ]) diff --git a/lib/replace/replace.c b/lib/replace/replace.c index 17fd46b..83966b1 100644 --- a/lib/replace/replace.c +++ b/lib/replace/replace.c @@ -704,3 +704,34 @@ void *rep_memmem(const void *haystack, size_t haystacklen, } #endif +#ifndef HAVE_VDPRINTF +int vdprintf(int fd, const char *format, va_list ap) +{ + char *s = NULL; + int ret; + + vasprintf(&s, format, ap); + if (s == NULL) { + errno = ENOMEM; + return -1; + } + ret = write(fd, s, strlen(s)); + free(s); + return ret; +} +#endif + +#ifndef HAVE_DPRINTF +int dprintf(int fd, const char *format, ...) +{ + int ret; + va_list ap; + + va_start(ap, format); + ret = vdprintf(fd, format, ap); + va_end(ap); + + return ret; +} +#endif + -- 1.7.9.5 From 2f97dc3a671f34b8c96c20c7a5206acec85d9a04 Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Wed, 24 Mar 2010 05:06:25 +1100 Subject: [PATCH 3/5] libreplace: fixed declaration of dprintf() on FreeBSD (cherry picked from commit a599319d0a389ff0c31dae8068cd7a78352aa9e7) --- lib/replace/replace.c | 4 ++-- lib/replace/replace.h | 10 ++++++++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/lib/replace/replace.c b/lib/replace/replace.c index 83966b1..b8e4e46 100644 --- a/lib/replace/replace.c +++ b/lib/replace/replace.c @@ -705,7 +705,7 @@ void *rep_memmem(const void *haystack, size_t haystacklen, #endif #ifndef HAVE_VDPRINTF -int vdprintf(int fd, const char *format, va_list ap) +int rep_vdprintf(int fd, const char *format, va_list ap) { char *s = NULL; int ret; @@ -722,7 +722,7 @@ int vdprintf(int fd, const char *format, va_list ap) #endif #ifndef HAVE_DPRINTF -int dprintf(int fd, const char *format, ...) +int rep_dprintf(int fd, const char *format, ...) { int ret; va_list ap; diff --git a/lib/replace/replace.h b/lib/replace/replace.h index baf2368..5199552 100644 --- a/lib/replace/replace.h +++ b/lib/replace/replace.h @@ -336,6 +336,16 @@ int rep_dlclose(void *handle); /* prototype is in system/network.h */ #endif +#ifndef HAVE_VDPRINTF +#define vdprintf rep_vdprintf +int rep_vdprintf(int fd, const char *format, va_list ap); +#endif + +#ifndef HAVE_DPRINTF +#define dprintf rep_dprintf +int rep_dprintf(int fd, const char *format, ...); +#endif + #ifndef PRINTF_ATTRIBUTE #if (__GNUC__ >= 3) && (__GNUC_MINOR__ >= 1 ) /** Use gcc attribute to check printf fns. a1 is the 1-based index of -- 1.7.9.5 From 1c45fb7b828b5079681f5a7049072c03710ceea7 Mon Sep 17 00:00:00 2001 From: Jelmer Vernooij Date: Sun, 13 May 2012 03:21:34 +0200 Subject: [PATCH 4/5] libreplace: Fix symbol names for snprintf/asprintf/vasprintf. Autobuild-User: Jelmer Vernooij Autobuild-Date: Sun May 13 05:16:28 CEST 2012 on sn-devel-104 (cherry picked from commit cf67da70c9a63c4dc63f287059321d6c36d1e19e) --- lib/replace/snprintf.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/replace/snprintf.c b/lib/replace/snprintf.c index bca7742..877d2a1 100644 --- a/lib/replace/snprintf.c +++ b/lib/replace/snprintf.c @@ -1187,7 +1187,7 @@ static int add_cnk_list_entry(struct pr_chunk_x **list, return max; } - int vsnprintf (char *str, size_t count, const char *fmt, va_list args) + int rep_vsnprintf (char *str, size_t count, const char *fmt, va_list args) { return dopr(str, count, fmt, args); } @@ -1200,7 +1200,7 @@ static int add_cnk_list_entry(struct pr_chunk_x **list, * that doesn't work properly according to the autoconf test. */ #if !defined(HAVE_SNPRINTF) || !defined(HAVE_C99_VSNPRINTF) - int snprintf(char *str,size_t count,const char *fmt,...) + int rep_snprintf(char *str,size_t count,const char *fmt,...) { size_t ret; va_list ap; @@ -1213,7 +1213,7 @@ static int add_cnk_list_entry(struct pr_chunk_x **list, #endif #ifndef HAVE_C99_VSNPRINTF - int printf(const char *fmt, ...) + int rep_printf(const char *fmt, ...) { va_list ap; int ret; @@ -1234,7 +1234,7 @@ static int add_cnk_list_entry(struct pr_chunk_x **list, #endif #ifndef HAVE_C99_VSNPRINTF - int fprintf(FILE *stream, const char *fmt, ...) + int rep_fprintf(FILE *stream, const char *fmt, ...) { va_list ap; int ret; @@ -1257,7 +1257,7 @@ static int add_cnk_list_entry(struct pr_chunk_x **list, #endif #ifndef HAVE_VASPRINTF - int vasprintf(char **ptr, const char *format, va_list ap) + int rep_vasprintf(char **ptr, const char *format, va_list ap) { int ret; va_list ap2; @@ -1280,7 +1280,7 @@ static int add_cnk_list_entry(struct pr_chunk_x **list, #ifndef HAVE_ASPRINTF - int asprintf(char **ptr, const char *format, ...) + int rep_asprintf(char **ptr, const char *format, ...) { va_list ap; int ret; -- 1.7.9.5 From 4d04a817a5298c56e80e307e57537bc683f89e51 Mon Sep 17 00:00:00 2001 From: Stefan Metzmacher Date: Tue, 13 Nov 2012 14:07:11 +0100 Subject: [PATCH 5/5] lib/replace: replace all *printf function if we replace snprintf (bug #9390) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This fixes segfaults in log level = 10 on Solaris. Signed-off-by: Stefan Metzmacher Signed-off-by: Björn Jacke Autobuild-User(master): Björn Jacke Autobuild-Date(master): Wed Nov 14 19:41:14 CET 2012 on sn-devel-104 (cherry picked from commit a15da3625850d97b3da1b02308c870f820007c52) --- lib/replace/replace.c | 4 ++-- lib/replace/replace.h | 42 ++++++++++++++++++++++++++++++------------ lib/replace/snprintf.c | 5 ++--- 3 files changed, 34 insertions(+), 17 deletions(-) diff --git a/lib/replace/replace.c b/lib/replace/replace.c index b8e4e46..85d0e36 100644 --- a/lib/replace/replace.c +++ b/lib/replace/replace.c @@ -704,7 +704,7 @@ void *rep_memmem(const void *haystack, size_t haystacklen, } #endif -#ifndef HAVE_VDPRINTF +#if !defined(HAVE_VDPRINTF) || !defined(HAVE_C99_VSNPRINTF) int rep_vdprintf(int fd, const char *format, va_list ap) { char *s = NULL; @@ -721,7 +721,7 @@ int rep_vdprintf(int fd, const char *format, va_list ap) } #endif -#ifndef HAVE_DPRINTF +#if !defined(HAVE_DPRINTF) || !defined(HAVE_C99_VSNPRINTF) int rep_dprintf(int fd, const char *format, ...) { int ret; diff --git a/lib/replace/replace.h b/lib/replace/replace.h index 5199552..a028d7a 100644 --- a/lib/replace/replace.h +++ b/lib/replace/replace.h @@ -336,16 +336,6 @@ int rep_dlclose(void *handle); /* prototype is in system/network.h */ #endif -#ifndef HAVE_VDPRINTF -#define vdprintf rep_vdprintf -int rep_vdprintf(int fd, const char *format, va_list ap); -#endif - -#ifndef HAVE_DPRINTF -#define dprintf rep_dprintf -int rep_dprintf(int fd, const char *format, ...); -#endif - #ifndef PRINTF_ATTRIBUTE #if (__GNUC__ >= 3) && (__GNUC_MINOR__ >= 1 ) /** Use gcc attribute to check printf fns. a1 is the 1-based index of @@ -366,7 +356,17 @@ int rep_dprintf(int fd, const char *format, ...); #endif #endif -#ifndef HAVE_VASPRINTF +#if !defined(HAVE_VDPRINTF) || !defined(HAVE_C99_VSNPRINTF) +#define vdprintf rep_vdprintf +int rep_vdprintf(int fd, const char *format, va_list ap) PRINTF_ATTRIBUTE(2,0); +#endif + +#if !defined(HAVE_DPRINTF) || !defined(HAVE_C99_VSNPRINTF) +#define dprintf rep_dprintf +int rep_dprintf(int fd, const char *format, ...) PRINTF_ATTRIBUTE(2,3); +#endif + +#if !defined(HAVE_VASPRINTF) || !defined(HAVE_C99_VSNPRINTF) #define vasprintf rep_vasprintf int rep_vasprintf(char **ptr, const char *format, va_list ap) PRINTF_ATTRIBUTE(2,0); #endif @@ -381,11 +381,29 @@ int rep_snprintf(char *,size_t ,const char *, ...) PRINTF_ATTRIBUTE(3,4); int rep_vsnprintf(char *,size_t ,const char *, va_list ap) PRINTF_ATTRIBUTE(3,0); #endif -#ifndef HAVE_ASPRINTF +#if !defined(HAVE_ASPRINTF) || !defined(HAVE_C99_VSNPRINTF) #define asprintf rep_asprintf int rep_asprintf(char **,const char *, ...) PRINTF_ATTRIBUTE(2,3); #endif +#if !defined(HAVE_C99_VSNPRINTF) +#ifdef REPLACE_BROKEN_PRINTF +/* + * We do not redefine printf by default + * as it breaks the build if system headers + * use __attribute__((format(printf, 3, 0))) + * instead of __attribute__((format(__printf__, 3, 0))) + */ +#define printf rep_printf +#endif +int rep_printf(const char *, ...) PRINTF_ATTRIBUTE(1,2); +#endif + +#if !defined(HAVE_C99_VSNPRINTF) +#define fprintf rep_fprintf +int rep_fprintf(FILE *stream, const char *, ...) PRINTF_ATTRIBUTE(2,3); +#endif + #ifndef HAVE_VSYSLOG #ifdef HAVE_SYSLOG #define vsyslog rep_vsyslog diff --git a/lib/replace/snprintf.c b/lib/replace/snprintf.c index 877d2a1..6b4a711 100644 --- a/lib/replace/snprintf.c +++ b/lib/replace/snprintf.c @@ -1256,7 +1256,7 @@ static int add_cnk_list_entry(struct pr_chunk_x **list, #endif -#ifndef HAVE_VASPRINTF +#if !defined(HAVE_VASPRINTF) || !defined(HAVE_C99_VSNPRINTF) int rep_vasprintf(char **ptr, const char *format, va_list ap) { int ret; @@ -1278,8 +1278,7 @@ static int add_cnk_list_entry(struct pr_chunk_x **list, } #endif - -#ifndef HAVE_ASPRINTF +#if !defined(HAVE_ASPRINTF) || !defined(HAVE_C99_VSNPRINTF) int rep_asprintf(char **ptr, const char *format, ...) { va_list ap; -- 1.7.9.5