/src/sudo/lib/util/strlcpy.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* $OpenBSD: strlcpy.c,v 1.12 2015/01/15 03:54:12 millert Exp $ */ |
2 | | |
3 | | /* |
4 | | * SPDX-License-Identifier: ISC |
5 | | * |
6 | | * Copyright (c) 1998, 2003-2005, 2010-2011, 2013-2015 |
7 | | * Todd C. Miller <Todd.Miller@sudo.ws> |
8 | | * |
9 | | * Permission to use, copy, modify, and distribute this software for any |
10 | | * purpose with or without fee is hereby granted, provided that the above |
11 | | * copyright notice and this permission notice appear in all copies. |
12 | | * |
13 | | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
14 | | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
15 | | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
16 | | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
17 | | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
18 | | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
19 | | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
20 | | */ |
21 | | |
22 | | /* |
23 | | * This is an open source non-commercial project. Dear PVS-Studio, please check it. |
24 | | * PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com |
25 | | */ |
26 | | |
27 | | #include <config.h> |
28 | | |
29 | | #ifndef HAVE_STRLCPY |
30 | | |
31 | | #include <string.h> |
32 | | |
33 | | #include "sudo_compat.h" |
34 | | |
35 | | /* |
36 | | * Copy string src to buffer dst of size dsize. At most dsize-1 |
37 | | * chars will be copied. Always NUL terminates (unless dsize == 0). |
38 | | * Returns strlen(src); if retval >= dsize, truncation occurred. |
39 | | */ |
40 | | size_t |
41 | | sudo_strlcpy(char *dst, const char *src, size_t dsize) |
42 | 0 | { |
43 | 0 | const char *osrc = src; |
44 | 0 | size_t nleft = dsize; |
45 | | |
46 | | /* Copy as many bytes as will fit. */ |
47 | 0 | if (nleft != 0) { |
48 | 0 | while (--nleft != 0) { |
49 | 0 | if ((*dst++ = *src++) == '\0') |
50 | 0 | break; |
51 | 0 | } |
52 | 0 | } |
53 | | |
54 | | /* Not enough room in dst, add NUL and traverse rest of src. */ |
55 | 0 | if (nleft == 0) { |
56 | 0 | if (dsize != 0) |
57 | 0 | *dst = '\0'; /* NUL-terminate dst */ |
58 | 0 | while (*src++) |
59 | 0 | continue; |
60 | 0 | } |
61 | |
|
62 | 0 | return(src - osrc - 1); /* count does not include NUL */ |
63 | 0 | } |
64 | | #endif /* HAVE_STRLCPY */ |