From 89a0e3a927dff09070ef9454bc9215efaba91036 Mon Sep 17 00:00:00 2001 From: Wayne Davison Date: Thu, 9 Nov 2006 02:34:17 +0000 Subject: [PATCH] Get rid of the last strcpy() call by using an static inline function. --- popt/system.h | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/popt/system.h b/popt/system.h index d2bbb9d8..0579b8ca 100644 --- a/popt/system.h +++ b/popt/system.h @@ -95,17 +95,6 @@ char * xstrdup (const char *str) /*@*/; /*@=redecl =redef@*/ -#if HAVE_MCHECK_H && defined(__GNUC__) -#define vmefail() (fprintf(stderr, "virtual memory exhausted.\n"), exit(EXIT_FAILURE), NULL) -#define xstrdup(_str) (strcpy((malloc(strlen(_str)+1) ? : vmefail()), (_str))) -#else -#define xstrdup(_str) strdup(_str) -#endif /* HAVE_MCHECK_H && defined(__GNUC__) */ - -#if HAVE___SECURE_GETENV && !defined(__LCLINT__) -#define getenv(_s) __secure_getenv(_s) -#endif - #ifndef HAVE_STRLCPY size_t strlcpy(char *d, const char *s, size_t bufsize); #endif @@ -114,6 +103,25 @@ size_t strlcpy(char *d, const char *s, size_t bufsize); size_t strlcat(char *d, const char *s, size_t bufsize); #endif +#if HAVE_MCHECK_H && defined(__GNUC__) +#define vmefail() (fprintf(stderr, "virtual memory exhausted.\n"), exit(EXIT_FAILURE), NULL) +static inline char * +xstrdup(const char *s) +{ + size_t memsize = strlen(s) + 1; + char *ptr = malloc(memsize); + if (!ptr) vmefail(); + strlcpy(ptr, s, memsize); + return ptr; +} +#else +#define xstrdup(_str) strdup(_str) +#endif /* HAVE_MCHECK_H && defined(__GNUC__) */ + +#if HAVE___SECURE_GETENV && !defined(__LCLINT__) +#define getenv(_s) __secure_getenv(_s) +#endif + #if !defined HAVE_SNPRINTF || !defined HAVE_C99_VSNPRINTF #define snprintf rsync_snprintf int snprintf(char *str,size_t count,const char *fmt,...);