diff options
author | Nick Mathewson <nickm@torproject.org> | 2019-05-29 09:43:20 -0400 |
---|---|---|
committer | Nick Mathewson <nickm@torproject.org> | 2019-05-29 09:43:20 -0400 |
commit | 8f0b29961eb4b9965f22bbcabdc0aa0354c19a4a (patch) | |
tree | 691d02de3990f09cee704be89373cdf26c6641d6 /src/lib/string | |
parent | e5deb2bbc73d8830ae6c479a4532e72112f5484a (diff) | |
parent | 0e0cf4abd80249faa23f2bbdb89e62ba96c898f0 (diff) | |
download | tor-8f0b29961eb4b9965f22bbcabdc0aa0354c19a4a.tar.gz tor-8f0b29961eb4b9965f22bbcabdc0aa0354c19a4a.zip |
Merge branch 'ticket30561_029' into ticket30561_035
Diffstat (limited to 'src/lib/string')
-rw-r--r-- | src/lib/string/printf.c | 16 |
1 files changed, 13 insertions, 3 deletions
diff --git a/src/lib/string/printf.c b/src/lib/string/printf.c index 415d4ac4a7..a5cb71ce09 100644 --- a/src/lib/string/printf.c +++ b/src/lib/string/printf.c @@ -131,14 +131,24 @@ tor_vasprintf(char **strp, const char *fmt, va_list args) * characters we need. We give it a try on a short buffer first, since * it might be nice to avoid the second vsnprintf call. */ + /* XXXX This code spent a number of years broken (see bug 30651). It is + * possible that no Tor users actually run on systems without vasprintf() or + * _vscprintf(). If so, we should consider removing this code. */ char buf[128]; int len, r; va_list tmp_args; va_copy(tmp_args, args); - /* vsnprintf() was properly checked but tor_vsnprintf() available so - * why not use it? */ - len = tor_vsnprintf(buf, sizeof(buf), fmt, tmp_args); + /* Use vsnprintf to retrieve needed length. tor_vsnprintf() is not an + * option here because it will simply return -1 if buf is not large enough + * to hold the complete string. + */ + len = vsnprintf(buf, sizeof(buf), fmt, tmp_args); va_end(tmp_args); + buf[sizeof(buf) - 1] = '\0'; + if (len < 0) { + *strp = NULL; + return -1; + } if (len < (int)sizeof(buf)) { *strp = tor_strdup(buf); return len; |