diff options
author | Nick Mathewson <nickm@torproject.org> | 2010-02-28 21:46:46 -0500 |
---|---|---|
committer | Nick Mathewson <nickm@torproject.org> | 2010-02-28 21:46:46 -0500 |
commit | 897b0ebbace3a14cc02e8a281a87e7db156fbd53 (patch) | |
tree | 6e8209545361c7a1048769ddf3ddeee46a637bf1 | |
parent | 6fa8dacb97587707156507aa35141e414fc284bb (diff) | |
download | tor-897b0ebbace3a14cc02e8a281a87e7db156fbd53.tar tor-897b0ebbace3a14cc02e8a281a87e7db156fbd53.tar.gz |
better handle the case where *strp is in asprintf args
-rw-r--r-- | src/common/compat.c | 24 |
1 files changed, 16 insertions, 8 deletions
diff --git a/src/common/compat.c b/src/common/compat.c index 49e66c6a6..e256f45a2 100644 --- a/src/common/compat.c +++ b/src/common/compat.c @@ -345,11 +345,15 @@ tor_asprintf(char **strp, const char *fmt, ...) int tor_vasprintf(char **strp, const char *fmt, va_list args) { + /* use a temporary variable in case *strp is in args. */ + char *strp_tmp=NULL; #ifdef HAVE_VASPRINTF /* If the platform gives us one, use it. */ - int r = vasprintf(strp, fmt, args); + int r = vasprintf(&strp_tmp, fmt, args); if (r < 0) *strp = NULL; + else + *strp = strp_tmp; return r; #elif defined(MS_WINDOWS) /* On Windows, _vsnprintf won't tell us the length of the string if it @@ -358,15 +362,17 @@ tor_vasprintf(char **strp, const char *fmt, va_list args) char *res; len = _vcsprintf(fmt, args); if (len < 0) { - strp = NULL; + *strp = NULL; return -1; } - *strp = tor_malloc(len + 1); - r = _vsnprintf(*strp, len+1, fmt, args); + strp_tmp = tor_malloc(len + 1); + r = _vsnprintf(strp_tmp, len+1, fmt, args); if (r != len) { - tor_free(*strp); + tor_free(strp_tmp); + *strp = NULL; return -1; } + *strp = strp_tmp; return len; #else /* Everywhere else, we have a decent vsnprintf that tells us how many @@ -383,12 +389,14 @@ tor_vasprintf(char **strp, const char *fmt, va_list args) *strp = tor_strdup(buf); return len; } - *strp = tor_malloc(len+1); - r = vsnprintf(*strp, len+1, fmt, args); + strp_tmp = tor_malloc(len+1); + r = vsnprintf(strp_tmp, len+1, fmt, args); if (r != len) { - tor_free(*strp); + tor_free(strp_tmp); + *strp = NULL; return -1; } + *strp = strp_tmp; return len; #endif } |