Coverage Report

Created: 2025-03-06 07:58

/src/gnutls/lib/minitasn1/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
5.56k
{
35
5.56k
  size_t str_size = strlen (src);
36
5.56k
  size_t dest_size = strlen (dest);
37
38
5.56k
  if (dest_tot_size - dest_size > str_size)
39
5.56k
    {
40
5.56k
      strcat (dest, src);
41
5.56k
    }
42
0
  else
43
0
    {
44
0
      if (dest_tot_size > dest_size)
45
0
  {
46
0
    strncat (dest, src, (dest_tot_size - dest_size) - 1);
47
0
    dest[dest_tot_size - 1] = 0;
48
0
  }
49
0
    }
50
5.56k
}
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
14.8k
{
56
14.8k
  size_t str_size = strlen (src);
57
58
14.8k
  if (dest_tot_size > str_size)
59
14.8k
    {
60
14.8k
      strcpy (dest, src);
61
14.8k
      return str_size;
62
14.8k
    }
63
0
  else
64
0
    {
65
0
      if (dest_tot_size > 0)
66
0
  {
67
0
    str_size = dest_tot_size - 1;
68
0
    memcpy (dest, src, str_size);
69
0
    dest[str_size] = 0;
70
0
    return str_size;
71
0
  }
72
0
      else
73
0
  return 0;
74
0
    }
75
14.8k
}