LCOV - code coverage report
Current view: top level - lib/dpkg - mlib.c (source / functions) Hit Total Coverage
Test: dpkg 1.21.11 C code coverage Lines: 31 54 57.4 %
Date: 2022-12-03 00:40:01 Functions: 11 13 84.6 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 7 20 35.0 %

           Branch data     Line data    Source code
       1                 :            : /*
       2                 :            :  * libdpkg - Debian packaging suite library routines
       3                 :            :  * mlib.c - ‘must’ library: routines will succeed or longjmp
       4                 :            :  *
       5                 :            :  * Copyright © 1994,1995 Ian Jackson <ijackson@chiark.greenend.org.uk>
       6                 :            :  * Copyright © 2006-2013, 2015 Guillem Jover <guillem@debian.org>
       7                 :            :  *
       8                 :            :  * This is free software; you can redistribute it and/or modify
       9                 :            :  * it under the terms of the GNU General Public License as published by
      10                 :            :  * the Free Software Foundation; either version 2 of the License, or
      11                 :            :  * (at your option) any later version.
      12                 :            :  *
      13                 :            :  * This is distributed in the hope that it will be useful,
      14                 :            :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      15                 :            :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      16                 :            :  * GNU General Public License for more details.
      17                 :            :  *
      18                 :            :  * You should have received a copy of the GNU General Public License
      19                 :            :  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
      20                 :            :  */
      21                 :            : 
      22                 :            : #include <config.h>
      23                 :            : #include <compat.h>
      24                 :            : 
      25                 :            : #include <sys/types.h>
      26                 :            : 
      27                 :            : #include <errno.h>
      28                 :            : #include <string.h>
      29                 :            : #include <fcntl.h>
      30                 :            : #include <unistd.h>
      31                 :            : #include <stdlib.h>
      32                 :            : #include <stdio.h>
      33                 :            : 
      34                 :            : #include <dpkg/i18n.h>
      35                 :            : #include <dpkg/dpkg.h>
      36                 :            : 
      37                 :            : static inline void *
      38                 :       3093 : must_alloc(void *ptr)
      39                 :            : {
      40         [ +  - ]:       3093 :   if (ptr)
      41                 :       3093 :     return ptr;
      42                 :            : 
      43                 :          0 :   onerr_abort++;
      44                 :          0 :   ohshite(_("failed to allocate memory"));
      45                 :            : }
      46                 :            : 
      47                 :        923 : void *m_malloc(size_t amount) {
      48                 :        923 :   return must_alloc(malloc(amount));
      49                 :            : }
      50                 :            : 
      51                 :            : void *
      52                 :        158 : m_calloc(size_t nmemb, size_t size)
      53                 :            : {
      54                 :        158 :   return must_alloc(calloc(nmemb, size));
      55                 :            : }
      56                 :            : 
      57                 :        739 : void *m_realloc(void *r, size_t amount) {
      58                 :        739 :   return must_alloc(realloc(r, amount));
      59                 :            : }
      60                 :            : 
      61                 :            : char *
      62                 :       1021 : m_strdup(const char *str)
      63                 :            : {
      64                 :       1021 :   return must_alloc(strdup(str));
      65                 :            : }
      66                 :            : 
      67                 :            : char *
      68                 :        252 : m_strndup(const char *str, size_t n)
      69                 :            : {
      70                 :        252 :   return must_alloc(strndup(str, n));
      71                 :            : }
      72                 :            : 
      73                 :            : int
      74                 :       1642 : m_vasprintf(char **strp, const char *fmt, va_list args)
      75                 :            : {
      76                 :            :   int n;
      77                 :            : 
      78                 :       1642 :   n = vasprintf(strp, fmt, args);
      79         [ +  - ]:       1642 :   if (n >= 0)
      80                 :       1642 :     return n;
      81                 :            : 
      82                 :          0 :   onerr_abort++;
      83                 :          0 :   ohshite(_("failed to allocate memory"));
      84                 :            : }
      85                 :            : 
      86                 :            : int
      87                 :          0 : m_asprintf(char **strp, const char *fmt, ...)
      88                 :            : {
      89                 :            :   va_list args;
      90                 :            :   int n;
      91                 :            : 
      92                 :          0 :   va_start(args, fmt);
      93                 :          0 :   n = m_vasprintf(strp, fmt, args);
      94                 :          0 :   va_end(args);
      95                 :            : 
      96                 :          0 :   return n;
      97                 :            : }
      98                 :            : 
      99                 :            : int
     100                 :          0 : m_dup(int oldfd)
     101                 :            : {
     102                 :            :   int newfd;
     103                 :            : 
     104                 :          0 :   newfd = dup(oldfd);
     105         [ #  # ]:          0 :   if (newfd >= 0)
     106                 :          0 :     return newfd;
     107                 :            : 
     108                 :          0 :   onerr_abort++;
     109                 :          0 :   ohshite(_("failed to dup for fd %d"), oldfd);
     110                 :            : }
     111                 :            : 
     112                 :         66 : void m_dup2(int oldfd, int newfd) {
     113                 :         66 :   const char *const stdstrings[]= { "in", "out", "err" };
     114                 :            : 
     115         [ +  - ]:         66 :   if (dup2(oldfd,newfd) == newfd) return;
     116                 :            : 
     117                 :          0 :   onerr_abort++;
     118         [ #  # ]:          0 :   if (newfd < 3) ohshite(_("failed to dup for std%s"),stdstrings[newfd]);
     119                 :          0 :   ohshite(_("failed to dup for fd %d"),newfd);
     120                 :            : }
     121                 :            : 
     122                 :            : void
     123                 :         92 : m_pipe(int fds[2])
     124                 :            : {
     125         [ +  - ]:         92 :   if (!pipe(fds)) return;
     126                 :          0 :   onerr_abort++;
     127                 :          0 :   ohshite(_("failed to create pipe"));
     128                 :            : }
     129                 :            : 
     130                 :            : void
     131                 :        281 : m_output(FILE *f, const char *name)
     132                 :            : {
     133                 :        281 :   fflush(f);
     134   [ -  +  -  - ]:        281 :   if (ferror(f) && errno != EPIPE)
     135                 :          0 :     ohshite(_("error writing to '%s'"), name);
     136                 :        281 : }
     137                 :            : 
     138                 :            : void
     139                 :         93 : setcloexec(int fd, const char *fn)
     140                 :            : {
     141                 :            :   int f;
     142                 :            : 
     143                 :         93 :   f = fcntl(fd, F_GETFD);
     144         [ -  + ]:         93 :   if (f == -1)
     145                 :          0 :     ohshite(_("unable to read filedescriptor flags for %.250s"),fn);
     146         [ -  + ]:         93 :   if (fcntl(fd, F_SETFD, (f|FD_CLOEXEC))==-1)
     147                 :          0 :     ohshite(_("unable to set close-on-exec flag for %.250s"),fn);
     148                 :         93 : }

Generated by: LCOV version 1.16