Print this page
10703 smatch unreachable code checking needs reworking
Reviewed by: Toomas Soome <tsoome@me.com>
Reviewed by: Yuri Pankov <yuri.pankov@nexenta.com>
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/lib/scsi/libsmp/common/smp_subr.c
+++ new/usr/src/lib/scsi/libsmp/common/smp_subr.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
↓ open down ↓ |
15 lines elided |
↑ open up ↑ |
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21
22 22 /*
23 23 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
24 24 */
25 25
26 +/*
27 + * Copyright 2019 Joyent, Inc.
28 + */
29 +
26 30 #include <sys/types.h>
27 31
28 32 #include <stddef.h>
29 33 #include <stdlib.h>
30 34 #include <string.h>
31 35 #include <strings.h>
32 36 #include <alloca.h>
33 37 #include <stdio.h>
34 38 #include <unistd.h>
35 39 #include <dlfcn.h>
36 40 #include <thread.h>
37 41 #include <pthread.h>
38 42 #include <ctype.h>
39 43
40 44 #include <scsi/libsmp.h>
41 45 #include <scsi/libsmp_plugin.h>
42 46 #include "smp_impl.h"
43 47
44 48 __thread smp_errno_t _smp_errno;
45 49 __thread char _smp_errmsg[LIBSMP_ERRMSGLEN];
46 50
47 51 int
48 52 smp_assert(const char *expr, const char *file, int line)
49 53 {
50 54 char *msg;
51 55 size_t len;
52 56
53 57 len = snprintf(NULL, 0,
↓ open down ↓ |
18 lines elided |
↑ open up ↑ |
54 58 "ABORT: \"%s\", line %d: assertion failed: %s\n", file, line, expr);
55 59
56 60 msg = alloca(len + 1);
57 61
58 62 (void) snprintf(msg, len + 1,
59 63 "ABORT: \"%s\", line %d: assertion failed: %s\n", file, line, expr);
60 64
61 65 (void) write(STDERR_FILENO, msg, strlen(msg));
62 66
63 67 abort();
64 - _exit(1);
65 -
66 68 /*NOTREACHED*/
67 - return (0);
68 69 }
69 70
70 71 int
71 72 smp_set_errno(smp_errno_t err)
72 73 {
73 74 _smp_errno = err;
74 75 _smp_errmsg[0] = '\0';
75 76
76 77 return (-1);
77 78 }
78 79
79 80 /*
80 81 * Internal routine for setting both _smp_errno and _smp_errmsg. We save
81 82 * and restore the UNIX errno across this routing so the caller can use either
82 83 * smp_set_errno(), smp_error(), or smp_verror() without this value changing.
83 84 */
84 85 int
85 86 smp_verror(smp_errno_t err, const char *fmt, va_list ap)
86 87 {
87 88 size_t n;
88 89 char *errmsg;
89 90
90 91 /*
91 92 * To allow the existing error message to itself be used in an error
92 93 * message, we put the new error message into a buffer on the stack,
93 94 * and then copy it into lsh_errmsg. We also need to set the errno,
94 95 * but because the call to smp_set_errno() is destructive to
95 96 * lsh_errmsg, we do this after we print into our temporary buffer
96 97 * (in case _smp_errmsg is part of the error message) and before we
97 98 * copy the temporary buffer on to _smp_errmsg (to prevent our new
98 99 * message from being nuked by the call to smp_set_errno()).
99 100 */
100 101 errmsg = alloca(sizeof (_smp_errmsg));
101 102 (void) vsnprintf(errmsg, sizeof (_smp_errmsg), fmt, ap);
102 103 (void) smp_set_errno(err);
103 104
104 105 n = strlen(errmsg);
105 106
106 107 if (n != 0 && errmsg[n - 1] == '\n')
107 108 errmsg[n - 1] = '\0';
108 109
109 110 bcopy(errmsg, _smp_errmsg, n + 1);
110 111
111 112 return (-1);
112 113 }
113 114
114 115 int
115 116 smp_error(smp_errno_t err, const char *fmt, ...)
116 117 {
117 118 va_list ap;
118 119
119 120 if (fmt == NULL)
120 121 return (smp_set_errno(err));
121 122
122 123 va_start(ap, fmt);
123 124 err = smp_verror(err, fmt, ap);
124 125 va_end(ap);
125 126
126 127 return (err);
127 128 }
128 129
129 130 smp_errno_t
130 131 smp_errno(void)
131 132 {
132 133 return (_smp_errno);
133 134 }
134 135
135 136 const char *
136 137 smp_errmsg(void)
137 138 {
138 139 if (_smp_errmsg[0] == '\0')
139 140 (void) strlcpy(_smp_errmsg, smp_strerror(_smp_errno),
140 141 sizeof (_smp_errmsg));
141 142
142 143 return (_smp_errmsg);
143 144 }
144 145
145 146 /*ARGSUSED*/
146 147 void *
147 148 smp_alloc(size_t size)
148 149 {
149 150 void *mem;
150 151
151 152 if (size == 0) {
152 153 (void) smp_set_errno(ESMP_ZERO_LENGTH);
153 154 return (NULL);
154 155 }
155 156
156 157 if ((mem = malloc(size)) == NULL)
157 158 (void) smp_set_errno(ESMP_NOMEM);
158 159
159 160 return (mem);
160 161 }
161 162
162 163 void *
163 164 smp_zalloc(size_t size)
164 165 {
165 166 void *mem;
166 167
167 168 if ((mem = smp_alloc(size)) == NULL)
168 169 return (NULL);
169 170
170 171 bzero(mem, size);
171 172
172 173 return (mem);
173 174 }
174 175
175 176 char *
176 177 smp_strdup(const char *str)
177 178 {
178 179 size_t len = strlen(str);
179 180 char *dup = smp_alloc(len + 1);
180 181
181 182 if (dup == NULL)
182 183 return (NULL);
183 184
184 185 return (strcpy(dup, str));
185 186 }
186 187
187 188 void
188 189 smp_free(void *ptr)
189 190 {
190 191 free(ptr);
191 192 }
192 193
193 194 /*
194 195 * Trim any leading and/or trailing spaces from the fixed-length string
195 196 * argument and return a newly-allocated copy of it.
196 197 */
197 198 char *
198 199 smp_trim_strdup(const char *str, size_t len)
199 200 {
200 201 const char *p;
201 202 char *r;
202 203
203 204 for (p = str; p - str < len && isspace(*p); p++)
204 205 ;
205 206
206 207 len -= (p - str);
207 208
208 209 if (len == 0)
209 210 return (NULL);
210 211
211 212 for (str = p + len - 1; str > p && isspace(*str); str--, len--)
212 213 ;
213 214
214 215 if (len == 0)
215 216 return (NULL);
216 217
217 218 r = smp_alloc(len + 1);
218 219 if (r == NULL)
219 220 return (NULL);
220 221
221 222 bcopy(p, r, len);
222 223 r[len] = '\0';
223 224
224 225 return (r);
225 226 }
226 227
227 228 int
228 229 smp_init(int version)
229 230 {
230 231 if (version != LIBSMP_VERSION)
231 232 return (smp_error(ESMP_VERSION,
232 233 "library version %d does not match requested version %d",
233 234 LIBSMP_VERSION, version));
234 235
235 236 smp_engine_init();
236 237
237 238 return (0);
238 239 }
239 240
240 241 void
241 242 smp_fini(void)
242 243 {
243 244 smp_engine_fini();
244 245 }
↓ open down ↓ |
167 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX