You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@apr.apache.org by ji...@apache.org on 2002/08/26 19:34:07 UTC

cvs commit: apr/strings apr_snprintf.c

jim         2002/08/26 10:34:07

  Modified:    strings  apr_snprintf.c
  Log:
  Document the len == 0 special case for apr_snprintf() and allow it to
  work no matter what buf is (NULL or not).
  Reviewed by:	rbb
  
  Revision  Changes    Path
  1.28      +24 -6     apr/strings/apr_snprintf.c
  
  Index: apr_snprintf.c
  ===================================================================
  RCS file: /home/cvs/apr/strings/apr_snprintf.c,v
  retrieving revision 1.27
  retrieving revision 1.28
  diff -u -r1.27 -r1.28
  --- apr_snprintf.c	25 Aug 2002 04:22:35 -0000	1.27
  +++ apr_snprintf.c	26 Aug 2002 17:34:07 -0000	1.28
  @@ -1249,9 +1249,21 @@
       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 @@
       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';