Coverage Report

Created: 2025-07-23 06:36

/src/libtasn1/lib/gstr.c
Line
Count
Source (jump to first uncovered line)
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
25.6k
{
35
25.6k
  size_t str_size = strlen (src);
36
25.6k
  size_t dest_size = strlen (dest);
37
38
25.6k
  if (dest_tot_size - dest_size > str_size)
39
25.0k
    {
40
25.0k
      strcat (dest, src);
41
25.0k
    }
42
663
  else
43
663
    {
44
663
      if (dest_tot_size > dest_size)
45
663
  {
46
663
    strncat (dest, src, (dest_tot_size - dest_size) - 1);
47
663
    dest[dest_tot_size - 1] = 0;
48
663
  }
49
663
    }
50
25.6k
}
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
103k
{
56
103k
  size_t str_size = strlen (src);
57
58
103k
  if (dest_tot_size > str_size)
59
103k
    {
60
103k
      strcpy (dest, src);
61
103k
      return str_size;
62
103k
    }
63
51
  else
64
51
    {
65
51
      if (dest_tot_size > 0)
66
51
  {
67
51
    str_size = dest_tot_size - 1;
68
51
    memcpy (dest, src, str_size);
69
51
    dest[str_size] = 0;
70
51
    return str_size;
71
51
  }
72
0
      else
73
0
  return 0;
74
51
    }
75
103k
}