Coverage Report

Created: 2026-06-07 07:07

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/samba/lib/util/util_str_escape.c
Line
Count
Source
1
/*
2
   Samba string escaping routines
3
4
   Copyright (C) Andrew Bartlett <abartlet@samba.org> 2017
5
6
   This program is free software; you can redistribute it and/or modify
7
   it under the terms of the GNU General Public License as published by
8
   the Free Software Foundation; either version 3 of the License, or
9
   (at your option) any later version.
10
11
   This program is distributed in the hope that it will be useful,
12
   but WITHOUT ANY WARRANTY; without even the implied warranty of
13
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
   GNU General Public License for more details.
15
16
   You should have received a copy of the GNU General Public License
17
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
*/
19
20
#include "replace.h"
21
#include "system/locale.h"
22
#include "lib/util/debug.h"
23
#include "lib/util/util_str_escape.h"
24
25
26
/*
27
 * Calculate the encoded length of a character for log_escape
28
 *
29
 */
30
static size_t encoded_length(unsigned char c)
31
0
{
32
0
  if (c != '\\' && !iscntrl(c)) {
33
0
    return 1;
34
0
  } else {
35
0
    switch (c) {
36
0
    case '\a':
37
0
    case '\b':
38
0
    case '\f':
39
0
    case '\n':
40
0
    case '\r':
41
0
    case '\t':
42
0
    case '\v':
43
0
    case '\\':
44
0
      return 2;  /* C escape sequence */
45
0
    default:
46
0
      return 4;  /* hex escape \xhh   */
47
0
    }
48
0
  }
49
0
}
50
51
/*
52
 * Escape any control characters in the inputs to prevent them from
53
 * interfering with the log output.
54
 */
55
char *log_escape(TALLOC_CTX *frame, const char *in)
56
0
{
57
0
  size_t size = 0;        /* Space to allocate for the escaped data */
58
0
  char *encoded = NULL;   /* The encoded string                     */
59
0
  const char *c;
60
0
  char *e;
61
62
0
  if (in == NULL) {
63
0
    return NULL;
64
0
  }
65
66
  /* Calculate the size required for the escaped array */
67
0
  c = in;
68
0
  while (*c) {
69
0
    size += encoded_length( *c);
70
0
    c++;
71
0
  }
72
0
  size++;
73
74
0
  encoded = talloc_array( frame, char, size);
75
0
  if (encoded == NULL) {
76
0
    DBG_ERR( "Out of memory allocating encoded string\n");
77
0
    return NULL;
78
0
  }
79
80
0
  c = in;
81
0
  e = encoded;
82
0
  while (*c) {
83
0
    if (*c != '\\' && !iscntrl((unsigned char)(*c))) {
84
0
      *e++ = *c++;
85
0
    } else {
86
0
      switch (*c) {
87
0
      case '\a':
88
0
        *e++ = '\\';
89
0
        *e++ = 'a';
90
0
        break;
91
0
      case '\b':
92
0
        *e++ = '\\';
93
0
        *e++ = 'b';
94
0
        break;
95
0
      case '\f':
96
0
        *e++ = '\\';
97
0
        *e++ = 'f';
98
0
        break;
99
0
      case '\n':
100
0
        *e++ = '\\';
101
0
        *e++ = 'n';
102
0
        break;
103
0
      case '\r':
104
0
        *e++ = '\\';
105
0
        *e++ = 'r';
106
0
        break;
107
0
      case '\t':
108
0
        *e++ = '\\';
109
0
        *e++ = 't';
110
0
        break;
111
0
      case '\v':
112
0
        *e++ = '\\';
113
0
        *e++ = 'v';
114
0
        break;
115
0
      case '\\':
116
0
        *e++ = '\\';
117
0
        *e++ = '\\';
118
0
        break;
119
0
      default:
120
0
        snprintf(e, 5, "\\x%02hhX", (unsigned char)(*c));
121
0
        e += 4;
122
0
      }
123
0
      c++;
124
0
    }
125
0
  }
126
0
  *e = '\0';
127
0
  return encoded;
128
0
}