LCOV - code coverage report
Current view: top level - core/support - string_posix.c (source / functions) Hit Total Coverage
Test: tmp.CaZ6RjdVn2 Lines: 15 21 71.4 %
Date: 2015-12-10 22:15:08 Functions: 1 1 100.0 %

          Line data    Source code
       1             : /*
       2             :  *
       3             :  * Copyright 2015, Google Inc.
       4             :  * All rights reserved.
       5             :  *
       6             :  * Redistribution and use in source and binary forms, with or without
       7             :  * modification, are permitted provided that the following conditions are
       8             :  * met:
       9             :  *
      10             :  *     * Redistributions of source code must retain the above copyright
      11             :  * notice, this list of conditions and the following disclaimer.
      12             :  *     * Redistributions in binary form must reproduce the above
      13             :  * copyright notice, this list of conditions and the following disclaimer
      14             :  * in the documentation and/or other materials provided with the
      15             :  * distribution.
      16             :  *     * Neither the name of Google Inc. nor the names of its
      17             :  * contributors may be used to endorse or promote products derived from
      18             :  * this software without specific prior written permission.
      19             :  *
      20             :  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
      21             :  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
      22             :  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
      23             :  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
      24             :  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
      25             :  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
      26             :  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
      27             :  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
      28             :  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
      29             :  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
      30             :  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
      31             :  *
      32             :  */
      33             : 
      34             : #include <grpc/support/port_platform.h>
      35             : 
      36             : #ifdef GPR_POSIX_STRING
      37             : 
      38             : #include <stdio.h>
      39             : #include <stdarg.h>
      40             : #include <string.h>
      41             : 
      42             : #include <grpc/support/alloc.h>
      43             : 
      44     6301893 : int gpr_asprintf(char **strp, const char *format, ...) {
      45             :   va_list args;
      46             :   int ret;
      47             :   char buf[64];
      48             :   size_t strp_buflen;
      49             : 
      50             :   /* Use a constant-sized buffer to determine the length. */
      51     6301893 :   va_start(args, format);
      52     6301893 :   ret = vsnprintf(buf, sizeof(buf), format, args);
      53     6301893 :   va_end(args);
      54     6301893 :   if (ret < 0) {
      55           0 :     *strp = NULL;
      56           0 :     return -1;
      57             :   }
      58             : 
      59             :   /* Allocate a new buffer, with space for the NUL terminator. */
      60     6301893 :   strp_buflen = (size_t)ret + 1;
      61     6301893 :   if ((*strp = gpr_malloc(strp_buflen)) == NULL) {
      62             :     /* This shouldn't happen, because gpr_malloc() calls abort(). */
      63           0 :     return -1;
      64             :   }
      65             : 
      66             :   /* Return early if we have all the bytes. */
      67     6301892 :   if (strp_buflen <= sizeof(buf)) {
      68     6301410 :     memcpy(*strp, buf, strp_buflen);
      69     6301410 :     return ret;
      70             :   }
      71             : 
      72             :   /* Try again using the larger buffer. */
      73         482 :   va_start(args, format);
      74         482 :   ret = vsnprintf(*strp, strp_buflen, format, args);
      75         482 :   va_end(args);
      76         482 :   if ((size_t)ret == strp_buflen - 1) {
      77         383 :     return ret;
      78             :   }
      79             : 
      80             :   /* This should never happen. */
      81           0 :   gpr_free(*strp);
      82           0 :   *strp = NULL;
      83           0 :   return -1;
      84             : }
      85             : 
      86             : #endif /* GPR_POSIX_STRING */

Generated by: LCOV version 1.11