LCOV - code coverage report
Current view: top level - libbpfilter - helper.c (source / functions) Coverage Total Hit
Test: coverage.lcov Lines: 90.2 % 61 55
Test Date: 2025-11-24 12:34:34 Functions: 100.0 % 8 8
Branches: 58.0 % 50 29

             Branch data     Line data    Source code
       1                 :             : /* SPDX-License-Identifier: GPL-2.0-only */
       2                 :             : /*
       3                 :             :  * Copyright (c) 2023 Meta Platforms, Inc. and affiliates.
       4                 :             :  */
       5                 :             : 
       6                 :             : #include "bpfilter/helper.h"
       7                 :             : 
       8                 :             : #include <ctype.h>
       9                 :             : #include <errno.h>
      10                 :             : #include <fcntl.h>
      11                 :             : #include <stdio.h>
      12                 :             : #include <stdlib.h>
      13                 :             : #include <string.h>
      14                 :             : #include <sys/types.h>
      15                 :             : #include <unistd.h>
      16                 :             : 
      17                 :             : #include "bpfilter/logger.h"
      18                 :             : 
      19                 :             : #define OPEN_MODE_644 (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)
      20                 :             : 
      21                 :        4138 : void closep(int *fd)
      22                 :             : {
      23         [ +  + ]:        4138 :     if (*fd == -1)
      24                 :             :         return;
      25                 :             : 
      26         [ +  + ]:        1747 :     if (close(*fd))
      27         [ +  - ]:          20 :         bf_warn_r(errno, "failed to close fd %d, assuming file is closed", *fd);
      28                 :             : 
      29                 :        1747 :     *fd = -1;
      30                 :             : }
      31                 :             : 
      32                 :         985 : int bf_strncpy(char *dst, size_t len, const char *src)
      33                 :             : {
      34                 :             :     size_t src_len;
      35                 :             :     size_t copy_len;
      36                 :             : 
      37                 :             :     bf_assert(dst && src);
      38                 :             :     bf_assert(len);
      39                 :             : 
      40                 :         985 :     src_len = strlen(src);
      41                 :         985 :     copy_len = bf_min(src_len, len - 1);
      42                 :             : 
      43                 :         985 :     memcpy(dst, src, copy_len);
      44                 :         985 :     dst[copy_len] = '\0';
      45                 :             : 
      46         [ +  + ]:         985 :     return copy_len != src_len ? -E2BIG : 0;
      47                 :             : }
      48                 :             : 
      49                 :        1278 : int bf_realloc(void **ptr, size_t size)
      50                 :             : {
      51                 :             :     _cleanup_free_ void *_ptr;
      52                 :             : 
      53                 :             :     bf_assert(ptr);
      54                 :             : 
      55                 :        1278 :     _ptr = realloc(*ptr, size);
      56         [ +  - ]:        1278 :     if (!_ptr)
      57                 :             :         return -ENOMEM;
      58                 :             : 
      59                 :        1278 :     *ptr = TAKE_PTR(_ptr);
      60                 :             : 
      61                 :        1278 :     return 0;
      62                 :             : }
      63                 :             : 
      64                 :           8 : int bf_read_file(const char *path, void **buf, size_t *len)
      65                 :             : {
      66                 :           8 :     _cleanup_close_ int fd = -1;
      67                 :             :     _cleanup_free_ void *_buf = NULL;
      68                 :             :     size_t _len;
      69                 :             :     ssize_t r;
      70                 :             : 
      71                 :             :     bf_assert(path);
      72                 :             :     bf_assert(buf);
      73                 :             :     bf_assert(len);
      74                 :             : 
      75                 :           8 :     fd = open(path, O_RDONLY);
      76         [ +  + ]:           8 :     if (fd < 0)
      77         [ +  - ]:           1 :         return bf_err_r(errno, "failed to open %s", path);
      78                 :             : 
      79                 :           7 :     _len = lseek(fd, 0, SEEK_END);
      80                 :           7 :     lseek(fd, 0, SEEK_SET);
      81                 :             : 
      82                 :           7 :     _buf = malloc(_len);
      83         [ -  + ]:           7 :     if (!_buf)
      84         [ #  # ]:           0 :         return bf_err_r(errno, "failed to allocate memory");
      85                 :             : 
      86                 :           7 :     r = read(fd, _buf, _len);
      87         [ -  + ]:           7 :     if (r < 0)
      88         [ #  # ]:           0 :         return bf_err_r(errno, "failed to read serialized data");
      89         [ -  + ]:           7 :     if ((size_t)r != _len)
      90         [ #  # ]:           0 :         return bf_err_r(EIO, "can't read full serialized data");
      91                 :             : 
      92                 :           7 :     closep(&fd);
      93                 :             : 
      94                 :           7 :     *buf = TAKE_PTR(_buf);
      95                 :           7 :     *len = _len;
      96                 :             : 
      97                 :           7 :     return 0;
      98                 :             : }
      99                 :             : 
     100                 :          83 : int bf_write_file(const char *path, const void *buf, size_t len)
     101                 :             : {
     102                 :          83 :     _cleanup_close_ int fd = -1;
     103                 :             :     ssize_t r;
     104                 :             : 
     105                 :             :     bf_assert(path);
     106                 :             :     bf_assert(buf);
     107                 :             : 
     108                 :          83 :     fd = open(path, O_TRUNC | O_CREAT | O_WRONLY, OPEN_MODE_644);
     109         [ -  + ]:          83 :     if (fd < 0)
     110         [ #  # ]:           0 :         return bf_err_r(errno, "failed to open %s", path);
     111                 :             : 
     112                 :          83 :     r = write(fd, buf, len);
     113         [ -  + ]:          83 :     if (r < 0)
     114         [ #  # ]:           0 :         return bf_err_r(errno, "failed to write to %s", path);
     115         [ -  + ]:          83 :     if ((size_t)r != len)
     116         [ #  # ]:           0 :         return bf_err_r(EIO, "can't write full data to %s", path);
     117                 :             : 
     118                 :          83 :     closep(&fd);
     119                 :             : 
     120                 :          83 :     return 0;
     121                 :             : }
     122                 :             : 
     123                 :         838 : char *bf_ltrim(char *str)
     124                 :             : {
     125                 :             :     bf_assert(str);
     126                 :             : 
     127         [ +  + ]:        2431 :     while (isspace(*str))
     128                 :        1593 :         str++;
     129                 :         838 :     return str;
     130                 :             : }
     131                 :             : 
     132                 :         839 : char *bf_rtrim(char *str)
     133                 :             : {
     134                 :             :     bf_assert(str);
     135                 :             : 
     136                 :         839 :     char *back = str + strlen(str);
     137                 :             : 
     138         [ +  + ]:         839 :     if (back == str)
     139                 :             :         return str;
     140                 :             : 
     141                 :             :     do {
     142                 :         858 :         --back;
     143   [ +  +  +  + ]:         858 :     } while (back > str && isspace(*back));
     144                 :             : 
     145   [ +  +  +  + ]:         794 :     if (back == str && isspace(*back))
     146                 :           1 :         *back = '\0';
     147                 :             :     else
     148                 :         793 :         *(back + 1) = '\0';
     149                 :             : 
     150                 :             :     return str;
     151                 :             : }
     152                 :             : 
     153                 :         830 : char *bf_trim(char *str)
     154                 :             : {
     155                 :             :     bf_assert(str);
     156                 :             : 
     157                 :         830 :     return bf_rtrim(bf_ltrim(str));
     158                 :             : }
        

Generated by: LCOV version 2.0-1