Coverage Report

Created: 2025-10-10 07:17

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libtasn1/lib/gstr.c
Line
Count
Source
1
/*
2
 * Copyright (C) 2002-2025 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, see
18
 * <https://www.gnu.org/licenses/>.
19
 */
20
21
#include <config.h>
22
23
#include "int.h"
24
#include "gstr.h"
25
26
/* These function are like strcat, strcpy. They only
27
 * do bounds checking (they shouldn't cause buffer overruns),
28
 * and they always produce null terminated strings.
29
 *
30
 * They should be used only with null terminated strings.
31
 */
32
void
33
_asn1_str_cat (char *dest, size_t dest_tot_size, const char *src)
34
9.90M
{
35
9.90M
  size_t str_size = strlen (src);
36
9.90M
  size_t dest_size = strlen (dest);
37
38
9.90M
  if (dest_tot_size - dest_size > str_size)
39
98.3k
    {
40
98.3k
      strcat (dest, src);
41
98.3k
    }
42
9.80M
  else
43
9.80M
    {
44
9.80M
      if (dest_tot_size > dest_size)
45
9.80M
  {
46
9.80M
    strncat (dest, src, (dest_tot_size - dest_size) - 1);
47
9.80M
    dest[dest_tot_size - 1] = 0;
48
9.80M
  }
49
9.80M
    }
50
9.90M
}
51
52
/* Returns the bytes copied (not including the null terminator) */
53
unsigned int
54
_asn1_str_cpy (char *dest, size_t dest_tot_size, const char *src)
55
777k
{
56
777k
  size_t str_size = strlen (src);
57
58
777k
  if (dest_tot_size > str_size)
59
777k
    {
60
777k
      strcpy (dest, src);
61
777k
      return str_size;
62
777k
    }
63
53
  else
64
53
    {
65
53
      if (dest_tot_size > 0)
66
53
  {
67
53
    str_size = dest_tot_size - 1;
68
53
    memcpy (dest, src, str_size);
69
53
    dest[str_size] = 0;
70
53
    return str_size;
71
53
  }
72
0
      else
73
0
  return 0;
74
53
    }
75
777k
}