LCOV - code coverage report
Current view: top level - lib - gstr.c (source / functions) Hit Total Coverage
Test: GNU Libtasn1 Lines: 12 20 60.0 %
Date: 2013-03-24 Functions: 2 2 100.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 2 8 25.0 %

           Branch data     Line data    Source code
       1                 :            : /*
       2                 :            :  * Copyright (C) 2002-2013 Free Software Foundation, Inc.
       3                 :            :  *
       4                 :            :  * This file is part of LIBTASN1.
       5                 :            :  *
       6                 :            :  * The LIBTASN1 library is free software; you can redistribute it
       7                 :            :  * and/or modify it under the terms of the GNU Lesser General Public
       8                 :            :  * License as published by the Free Software Foundation; either
       9                 :            :  * version 2.1 of the License, or (at your option) any later version.
      10                 :            :  *
      11                 :            :  * This library is distributed in the hope that it will be useful, but
      12                 :            :  * WITHOUT ANY WARRANTY; without even the implied warranty of
      13                 :            :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      14                 :            :  * Lesser General Public License for more details.
      15                 :            :  *
      16                 :            :  * You should have received a copy of the GNU Lesser General Public
      17                 :            :  * License along with this library; if not, write to the Free Software
      18                 :            :  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
      19                 :            :  * 02110-1301, USA
      20                 :            :  */
      21                 :            : 
      22                 :            : #include <int.h>
      23                 :            : #include "gstr.h"
      24                 :            : 
      25                 :            : /* These function are like strcat, strcpy. They only
      26                 :            :  * do bounds checking (they shouldn't cause buffer overruns),
      27                 :            :  * and they always produce null terminated strings.
      28                 :            :  *
      29                 :            :  * They should be used only with null terminated strings.
      30                 :            :  */
      31                 :            : void
      32                 :       1667 : _asn1_str_cat (char *dest, size_t dest_tot_size, const char *src)
      33                 :            : {
      34                 :       1667 :   size_t str_size = strlen (src);
      35                 :       1667 :   size_t dest_size = strlen (dest);
      36                 :            : 
      37         [ +  - ]:       1667 :   if (dest_tot_size - dest_size > str_size)
      38                 :            :     {
      39                 :       1667 :       strcat (dest, src);
      40                 :            :     }
      41                 :            :   else
      42                 :            :     {
      43         [ #  # ]:          0 :       if (dest_tot_size - dest_size > 0)
      44                 :            :         {
      45                 :          0 :           strncat (dest, src, (dest_tot_size - dest_size) - 1);
      46                 :          0 :           dest[dest_tot_size - 1] = 0;
      47                 :            :         }
      48                 :            :     }
      49                 :       1667 : }
      50                 :            : 
      51                 :            : /* Returns the bytes copied (not including the null terminator) */
      52                 :            : unsigned int
      53                 :       5232 : _asn1_str_cpy (char *dest, size_t dest_tot_size, const char *src)
      54                 :            : {
      55                 :       5232 :   size_t str_size = strlen (src);
      56                 :            : 
      57         [ +  - ]:       5232 :   if (dest_tot_size > str_size)
      58                 :            :     {
      59                 :       5232 :       strcpy (dest, src);
      60                 :       5232 :       return str_size;
      61                 :            :     }
      62                 :            :   else
      63                 :            :     {
      64         [ #  # ]:          0 :       if (dest_tot_size > 0)
      65                 :            :         {
      66                 :          0 :           str_size = dest_tot_size - 1;
      67                 :          0 :           memcpy (dest, src, str_size);
      68                 :          0 :           dest[str_size] = 0;
      69                 :          0 :           return str_size;
      70                 :            :         }
      71                 :            :       else
      72                 :       5232 :         return 0;
      73                 :            :     }
      74                 :            : }

Generated by: LCOV version 1.9