/src/sudo/plugins/sudoers/strlcpy_unesc.c
Line | Count | Source |
1 | | /* |
2 | | * SPDX-License-Identifier: ISC |
3 | | * |
4 | | * Copyright (c) 2021 Todd C. Miller <Todd.Miller@sudo.ws> |
5 | | * |
6 | | * Permission to use, copy, modify, and distribute this software for any |
7 | | * purpose with or without fee is hereby granted, provided that the above |
8 | | * copyright notice and this permission notice appear in all copies. |
9 | | * |
10 | | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
11 | | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
12 | | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
13 | | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
14 | | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
15 | | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
16 | | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
17 | | */ |
18 | | |
19 | | /* |
20 | | * This is an open source non-commercial project. Dear PVS-Studio, please check it. |
21 | | * PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com |
22 | | */ |
23 | | |
24 | | #include <config.h> |
25 | | |
26 | | #include <sys/types.h> |
27 | | #include <stdio.h> |
28 | | #include <ctype.h> |
29 | | |
30 | | #include <sudoers.h> |
31 | | |
32 | | /* |
33 | | * Like strlcpy() but collapses non-space chars escaped with a backslash. |
34 | | */ |
35 | | size_t |
36 | | strlcpy_unescape(char *restrict dst, const char *restrict src, size_t size) |
37 | 270k | { |
38 | 270k | size_t len = 0; |
39 | 270k | char ch; |
40 | 270k | debug_decl(strlcpy_unescape, SUDOERS_DEBUG_UTIL); |
41 | | |
42 | 24.3M | while ((ch = *src++) != '\0') { |
43 | 24.0M | if (ch == '\\' && *src != '\0' && !isspace((unsigned char)*src)) |
44 | 8.46k | ch = *src++; |
45 | 24.0M | if (size > 1) { |
46 | 24.0M | *dst++ = ch; |
47 | 24.0M | size--; |
48 | 24.0M | } |
49 | 24.0M | len++; |
50 | 24.0M | } |
51 | 270k | if (size > 0) |
52 | 270k | *dst = '\0'; |
53 | | |
54 | 270k | debug_return_size_t(len); |
55 | 270k | } |