diff options
author | jim <jim@13f79535-47bb-0310-9956-ffa450edef68> | 2002-08-26 17:34:07 +0000 |
---|---|---|
committer | jim <jim@13f79535-47bb-0310-9956-ffa450edef68> | 2002-08-26 17:34:07 +0000 |
commit | 06715cf67c18ecc1df26d011cce9c58639c6790c (patch) | |
tree | 74576dcf12a34ec1f039382a3d853648258226b3 /strings | |
parent | 5c6e47a47b58dcd56dc0a31de0ffb6a0c65ce433 (diff) | |
download | libapr-06715cf67c18ecc1df26d011cce9c58639c6790c.tar.gz |
Document the len == 0 special case for apr_snprintf() and allow it to
work no matter what buf is (NULL or not).
PR:
Obtained from:
Submitted by:
Reviewed by: rbb
git-svn-id: http://svn.apache.org/repos/asf/apr/apr/trunk@63832 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'strings')
-rw-r--r-- | strings/apr_snprintf.c | 30 |
1 files changed, 24 insertions, 6 deletions
diff --git a/strings/apr_snprintf.c b/strings/apr_snprintf.c index cb97e18b5..6cde18779 100644 --- a/strings/apr_snprintf.c +++ b/strings/apr_snprintf.c @@ -1249,9 +1249,21 @@ APR_DECLARE_NONSTD(int) apr_snprintf(char *buf, apr_size_t len, va_list ap; apr_vformatter_buff_t vbuff; - /* save one byte for nul terminator */ - vbuff.curpos = buf; - vbuff.endpos = buf + len - 1; + if (len == 0) { + /* NOTE: This is a special case; we just want to return the number + * of chars that would be written (minus \0) if the buffer + * size was infinite. We leverage the fact that INS_CHAR + * just does actual inserts iff the buffer pointer is non-NULL. + * In this case, we don't care what buf is; it can be NULL, since + * we don't touch it at all. + * / + vbuff.curpos = NULL; + vbuff.endpos = NULL; + } else { + /* save one byte for nul terminator */ + vbuff.curpos = buf; + vbuff.endpos = buf + len - 1; + } va_start(ap, format); cc = apr_vformatter(snprintf_flush, &vbuff, format, ap); va_end(ap); @@ -1268,9 +1280,15 @@ APR_DECLARE(int) apr_vsnprintf(char *buf, apr_size_t len, const char *format, int cc; apr_vformatter_buff_t vbuff; - /* save one byte for nul terminator */ - vbuff.curpos = buf; - vbuff.endpos = buf + len - 1; + if (len == 0) { + /* See above notee */ + vbuff.curpos = NULL; + vbuff.endpos = NULL; + } else { + /* save one byte for nul terminator */ + vbuff.curpos = buf; + vbuff.endpos = buf + len - 1; + } cc = apr_vformatter(snprintf_flush, &vbuff, format, ap); if (len != 0) { *vbuff.curpos = '\0'; |