LCOV - code coverage report
Current view: top level - lib - kasprintf.c (source / functions) Hit Total Coverage
Test: gcov_data_raspi2_real_modules_combined.info Lines: 20 20 100.0 %
Date: 2020-09-30 20:25:40 Functions: 3 3 100.0 %
Branches: 7 8 87.5 %

           Branch data     Line data    Source code
       1                 :            : // SPDX-License-Identifier: GPL-2.0
       2                 :            : /*
       3                 :            :  *  linux/lib/kasprintf.c
       4                 :            :  *
       5                 :            :  *  Copyright (C) 1991, 1992  Linus Torvalds
       6                 :            :  */
       7                 :            : 
       8                 :            : #include <stdarg.h>
       9                 :            : #include <linux/export.h>
      10                 :            : #include <linux/slab.h>
      11                 :            : #include <linux/types.h>
      12                 :            : #include <linux/string.h>
      13                 :            : 
      14                 :            : /* Simplified asprintf. */
      15                 :     517473 : char *kvasprintf(gfp_t gfp, const char *fmt, va_list ap)
      16                 :            : {
      17                 :            :         unsigned int first, second;
      18                 :            :         char *p;
      19                 :            :         va_list aq;
      20                 :            : 
      21                 :     517473 :         va_copy(aq, ap);
      22                 :     517473 :         first = vsnprintf(NULL, 0, fmt, aq);
      23                 :     517473 :         va_end(aq);
      24                 :            : 
      25                 :     517473 :         p = kmalloc_track_caller(first+1, gfp);
      26         [ +  + ]:     517480 :         if (!p)
      27                 :            :                 return NULL;
      28                 :            : 
      29                 :     517457 :         second = vsnprintf(p, first+1, fmt, ap);
      30         [ -  + ]:     517457 :         WARN(first != second, "different return values (%u and %u) from vsnprintf(\"%s\", ...)",
      31                 :            :              first, second, fmt);
      32                 :            : 
      33                 :     517483 :         return p;
      34                 :            : }
      35                 :            : EXPORT_SYMBOL(kvasprintf);
      36                 :            : 
      37                 :            : /*
      38                 :            :  * If fmt contains no % (or is exactly %s), use kstrdup_const. If fmt
      39                 :            :  * (or the sole vararg) points to rodata, we will then save a memory
      40                 :            :  * allocation and string copy. In any case, the return value should be
      41                 :            :  * freed using kfree_const().
      42                 :            :  */
      43                 :     288765 : const char *kvasprintf_const(gfp_t gfp, const char *fmt, va_list ap)
      44                 :            : {
      45         [ +  + ]:     288765 :         if (!strchr(fmt, '%'))
      46                 :      11385 :                 return kstrdup_const(fmt, gfp);
      47         [ +  + ]:     277380 :         if (!strcmp(fmt, "%s"))
      48                 :     203895 :                 return kstrdup_const(va_arg(ap, const char*), gfp);
      49                 :      73485 :         return kvasprintf(gfp, fmt, ap);
      50                 :            : }
      51                 :            : EXPORT_SYMBOL(kvasprintf_const);
      52                 :            : 
      53                 :     443976 : char *kasprintf(gfp_t gfp, const char *fmt, ...)
      54                 :            : {
      55                 :            :         va_list ap;
      56                 :            :         char *p;
      57                 :            : 
      58                 :     443976 :         va_start(ap, fmt);
      59                 :     443976 :         p = kvasprintf(gfp, fmt, ap);
      60                 :     443997 :         va_end(ap);
      61                 :            : 
      62                 :     443997 :         return p;
      63                 :            : }
      64                 :            : EXPORT_SYMBOL(kasprintf);

Generated by: LCOV version 1.14