From: VMware, Inc <> Date: Thu, 2 Aug 2012 06:57:12 +0000 (-0700) Subject: lib/misc: add some string concatenation helpes X-Git-Tag: 2012.10.14-874563~53 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=123ca1c6fd671557634abdb363b73f331f3d19e1;p=thirdparty%2Fopen-vm-tools.git lib/misc: add some string concatenation helpes Signed-off-by: Dmitry Torokhov --- diff --git a/open-vm-tools/lib/include/strutil.h b/open-vm-tools/lib/include/strutil.h index 7fed680a2..ebc65739d 100644 --- a/open-vm-tools/lib/include/strutil.h +++ b/open-vm-tools/lib/include/strutil.h @@ -62,4 +62,8 @@ Bool StrUtil_VDynBufPrintf(struct DynBuf *b, const char *fmt, va_list args); Bool StrUtil_DynBufPrintf(struct DynBuf *b, const char *fmt, ...); void StrUtil_SafeDynBufPrintf(struct DynBuf *b, const char *fmt, ...); +void StrUtil_SafeStrcat(char **prefix, const char *str); +void StrUtil_SafeStrcatFV(char **prefix, const char *fmt, va_list args); +void StrUtil_SafeStrcatF(char **prefix, const char *fmt, ...) PRINTF_DECL(2, 3); + #endif /* STRUTIL_H */ diff --git a/open-vm-tools/lib/misc/strutil.c b/open-vm-tools/lib/misc/strutil.c index 096b5fb7d..246223b67 100644 --- a/open-vm-tools/lib/misc/strutil.c +++ b/open-vm-tools/lib/misc/strutil.c @@ -1,5 +1,5 @@ /********************************************************* - * Copyright (C) 1998 VMware, Inc. All rights reserved. + * Copyright (C) 1998-2012 VMware, Inc. All rights reserved. * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published @@ -1059,3 +1059,97 @@ StrUtil_SafeDynBufPrintf(DynBuf *b, // IN/OUT ASSERT_MEM_ALLOC(success); } + + +/* + *----------------------------------------------------------------------------- + * + * StrUtil_SafeStrcat -- + * + * Given an input buffer, append another string and return the resulting + * string. The input buffer is freed along the way. A fancy strcat(). + * + * Results: + * New buffer returned via 'prefix' + * + * Side effects: + * None. + * + *----------------------------------------------------------------------------- + */ + +void +StrUtil_SafeStrcat(char **prefix, // IN/OUT + const char *str) // IN +{ + char *tmp; + size_t plen = *prefix != NULL ? strlen(*prefix) : 0; + size_t slen = strlen(str); + + /* Check for overflow */ + ASSERT_NOT_IMPLEMENTED((size_t)-1 - plen > slen + 1); + + tmp = realloc(*prefix, plen + slen + 1 /* NUL */); + ASSERT_MEM_ALLOC(tmp); + + memcpy(tmp + plen, str, slen + 1 /* NUL */); + *prefix = tmp; +} + + +/* + *----------------------------------------------------------------------------- + * + * StrUtil_SafeStrcatFV -- + * + * Given an input buffer, append another string and return the resulting + * string. The input buffer is freed along the way. A fancy vasprintf(). + * + * Results: + * New buffer returned via 'prefix' + * + * Side effects: + * None. + * + *----------------------------------------------------------------------------- + */ + +void +StrUtil_SafeStrcatFV(char **prefix, // IN/OUT + const char *fmt, // IN + va_list args) // IN +{ + char *str = Str_SafeVasprintf(NULL, fmt, args); + StrUtil_SafeStrcat(prefix, str); + free(str); +} + + +/* + *----------------------------------------------------------------------------- + * + * StrUtil_SafeStrcatF -- + * + * Given an input buffer, append another string and return the resulting + * string. The input buffer is freed along the way. A fancy asprintf(). + * + * Results: + * New buffer returned via 'prefix' + * + * Side effects: + * None. + * + *----------------------------------------------------------------------------- + */ + +void +StrUtil_SafeStrcatF(char **prefix, // IN/OUT + const char *fmt, // IN + ...) // IN +{ + va_list args; + + va_start(args, fmt); + StrUtil_SafeStrcatFV(prefix, fmt, args); + va_end(args); +}