Absolute File Name: | /home/opencoverage/opencoverage/guest-scripts/openssh/src/xmalloc.c |
Source code | Switch to Preprocessed file |
Line | Source | Count | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1 | /* $OpenBSD: xmalloc.c,v 1.34 2017/05/31 09:15:42 deraadt Exp $ */ | - | ||||||||||||
2 | /* | - | ||||||||||||
3 | * Author: Tatu Ylonen <ylo@cs.hut.fi> | - | ||||||||||||
4 | * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland | - | ||||||||||||
5 | * All rights reserved | - | ||||||||||||
6 | * Versions of malloc and friends that check their results, and never return | - | ||||||||||||
7 | * failure (they call fatal if they encounter an error). | - | ||||||||||||
8 | * | - | ||||||||||||
9 | * As far as I am concerned, the code I have written for this software | - | ||||||||||||
10 | * can be used freely for any purpose. Any derived versions of this | - | ||||||||||||
11 | * software must be clearly marked as such, and if the derived work is | - | ||||||||||||
12 | * incompatible with the protocol description in the RFC file, it must be | - | ||||||||||||
13 | * called by a name other than "ssh" or "Secure Shell". | - | ||||||||||||
14 | */ | - | ||||||||||||
15 | - | |||||||||||||
16 | #include "includes.h" | - | ||||||||||||
17 | - | |||||||||||||
18 | #include <stdarg.h> | - | ||||||||||||
19 | #ifdef HAVE_STDINT_H | - | ||||||||||||
20 | #include <stdint.h> | - | ||||||||||||
21 | #endif | - | ||||||||||||
22 | #include <stdio.h> | - | ||||||||||||
23 | #include <stdlib.h> | - | ||||||||||||
24 | #include <string.h> | - | ||||||||||||
25 | - | |||||||||||||
26 | #include "xmalloc.h" | - | ||||||||||||
27 | #include "log.h" | - | ||||||||||||
28 | - | |||||||||||||
29 | void | - | ||||||||||||
30 | ssh_malloc_init(void) | - | ||||||||||||
31 | { | - | ||||||||||||
32 | #if defined(__OpenBSD__) | - | ||||||||||||
33 | extern char *malloc_options; | - | ||||||||||||
34 | - | |||||||||||||
35 | malloc_options = "S"; | - | ||||||||||||
36 | #endif /* __OpenBSD__ */ | - | ||||||||||||
37 | } | - | ||||||||||||
38 | - | |||||||||||||
39 | void * | - | ||||||||||||
40 | xmalloc(size_t size) | - | ||||||||||||
41 | { | - | ||||||||||||
42 | void *ptr; | - | ||||||||||||
43 | - | |||||||||||||
44 | if (size == 0)
| 0-7981 | ||||||||||||
45 | fatal("xmalloc: zero size"); never executed: fatal("xmalloc: zero size"); | 0 | ||||||||||||
46 | ptr = malloc(size); | - | ||||||||||||
47 | if (ptr == NULL)
| 0-7981 | ||||||||||||
48 | fatal("xmalloc: out of memory (allocating %zu bytes)", size); never executed: fatal("xmalloc: out of memory (allocating %zu bytes)", size); | 0 | ||||||||||||
49 | return ptr; executed 7981 times by 4 tests: return ptr; Executed by:
| 7981 | ||||||||||||
50 | } | - | ||||||||||||
51 | - | |||||||||||||
52 | void * | - | ||||||||||||
53 | xcalloc(size_t nmemb, size_t size) | - | ||||||||||||
54 | { | - | ||||||||||||
55 | void *ptr; | - | ||||||||||||
56 | - | |||||||||||||
57 | if (size == 0 || nmemb == 0)
| 0-10 | ||||||||||||
58 | fatal("xcalloc: zero size"); never executed: fatal("xcalloc: zero size"); | 0 | ||||||||||||
59 | if (SIZE_MAX / nmemb < size)
| 0-10 | ||||||||||||
60 | fatal("xcalloc: nmemb * size > SIZE_MAX"); never executed: fatal("xcalloc: nmemb * size > SIZE_MAX"); | 0 | ||||||||||||
61 | ptr = calloc(nmemb, size); | - | ||||||||||||
62 | if (ptr == NULL)
| 0-10 | ||||||||||||
63 | fatal("xcalloc: out of memory (allocating %zu bytes)", never executed: fatal("xcalloc: out of memory (allocating %zu bytes)", size * nmemb); | 0 | ||||||||||||
64 | size * nmemb); never executed: fatal("xcalloc: out of memory (allocating %zu bytes)", size * nmemb); | 0 | ||||||||||||
65 | return ptr; executed 10 times by 1 test: return ptr; Executed by:
| 10 | ||||||||||||
66 | } | - | ||||||||||||
67 | - | |||||||||||||
68 | void * | - | ||||||||||||
69 | xreallocarray(void *ptr, size_t nmemb, size_t size) | - | ||||||||||||
70 | { | - | ||||||||||||
71 | void *new_ptr; | - | ||||||||||||
72 | - | |||||||||||||
73 | new_ptr = reallocarray(ptr, nmemb, size); | - | ||||||||||||
74 | if (new_ptr == NULL)
| 0-1 | ||||||||||||
75 | fatal("xreallocarray: out of memory (%zu elements of %zu bytes)", never executed: fatal("xreallocarray: out of memory (%zu elements of %zu bytes)", nmemb, size); | 0 | ||||||||||||
76 | nmemb, size); never executed: fatal("xreallocarray: out of memory (%zu elements of %zu bytes)", nmemb, size); | 0 | ||||||||||||
77 | return new_ptr; executed 1 time by 1 test: return new_ptr; Executed by:
| 1 | ||||||||||||
78 | } | - | ||||||||||||
79 | - | |||||||||||||
80 | void * | - | ||||||||||||
81 | xrecallocarray(void *ptr, size_t onmemb, size_t nmemb, size_t size) | - | ||||||||||||
82 | { | - | ||||||||||||
83 | void *new_ptr; | - | ||||||||||||
84 | - | |||||||||||||
85 | new_ptr = recallocarray(ptr, onmemb, nmemb, size); | - | ||||||||||||
86 | if (new_ptr == NULL)
| 0-14 | ||||||||||||
87 | fatal("xrecallocarray: out of memory (%zu elements of %zu bytes)", never executed: fatal("xrecallocarray: out of memory (%zu elements of %zu bytes)", nmemb, size); | 0 | ||||||||||||
88 | nmemb, size); never executed: fatal("xrecallocarray: out of memory (%zu elements of %zu bytes)", nmemb, size); | 0 | ||||||||||||
89 | return new_ptr; executed 14 times by 1 test: return new_ptr; Executed by:
| 14 | ||||||||||||
90 | } | - | ||||||||||||
91 | - | |||||||||||||
92 | char * | - | ||||||||||||
93 | xstrdup(const char *str) | - | ||||||||||||
94 | { | - | ||||||||||||
95 | size_t len; | - | ||||||||||||
96 | char *cp; | - | ||||||||||||
97 | - | |||||||||||||
98 | len = strlen(str) + 1; | - | ||||||||||||
99 | cp = xmalloc(len); | - | ||||||||||||
100 | strlcpy(cp, str, len); | - | ||||||||||||
101 | return cp; executed 7563 times by 4 tests: return cp; Executed by:
| 7563 | ||||||||||||
102 | } | - | ||||||||||||
103 | - | |||||||||||||
104 | int | - | ||||||||||||
105 | xasprintf(char **ret, const char *fmt, ...) | - | ||||||||||||
106 | { | - | ||||||||||||
107 | va_list ap; | - | ||||||||||||
108 | int i; | - | ||||||||||||
109 | - | |||||||||||||
110 | va_start(ap, fmt); | - | ||||||||||||
111 | i = vasprintf(ret, fmt, ap); | - | ||||||||||||
112 | va_end(ap); | - | ||||||||||||
113 | - | |||||||||||||
114 | if (i < 0 || *ret == NULL)
| 0 | ||||||||||||
115 | fatal("xasprintf: could not allocate memory"); never executed: fatal("xasprintf: could not allocate memory"); | 0 | ||||||||||||
116 | - | |||||||||||||
117 | return (i); never executed: return (i); | 0 | ||||||||||||
118 | } | - | ||||||||||||
Source code | Switch to Preprocessed file |