Blame


1 f224a3fe 2021-08-24 xhr /* Author: Tatu Ylonen <ylo@cs.hut.fi>
2 f224a3fe 2021-08-24 xhr * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
3 f224a3fe 2021-08-24 xhr * All rights reserved
4 f224a3fe 2021-08-24 xhr * Versions of malloc and friends that check their results, and never return
5 f224a3fe 2021-08-24 xhr * failure (they call fatal if they encounter an error).
6 f224a3fe 2021-08-24 xhr *
7 f224a3fe 2021-08-24 xhr * As far as I am concerned, the code I have written for this software
8 f224a3fe 2021-08-24 xhr * can be used freely for any purpose. Any derived versions of this
9 f224a3fe 2021-08-24 xhr * software must be clearly marked as such, and if the derived work is
10 f224a3fe 2021-08-24 xhr * incompatible with the protocol description in the RFC file, it must be
11 f224a3fe 2021-08-24 xhr * called by a name other than "ssh" or "Secure Shell".
12 f224a3fe 2021-08-24 xhr */
13 f224a3fe 2021-08-24 xhr
14 f224a3fe 2021-08-24 xhr #include <sys/types.h>
15 f224a3fe 2021-08-24 xhr
16 f224a3fe 2021-08-24 xhr #include <stddef.h>
17 f224a3fe 2021-08-24 xhr #include <stdlib.h>
18 f224a3fe 2021-08-24 xhr #include <string.h>
19 f224a3fe 2021-08-24 xhr
20 f224a3fe 2021-08-24 xhr #include "log.h"
21 f224a3fe 2021-08-24 xhr #include "twind.h"
22 f224a3fe 2021-08-24 xhr
23 f224a3fe 2021-08-24 xhr void *
24 f224a3fe 2021-08-24 xhr xmalloc(size_t size)
25 f224a3fe 2021-08-24 xhr {
26 f224a3fe 2021-08-24 xhr void *ptr;
27 f224a3fe 2021-08-24 xhr
28 f224a3fe 2021-08-24 xhr if (size == 0)
29 f224a3fe 2021-08-24 xhr fatal("xmalloc: zero size");
30 cebc5223 2021-08-25 xhr ptr = calloc(1, size);
31 f224a3fe 2021-08-24 xhr if (ptr == NULL)
32 f224a3fe 2021-08-24 xhr fatal("xmalloc: out of memory (allocating %zu bytes)", size);
33 f224a3fe 2021-08-24 xhr return ptr;
34 f224a3fe 2021-08-24 xhr }
35 f224a3fe 2021-08-24 xhr
36 f224a3fe 2021-08-24 xhr char *
37 f224a3fe 2021-08-24 xhr xstrdup(const char *str)
38 f224a3fe 2021-08-24 xhr {
39 f224a3fe 2021-08-24 xhr size_t len;
40 f224a3fe 2021-08-24 xhr char *cp;
41 f224a3fe 2021-08-24 xhr
42 f224a3fe 2021-08-24 xhr len = strlen(str) + 1;
43 f224a3fe 2021-08-24 xhr cp = xmalloc(len);
44 f224a3fe 2021-08-24 xhr strlcpy(cp, str, len);
45 f224a3fe 2021-08-24 xhr return cp;
46 f224a3fe 2021-08-24 xhr }
47 f224a3fe 2021-08-24 xhr
48 f224a3fe 2021-08-24 xhr /*
49 f224a3fe 2021-08-24 xhr * Copyright (c) 1998, 2015 Todd C. Miller <millert@openbsd.org>
50 f224a3fe 2021-08-24 xhr *
51 f224a3fe 2021-08-24 xhr * Permission to use, copy, modify, and distribute this software for any
52 f224a3fe 2021-08-24 xhr * purpose with or without fee is hereby granted, provided that the above
53 f224a3fe 2021-08-24 xhr * copyright notice and this permission notice appear in all copies.
54 f224a3fe 2021-08-24 xhr *
55 f224a3fe 2021-08-24 xhr * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
56 f224a3fe 2021-08-24 xhr * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
57 f224a3fe 2021-08-24 xhr * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
58 f224a3fe 2021-08-24 xhr * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
59 f224a3fe 2021-08-24 xhr * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
60 f224a3fe 2021-08-24 xhr * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
61 f224a3fe 2021-08-24 xhr * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
62 f224a3fe 2021-08-24 xhr */
63 f224a3fe 2021-08-24 xhr
64 f224a3fe 2021-08-24 xhr
65 f224a3fe 2021-08-24 xhr #if !defined(__FreeBSD__) && !defined(__OpenBSD__) && !defined(__NetBSD__) &&\
66 f224a3fe 2021-08-24 xhr !defined(__DragonFly__)
67 f224a3fe 2021-08-24 xhr /*
68 f224a3fe 2021-08-24 xhr * Copy string src to buffer dst of size dsize. At most dsize-1
69 f224a3fe 2021-08-24 xhr * chars will be copied. Always NUL terminates (unless dsize == 0).
70 f224a3fe 2021-08-24 xhr * Returns strlen(src); if retval >= dsize, truncation occurred.
71 f224a3fe 2021-08-24 xhr */
72 f224a3fe 2021-08-24 xhr size_t
73 f224a3fe 2021-08-24 xhr strlcpy(char *dst, const char *src, size_t dsize)
74 f224a3fe 2021-08-24 xhr {
75 f224a3fe 2021-08-24 xhr const char *osrc = src;
76 f224a3fe 2021-08-24 xhr size_t nleft = dsize;
77 f224a3fe 2021-08-24 xhr
78 f224a3fe 2021-08-24 xhr /* Copy as many bytes as will fit. */
79 f224a3fe 2021-08-24 xhr if (nleft != 0) {
80 f224a3fe 2021-08-24 xhr while (--nleft != 0) {
81 f224a3fe 2021-08-24 xhr if ((*dst++ = *src++) == '\0')
82 f224a3fe 2021-08-24 xhr break;
83 f224a3fe 2021-08-24 xhr }
84 f224a3fe 2021-08-24 xhr }
85 f224a3fe 2021-08-24 xhr
86 f224a3fe 2021-08-24 xhr /* Not enough room in dst, add NUL and traverse rest of src. */
87 f224a3fe 2021-08-24 xhr if (nleft == 0) {
88 f224a3fe 2021-08-24 xhr if (dsize != 0)
89 f224a3fe 2021-08-24 xhr *dst = '\0'; /* NUL-terminate dst */
90 f224a3fe 2021-08-24 xhr while (*src++)
91 f224a3fe 2021-08-24 xhr ;
92 f224a3fe 2021-08-24 xhr }
93 f224a3fe 2021-08-24 xhr
94 f224a3fe 2021-08-24 xhr return(src - osrc - 1); /* count does not include NUL */
95 f224a3fe 2021-08-24 xhr }
96 f224a3fe 2021-08-24 xhr #endif /* __BSD__ */