LCOV - code coverage report
Current view: top level - strace - xmalloc.c (source / functions) Hit Total Coverage
Test: strace-4.18.0.129.97bbb Code Coverage Lines: 24 36 66.7 %
Date: 2017-07-31 03:46:08 Functions: 5 6 83.3 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 7 20 35.0 %

           Branch data     Line data    Source code
       1                 :            : /*
       2                 :            :  * Copyright (c) 2015 Dmitry V. Levin <ldv@altlinux.org>
       3                 :            :  * All rights reserved.
       4                 :            :  *
       5                 :            :  * Redistribution and use in source and binary forms, with or without
       6                 :            :  * modification, are permitted provided that the following conditions
       7                 :            :  * are met:
       8                 :            :  * 1. Redistributions of source code must retain the above copyright
       9                 :            :  *    notice, this list of conditions and the following disclaimer.
      10                 :            :  * 2. Redistributions in binary form must reproduce the above copyright
      11                 :            :  *    notice, this list of conditions and the following disclaimer in the
      12                 :            :  *    documentation and/or other materials provided with the distribution.
      13                 :            :  * 3. The name of the author may not be used to endorse or promote products
      14                 :            :  *    derived from this software without specific prior written permission.
      15                 :            :  *
      16                 :            :  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
      17                 :            :  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
      18                 :            :  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
      19                 :            :  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
      20                 :            :  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
      21                 :            :  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
      22                 :            :  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
      23                 :            :  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
      24                 :            :  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
      25                 :            :  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
      26                 :            :  */
      27                 :            : 
      28                 :            : #include <stdbool.h>
      29                 :            : #include <stdlib.h>
      30                 :            : #include <string.h>
      31                 :            : #include <stdio.h>
      32                 :            : 
      33                 :            : #include "error_prints.h"
      34                 :            : #include "xmalloc.h"
      35                 :            : 
      36                 :            : static void
      37                 :          0 : die_out_of_memory(void)
      38                 :            : {
      39                 :            :         static bool recursed;
      40                 :            : 
      41         [ #  # ]:          0 :         if (recursed)
      42                 :          0 :                 exit(1);
      43                 :          0 :         recursed = 1;
      44                 :            : 
      45                 :          0 :         error_msg_and_die("Out of memory");
      46                 :            : }
      47                 :            : 
      48                 :            : void *
      49                 :      13442 : xmalloc(size_t size)
      50                 :            : {
      51                 :      13456 :         void *p = malloc(size);
      52                 :            : 
      53 [ -  + ][ -  + ]:      13456 :         if (!p)
      54                 :          0 :                 die_out_of_memory();
      55                 :            : 
      56                 :      13442 :         return p;
      57                 :            : }
      58                 :            : 
      59                 :            : void *
      60                 :      20656 : xcalloc(size_t nmemb, size_t size)
      61                 :            : {
      62                 :      20656 :         void *p = calloc(nmemb, size);
      63                 :            : 
      64         [ -  + ]:      20656 :         if (!p)
      65                 :          0 :                 die_out_of_memory();
      66                 :            : 
      67                 :      20656 :         return p;
      68                 :            : }
      69                 :            : 
      70                 :            : #define HALF_SIZE_T     (((size_t) 1) << (sizeof(size_t) * 4))
      71                 :            : 
      72                 :            : void *
      73                 :     125080 : xreallocarray(void *ptr, size_t nmemb, size_t size)
      74                 :            : {
      75                 :     125080 :         size_t bytes = nmemb * size;
      76                 :            : 
      77 [ -  + ][ #  # ]:     125080 :         if ((nmemb | size) >= HALF_SIZE_T &&
      78         [ #  # ]:          0 :             size && bytes / size != nmemb)
      79                 :          0 :                 die_out_of_memory();
      80                 :            : 
      81                 :     125080 :         void *p = realloc(ptr, bytes);
      82                 :            : 
      83         [ -  + ]:     125080 :         if (!p)
      84                 :          0 :                 die_out_of_memory();
      85                 :            : 
      86                 :     125080 :         return p;
      87                 :            : }
      88                 :            : 
      89                 :            : char *
      90                 :      12929 : xstrdup(const char *str)
      91                 :            : {
      92                 :      12929 :         char *p = strdup(str);
      93                 :            : 
      94         [ -  + ]:      12929 :         if (!p)
      95                 :          0 :                 die_out_of_memory();
      96                 :            : 
      97                 :      12929 :         return p;
      98                 :            : }
      99                 :            : 
     100                 :            : char *
     101                 :         14 : xstrndup(const char *str, size_t n)
     102                 :            : {
     103                 :            :         char *p;
     104                 :            : 
     105                 :            : #ifdef HAVE_STRNDUP
     106                 :            :         p = strndup(str, n);
     107                 :            : #else
     108                 :         28 :         p = xmalloc(n + 1);
     109                 :            : #endif
     110                 :            : 
     111         [ -  + ]:         14 :         if (!p)
     112                 :          0 :                 die_out_of_memory();
     113                 :            : 
     114                 :            : #ifndef HAVE_STRNDUP
     115                 :         14 :         strncpy(p, str, n);
     116                 :         14 :         p[n] = '\0';
     117                 :            : #endif
     118                 :            : 
     119                 :         14 :         return p;
     120                 :            : }

Generated by: LCOV version 1.11