Debian Codegraph @

src/lib/libdwarf/dwarf_safe_strcpy.c

raw
1 /*
2 Copyright (c) 2021, David Anderson
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with
6 or without modification, are permitted provided that the
7 following conditions are met:
8
9 Redistributions of source code must retain the above
10 copyright notice, this list of conditions and the following
11 disclaimer.
12
13 Redistributions in binary form must reproduce the above
14 copyright notice, this list of conditions and the following
15 disclaimer in the documentation and/or other materials
16 provided with the distribution.
17
18 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 #include <config.h>
34 #include <stddef.h> /* NULL size_t */
35 #include "dwarf_safe_strcpy.h"
36
37 /* An strcpy/strncpy which ensures NUL terminated string
38 and never overruns the output.
39 inlen is strlen() size of in_s
40 outlen is buffer and has to have space
41 for the NUL in in_s to avoid truncation.
42 So typically outlen == (inlen+1).
43
44 If outlen is 0 it quietly returns.
45 If outlen is 1 it assigns a NUL byte to *out and returns.
46 If outlen > 0 then this function always writes
47 a trailing NUL byte.
48
49 ISO C 9899:1990 specifies that if outlen has extra space
50 that the function zeroes the extra bytes.
51 And if outlen is too small no NUL byte is written
52 to out.
53
54 This function writes only one NUL byte, the rest of
55 the 'out' buffer is untouched. Sometimes our callers
56 do not have a narrow bound to outlen, so zeroing
57 all the unused bytes is wasteful (hence we do not
58 do that here).
59
60 If an input is only partly copied due to limited
61 target space this may destroy the
62 correctness of a multi-byte-string (UTF-8).
63 Note that this will not harm dwarfdump as
64 dwarfdump 'sanitizes' all printf output
65 in a uri-style. So it only prints true ASCII characters
66 in the printable range.
67
68 PRECONDITION:
69 The pointer arguments are required to be non-null.
70 */
71 void
72 _dwarf_safe_strcpy(char *out,
73 size_t outlen,
74 const char *in_s,
75 size_t inlen)
76 {
77 size_t full_inlen = inlen+1;
78 char *cpo = 0;
79 const char *cpi= 0;
80 const char *cpiend = 0;
81
82 if (full_inlen >= outlen) {
83 if (!outlen) {
84 return;
85 }
86 cpo = out;
87 cpi= in_s;
88 cpiend = in_s +(outlen-1);
89 } else {
90 /* If outlen is very large
91 strncpy is very wasteful. */
92 cpo = out;
93 cpi= in_s;
94 cpiend = in_s +inlen;
95 }
96 for ( ; *cpi && cpi < cpiend ; ++cpo, ++cpi) {
97 *cpo = *cpi;
98 }
99 *cpo = 0;
100 }