Skip to content

Commit e375330

Browse files
committed
copy_string: use an end pointer to quiet a coverity warning
Instead of modifying the len parameter and using it for bounds checking, compute the end of the source string and bound check on that instead. Also simplify the code slightly and enable debugging.
1 parent af4634a commit e375330

1 file changed

Lines changed: 15 additions & 14 deletions

File tree

plugins/sudoers/toke_util.c

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* SPDX-License-Identifier: ISC
33
*
4-
* Copyright (c) 1996, 1998-2005, 2007-2016
4+
* Copyright (c) 1996, 1998-2005, 2007-2023, 2025
55
* Todd C. Miller <[email protected]>
66
*
77
* Permission to use, copy, modify, and distribute this software for any
@@ -47,24 +47,25 @@ static size_t arg_size = 0;
4747
static void
4848
copy_string(char *dst, const char *src, size_t len)
4949
{
50-
int h;
51-
52-
while (len--) {
53-
if (*src == '\\' && len) {
54-
if (src[1] == 'x' && len >= 3 && (h = sudo_hexchar(src + 2)) != -1) {
55-
*dst++ = (char)h;
56-
src += 4;
57-
len -= 3;
50+
const char *end = src + len;
51+
debug_decl(copy_string, SUDOERS_DEBUG_PARSER);
52+
53+
while (src < end) {
54+
int ch = *src++;
55+
if (ch == '\\' && src < end) {
56+
if (*src == 'x' && src + 3 <= end && (ch = sudo_hexchar(src + 1)) != -1) {
57+
/* Hex character, skip remaining part of src. */
58+
src += 3;
5859
} else {
59-
src++;
60-
len--;
61-
*dst++ = *src++;
60+
/* Escaped regular character. */
61+
ch = *src++;
6262
}
63-
} else {
64-
*dst++ = *src++;
6563
}
64+
*dst++ = (char)ch;
6665
}
6766
*dst = '\0';
67+
68+
debug_return;
6869
}
6970

7071
bool

0 commit comments

Comments
 (0)