Bug Summary

File:.build-ci/../libnvme/src/nvme/ctrl-sysfs-custom-linux.c
Warning:line 79, column 9
Potential leak of memory pointed to by 'target_addr'

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple x86_64-redhat-linux-gnu -O3 -analyze -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name ctrl-sysfs-custom-linux.c -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -mrelocation-model pic -pic-level 2 -fhalf-no-semantic-interposition -mframe-pointer=none -fmath-errno -ffp-contract=on -fno-rounding-math -mconstructor-aliases -funwind-tables=2 -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -fdebug-compilation-dir=/__w/nvme-cli/nvme-cli/.build-ci -fcoverage-compilation-dir=/__w/nvme-cli/nvme-cli/.build-ci -resource-dir /usr/bin/../lib/clang/22 -include /__w/nvme-cli/nvme-cli/.build-ci/nvme-config.h -I libnvme/src/libnvme3.so.1.0.0.p -I libnvme/src -I ../libnvme/src -I ccan -I ../ccan -I . -I .. -I shared -I ../shared -D _FILE_OFFSET_BITS=64 -D _GNU_SOURCE -U NDEBUG -internal-isystem /usr/bin/../lib/clang/22/include -internal-isystem /usr/local/include -internal-isystem /usr/bin/../lib/gcc/x86_64-redhat-linux/16/../../../../x86_64-redhat-linux/include -internal-externc-isystem /include -internal-externc-isystem /usr/include -std=gnu99 -ferror-limit 19 -fvisibility=hidden -fgnuc-version=4.2.1 -fskip-odr-check-in-gmf -fcolor-diagnostics -vectorize-loops -vectorize-slp -analyzer-opt-analyze-headers -analyzer-output=html -faddrsig -fdwarf2-cfi-asm -o /__w/nvme-cli/nvme-cli/.build-ci/scan-results/2026-08-08-045408-589-1 -x c ../libnvme/src/nvme/ctrl-sysfs-custom-linux.c

../libnvme/src/nvme/ctrl-sysfs.c

1// SPDX-License-Identifier: LGPL-2.1-or-later
2
3/*
4 * This file is part of libnvme.
5 *
6 * Copyright (c) 2025, Dell Technologies Inc. or its subsidiaries.
7 * Authors: Martin Belanger <Martin.Belanger@dell.com>
8 *
9 * ____ _ _ ____ _
10 * / ___| ___ _ __ ___ _ __ __ _| |_ ___ __| | / ___|___ __| | ___
11 * | | _ / _ \ '_ \ / _ \ '__/ _` | __/ _ \/ _` | | | / _ \ / _` |/ _ \
12 * | |_| | __/ | | | __/ | | (_| | || __/ (_| | | |__| (_) | (_| | __/
13 * \____|\___|_| |_|\___|_| \__,_|\__\___|\__,_| \____\___/ \__,_|\___|
14 *
15 * Auto-generated struct member accessors (setter/getter)
16 *
17 * To update run: meson compile -C [BUILD-DIR] update-accessors
18 * Or: make update-accessors
19 */
20
21#include <errno(*__errno_location ()).h>
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25
26#include <compiler-attributes.h>
27
28#include "private.h"
29#include "private-tree.h"
30#include "ctrl-sysfs.h"
31
32struct libnvme_ctrl_sysfs {
33 char *numa_node;
34 char *queue_count;
35 char *sqsize;
36 long command_error_count;
37 long reset_count;
38 long reconnect_count;
39 char *firmware;
40 char *model;
41 char *serial;
42 char *cntrltype;
43 char *cntlid;
44 char *dctype;
45 char *phy_slot;
46 char *dhchap_host_key;
47 char *dhchap_ctrl_key;
48 char *keyring;
49};
50
51struct libnvme_ctrl_sysfs *libnvme_ctrl_sysfs_alloc(void)
52{
53 return calloc(1, sizeof(struct libnvme_ctrl_sysfs));
54}
55
56void libnvme_ctrl_sysfs_reset(
57 struct libnvme_ctrl_sysfs *sysfs)
58{
59 if (!sysfs)
60 return;
61
62 SYSFS_FREE(sysfs->numa_node)do { if ((sysfs->numa_node) != ((void*)0) && !((void
*)(sysfs->numa_node) == (void *)NO_SYSFS_ATTR)) free(sysfs
->numa_node); (sysfs->numa_node) = ((void*)0); } while (
0)
;
63 SYSFS_FREE(sysfs->queue_count)do { if ((sysfs->queue_count) != ((void*)0) && !((
void *)(sysfs->queue_count) == (void *)NO_SYSFS_ATTR)) free
(sysfs->queue_count); (sysfs->queue_count) = ((void*)0)
; } while (0)
;
64 SYSFS_FREE(sysfs->sqsize)do { if ((sysfs->sqsize) != ((void*)0) && !((void *
)(sysfs->sqsize) == (void *)NO_SYSFS_ATTR)) free(sysfs->
sqsize); (sysfs->sqsize) = ((void*)0); } while (0)
;
65 SYSFS_FREE(sysfs->firmware)do { if ((sysfs->firmware) != ((void*)0) && !((void
*)(sysfs->firmware) == (void *)NO_SYSFS_ATTR)) free(sysfs
->firmware); (sysfs->firmware) = ((void*)0); } while (0
)
;
66 SYSFS_FREE(sysfs->model)do { if ((sysfs->model) != ((void*)0) && !((void *
)(sysfs->model) == (void *)NO_SYSFS_ATTR)) free(sysfs->
model); (sysfs->model) = ((void*)0); } while (0)
;
67 SYSFS_FREE(sysfs->serial)do { if ((sysfs->serial) != ((void*)0) && !((void *
)(sysfs->serial) == (void *)NO_SYSFS_ATTR)) free(sysfs->
serial); (sysfs->serial) = ((void*)0); } while (0)
;
68 SYSFS_FREE(sysfs->cntrltype)do { if ((sysfs->cntrltype) != ((void*)0) && !((void
*)(sysfs->cntrltype) == (void *)NO_SYSFS_ATTR)) free(sysfs
->cntrltype); (sysfs->cntrltype) = ((void*)0); } while (
0)
;
69 SYSFS_FREE(sysfs->cntlid)do { if ((sysfs->cntlid) != ((void*)0) && !((void *
)(sysfs->cntlid) == (void *)NO_SYSFS_ATTR)) free(sysfs->
cntlid); (sysfs->cntlid) = ((void*)0); } while (0)
;
70 SYSFS_FREE(sysfs->dctype)do { if ((sysfs->dctype) != ((void*)0) && !((void *
)(sysfs->dctype) == (void *)NO_SYSFS_ATTR)) free(sysfs->
dctype); (sysfs->dctype) = ((void*)0); } while (0)
;
71 SYSFS_FREE(sysfs->phy_slot)do { if ((sysfs->phy_slot) != ((void*)0) && !((void
*)(sysfs->phy_slot) == (void *)NO_SYSFS_ATTR)) free(sysfs
->phy_slot); (sysfs->phy_slot) = ((void*)0); } while (0
)
;
72 SYSFS_FREE(sysfs->dhchap_host_key)do { if ((sysfs->dhchap_host_key) != ((void*)0) &&
!((void *)(sysfs->dhchap_host_key) == (void *)NO_SYSFS_ATTR
)) free(sysfs->dhchap_host_key); (sysfs->dhchap_host_key
) = ((void*)0); } while (0)
;
73 SYSFS_FREE(sysfs->dhchap_ctrl_key)do { if ((sysfs->dhchap_ctrl_key) != ((void*)0) &&
!((void *)(sysfs->dhchap_ctrl_key) == (void *)NO_SYSFS_ATTR
)) free(sysfs->dhchap_ctrl_key); (sysfs->dhchap_ctrl_key
) = ((void*)0); } while (0)
;
74 SYSFS_FREE(sysfs->keyring)do { if ((sysfs->keyring) != ((void*)0) && !((void
*)(sysfs->keyring) == (void *)NO_SYSFS_ATTR)) free(sysfs->
keyring); (sysfs->keyring) = ((void*)0); } while (0)
;
75}
76
77void libnvme_ctrl_sysfs_free(
78 struct libnvme_ctrl_sysfs *sysfs)
79{
80 if (!sysfs)
81 return;
82
83 free(sysfs);
84}
85
86__shr_public__attribute__((visibility("default"))) int libnvme_ctrl_get_numa_node(
87 const struct libnvme_ctrl *p,
88 const char **val,
89 const char *dflt)
90{
91 struct libnvme_ctrl *c = (struct libnvme_ctrl *)p;
92
93 *val = dflt;
94
95 if (__shr_unlikely(!SYSFS_IS_LOADED(c->sysfs->numa_node))__builtin_expect(!!(!((c->sysfs->numa_node) != ((void*)
0))), 0)
) {
96 c->sysfs->numa_node = libnvme_get_ctrl_attr(c, "numa_node");
97 if (!c->sysfs->numa_node)
98 c->sysfs->numa_node = NO_SYSFS_ATTR;
99 }
100
101 if (SYSFS_IS_ABSENT(c->sysfs->numa_node)((void *)(c->sysfs->numa_node) == (void *)NO_SYSFS_ATTR
)
)
102 return -ENOENT2;
103
104 *val = c->sysfs->numa_node;
105 return 0;
106}
107
108__shr_public__attribute__((visibility("default"))) int libnvme_ctrl_get_queue_count(
109 const struct libnvme_ctrl *p,
110 const char **val,
111 const char *dflt)
112{
113 struct libnvme_ctrl *c = (struct libnvme_ctrl *)p;
114
115 *val = dflt;
116
117 if (__shr_unlikely(!SYSFS_IS_LOADED(c->sysfs->queue_count))__builtin_expect(!!(!((c->sysfs->queue_count) != ((void
*)0))), 0)
) {
118 c->sysfs->queue_count = libnvme_get_ctrl_attr(c, "queue_count");
119 if (!c->sysfs->queue_count)
120 c->sysfs->queue_count = NO_SYSFS_ATTR;
121 }
122
123 if (SYSFS_IS_ABSENT(c->sysfs->queue_count)((void *)(c->sysfs->queue_count) == (void *)NO_SYSFS_ATTR
)
)
124 return -ENOENT2;
125
126 *val = c->sysfs->queue_count;
127 return 0;
128}
129
130__shr_public__attribute__((visibility("default"))) int libnvme_ctrl_get_sqsize(
131 const struct libnvme_ctrl *p,
132 const char **val,
133 const char *dflt)
134{
135 struct libnvme_ctrl *c = (struct libnvme_ctrl *)p;
136
137 *val = dflt;
138
139 if (__shr_unlikely(!SYSFS_IS_LOADED(c->sysfs->sqsize))__builtin_expect(!!(!((c->sysfs->sqsize) != ((void*)0))
), 0)
) {
140 c->sysfs->sqsize = libnvme_get_ctrl_attr(c, "sqsize");
141 if (!c->sysfs->sqsize)
142 c->sysfs->sqsize = NO_SYSFS_ATTR;
143 }
144
145 if (SYSFS_IS_ABSENT(c->sysfs->sqsize)((void *)(c->sysfs->sqsize) == (void *)NO_SYSFS_ATTR))
146 return -ENOENT2;
147
148 *val = c->sysfs->sqsize;
149 return 0;
150}
151
152__shr_public__attribute__((visibility("default"))) int libnvme_ctrl_get_command_error_count(
153 const struct libnvme_ctrl *p,
154 long *val,
155 long dflt)
156{
157 struct libnvme_ctrl *c = (struct libnvme_ctrl *)p;
158 __cleanup_free__attribute__((cleanup(shr_freep))) char *str = NULL((void*)0);
159
160 *val = dflt;
161
162 str = libnvme_get_ctrl_attr(c, "diag/command_error_count");
163 if (!str)
164 return -ENOENT2;
165
166 if (sscanf(str, "%ld", &c->sysfs->command_error_count) != 1)
167 return -EINVAL22;
168
169 *val = c->sysfs->command_error_count;
170 return 0;
171}
172
173__shr_public__attribute__((visibility("default"))) int libnvme_ctrl_get_reset_count(
174 const struct libnvme_ctrl *p,
175 long *val,
176 long dflt)
177{
178 struct libnvme_ctrl *c = (struct libnvme_ctrl *)p;
179 __cleanup_free__attribute__((cleanup(shr_freep))) char *str = NULL((void*)0);
180
181 *val = dflt;
182
183 str = libnvme_get_ctrl_attr(c, "diag/reset_count");
184 if (!str)
185 return -ENOENT2;
186
187 if (sscanf(str, "%ld", &c->sysfs->reset_count) != 1)
188 return -EINVAL22;
189
190 *val = c->sysfs->reset_count;
191 return 0;
192}
193
194__shr_public__attribute__((visibility("default"))) int libnvme_ctrl_get_reconnect_count(
195 const struct libnvme_ctrl *p,
196 long *val,
197 long dflt)
198{
199 struct libnvme_ctrl *c = (struct libnvme_ctrl *)p;
200 __cleanup_free__attribute__((cleanup(shr_freep))) char *str = NULL((void*)0);
201
202 *val = dflt;
203
204 str = libnvme_get_ctrl_attr(c, "diag/reconnect_count");
205 if (!str)
206 return -ENOENT2;
207
208 if (sscanf(str, "%ld", &c->sysfs->reconnect_count) != 1)
209 return -EINVAL22;
210
211 *val = c->sysfs->reconnect_count;
212 return 0;
213}
214
215__shr_public__attribute__((visibility("default"))) void libnvme_ctrl_set_firmware(
216 struct libnvme_ctrl *p,
217 const char *firmware)
218{
219 SYSFS_FREE(p->sysfs->firmware)do { if ((p->sysfs->firmware) != ((void*)0) && !
((void *)(p->sysfs->firmware) == (void *)NO_SYSFS_ATTR)
) free(p->sysfs->firmware); (p->sysfs->firmware) =
((void*)0); } while (0)
;
220 p->sysfs->firmware = firmware ? strdup(firmware) : NO_SYSFS_ATTR;
221}
222
223__shr_public__attribute__((visibility("default"))) int libnvme_ctrl_get_firmware(
224 const struct libnvme_ctrl *p,
225 const char **val,
226 const char *dflt)
227{
228 struct libnvme_ctrl *c = (struct libnvme_ctrl *)p;
229 int ret;
230
231 *val = dflt;
232
233 if (__shr_unlikely(!SYSFS_IS_LOADED(c->sysfs->firmware))__builtin_expect(!!(!((c->sysfs->firmware) != ((void*)0
))), 0)
) {
234 ret = libnvme_ctrl_load_identity(c);
235 if (ret)
236 return ret;
237 if (!c->sysfs->firmware)
238 c->sysfs->firmware = NO_SYSFS_ATTR;
239 }
240
241 if (SYSFS_IS_ABSENT(c->sysfs->firmware)((void *)(c->sysfs->firmware) == (void *)NO_SYSFS_ATTR))
242 return -ENOENT2;
243
244 *val = c->sysfs->firmware;
245 return 0;
246}
247
248__shr_public__attribute__((visibility("default"))) void libnvme_ctrl_set_model(
249 struct libnvme_ctrl *p,
250 const char *model)
251{
252 SYSFS_FREE(p->sysfs->model)do { if ((p->sysfs->model) != ((void*)0) && !((
void *)(p->sysfs->model) == (void *)NO_SYSFS_ATTR)) free
(p->sysfs->model); (p->sysfs->model) = ((void*)0)
; } while (0)
;
253 p->sysfs->model = model ? strdup(model) : NO_SYSFS_ATTR;
254}
255
256__shr_public__attribute__((visibility("default"))) int libnvme_ctrl_get_model(
257 const struct libnvme_ctrl *p,
258 const char **val,
259 const char *dflt)
260{
261 struct libnvme_ctrl *c = (struct libnvme_ctrl *)p;
262 int ret;
263
264 *val = dflt;
265
266 if (__shr_unlikely(!SYSFS_IS_LOADED(c->sysfs->model))__builtin_expect(!!(!((c->sysfs->model) != ((void*)0)))
, 0)
) {
267 ret = libnvme_ctrl_load_identity(c);
268 if (ret)
269 return ret;
270 if (!c->sysfs->model)
271 c->sysfs->model = NO_SYSFS_ATTR;
272 }
273
274 if (SYSFS_IS_ABSENT(c->sysfs->model)((void *)(c->sysfs->model) == (void *)NO_SYSFS_ATTR))
275 return -ENOENT2;
276
277 *val = c->sysfs->model;
278 return 0;
279}
280
281__shr_public__attribute__((visibility("default"))) void libnvme_ctrl_set_serial(
282 struct libnvme_ctrl *p,
283 const char *serial)
284{
285 SYSFS_FREE(p->sysfs->serial)do { if ((p->sysfs->serial) != ((void*)0) && !(
(void *)(p->sysfs->serial) == (void *)NO_SYSFS_ATTR)) free
(p->sysfs->serial); (p->sysfs->serial) = ((void*)
0); } while (0)
;
286 p->sysfs->serial = serial ? strdup(serial) : NO_SYSFS_ATTR;
287}
288
289__shr_public__attribute__((visibility("default"))) int libnvme_ctrl_get_serial(
290 const struct libnvme_ctrl *p,
291 const char **val,
292 const char *dflt)
293{
294 struct libnvme_ctrl *c = (struct libnvme_ctrl *)p;
295 int ret;
296
297 *val = dflt;
298
299 if (__shr_unlikely(!SYSFS_IS_LOADED(c->sysfs->serial))__builtin_expect(!!(!((c->sysfs->serial) != ((void*)0))
), 0)
) {
300 ret = libnvme_ctrl_load_identity(c);
301 if (ret)
302 return ret;
303 if (!c->sysfs->serial)
304 c->sysfs->serial = NO_SYSFS_ATTR;
305 }
306
307 if (SYSFS_IS_ABSENT(c->sysfs->serial)((void *)(c->sysfs->serial) == (void *)NO_SYSFS_ATTR))
308 return -ENOENT2;
309
310 *val = c->sysfs->serial;
311 return 0;
312}
313
314__shr_public__attribute__((visibility("default"))) void libnvme_ctrl_set_cntrltype(
315 struct libnvme_ctrl *p,
316 const char *cntrltype)
317{
318 SYSFS_FREE(p->sysfs->cntrltype)do { if ((p->sysfs->cntrltype) != ((void*)0) &&
!((void *)(p->sysfs->cntrltype) == (void *)NO_SYSFS_ATTR
)) free(p->sysfs->cntrltype); (p->sysfs->cntrltype
) = ((void*)0); } while (0)
;
319 p->sysfs->cntrltype = cntrltype ? strdup(cntrltype) : NO_SYSFS_ATTR;
320}
321
322__shr_public__attribute__((visibility("default"))) int libnvme_ctrl_get_cntrltype(
323 const struct libnvme_ctrl *p,
324 const char **val,
325 const char *dflt)
326{
327 struct libnvme_ctrl *c = (struct libnvme_ctrl *)p;
328 int ret;
329
330 *val = dflt;
331
332 if (__shr_unlikely(!SYSFS_IS_LOADED(c->sysfs->cntrltype))__builtin_expect(!!(!((c->sysfs->cntrltype) != ((void*)
0))), 0)
) {
333 ret = libnvme_ctrl_load_identity(c);
334 if (ret)
335 return ret;
336 if (!c->sysfs->cntrltype)
337 c->sysfs->cntrltype = NO_SYSFS_ATTR;
338 }
339
340 if (SYSFS_IS_ABSENT(c->sysfs->cntrltype)((void *)(c->sysfs->cntrltype) == (void *)NO_SYSFS_ATTR
)
)
341 return -ENOENT2;
342
343 *val = c->sysfs->cntrltype;
344 return 0;
345}
346
347__shr_public__attribute__((visibility("default"))) void libnvme_ctrl_set_cntlid(
348 struct libnvme_ctrl *p,
349 const char *cntlid)
350{
351 SYSFS_FREE(p->sysfs->cntlid)do { if ((p->sysfs->cntlid) != ((void*)0) && !(
(void *)(p->sysfs->cntlid) == (void *)NO_SYSFS_ATTR)) free
(p->sysfs->cntlid); (p->sysfs->cntlid) = ((void*)
0); } while (0)
;
352 p->sysfs->cntlid = cntlid ? strdup(cntlid) : NO_SYSFS_ATTR;
353}
354
355__shr_public__attribute__((visibility("default"))) int libnvme_ctrl_get_cntlid(
356 const struct libnvme_ctrl *p,
357 const char **val,
358 const char *dflt)
359{
360 struct libnvme_ctrl *c = (struct libnvme_ctrl *)p;
361 int ret;
362
363 *val = dflt;
364
365 if (__shr_unlikely(!SYSFS_IS_LOADED(c->sysfs->cntlid))__builtin_expect(!!(!((c->sysfs->cntlid) != ((void*)0))
), 0)
) {
366 ret = libnvme_ctrl_load_identity(c);
367 if (ret)
368 return ret;
369 if (!c->sysfs->cntlid)
370 c->sysfs->cntlid = NO_SYSFS_ATTR;
371 }
372
373 if (SYSFS_IS_ABSENT(c->sysfs->cntlid)((void *)(c->sysfs->cntlid) == (void *)NO_SYSFS_ATTR))
374 return -ENOENT2;
375
376 *val = c->sysfs->cntlid;
377 return 0;
378}
379
380__shr_public__attribute__((visibility("default"))) void libnvme_ctrl_set_dctype(
381 struct libnvme_ctrl *p,
382 const char *dctype)
383{
384 SYSFS_FREE(p->sysfs->dctype)do { if ((p->sysfs->dctype) != ((void*)0) && !(
(void *)(p->sysfs->dctype) == (void *)NO_SYSFS_ATTR)) free
(p->sysfs->dctype); (p->sysfs->dctype) = ((void*)
0); } while (0)
;
385 p->sysfs->dctype = dctype ? strdup(dctype) : NO_SYSFS_ATTR;
386}
387
388__shr_public__attribute__((visibility("default"))) int libnvme_ctrl_get_dctype(
389 const struct libnvme_ctrl *p,
390 const char **val,
391 const char *dflt)
392{
393 struct libnvme_ctrl *c = (struct libnvme_ctrl *)p;
394 int ret;
395
396 *val = dflt;
397
398 if (__shr_unlikely(!SYSFS_IS_LOADED(c->sysfs->dctype))__builtin_expect(!!(!((c->sysfs->dctype) != ((void*)0))
), 0)
) {
399 ret = libnvme_ctrl_load_identity(c);
400 if (ret)
401 return ret;
402 if (!c->sysfs->dctype)
403 c->sysfs->dctype = NO_SYSFS_ATTR;
404 }
405
406 if (SYSFS_IS_ABSENT(c->sysfs->dctype)((void *)(c->sysfs->dctype) == (void *)NO_SYSFS_ATTR))
407 return -ENOENT2;
408
409 *val = c->sysfs->dctype;
410 return 0;
411}
412
413__shr_public__attribute__((visibility("default"))) int libnvme_ctrl_get_phy_slot(
414 const struct libnvme_ctrl *p,
415 const char **val,
416 const char *dflt)
417{
418 struct libnvme_ctrl *c = (struct libnvme_ctrl *)p;
419 int ret;
420
421 *val = dflt;
422
423 if (__shr_unlikely(!SYSFS_IS_LOADED(c->sysfs->phy_slot))__builtin_expect(!!(!((c->sysfs->phy_slot) != ((void*)0
))), 0)
) {
1
Assuming field 'phy_slot' is equal to null
2
Taking true branch
424 ret = libnvme_ctrl_load_phy_slot(c);
3
Calling 'libnvme_ctrl_load_phy_slot'
425 if (ret)
426 return ret;
427 if (!c->sysfs->phy_slot)
428 c->sysfs->phy_slot = NO_SYSFS_ATTR;
429 }
430
431 if (SYSFS_IS_ABSENT(c->sysfs->phy_slot)((void *)(c->sysfs->phy_slot) == (void *)NO_SYSFS_ATTR))
432 return -ENOENT2;
433
434 *val = c->sysfs->phy_slot;
435 return 0;
436}
437
438__shr_public__attribute__((visibility("default"))) void libnvme_ctrl_set_dhchap_host_key(
439 struct libnvme_ctrl *p,
440 const char *dhchap_host_key)
441{
442 SYSFS_FREE(p->sysfs->dhchap_host_key)do { if ((p->sysfs->dhchap_host_key) != ((void*)0) &&
!((void *)(p->sysfs->dhchap_host_key) == (void *)NO_SYSFS_ATTR
)) free(p->sysfs->dhchap_host_key); (p->sysfs->dhchap_host_key
) = ((void*)0); } while (0)
;
443 p->sysfs->dhchap_host_key =
444 dhchap_host_key ? strdup(dhchap_host_key) : NO_SYSFS_ATTR;
445}
446
447__shr_public__attribute__((visibility("default"))) int libnvme_ctrl_get_dhchap_host_key(
448 const struct libnvme_ctrl *p,
449 const char **val,
450 const char *dflt)
451{
452 struct libnvme_ctrl *c = (struct libnvme_ctrl *)p;
453 int ret;
454
455 *val = dflt;
456
457 if (__shr_unlikely(!SYSFS_IS_LOADED(c->sysfs->dhchap_host_key))__builtin_expect(!!(!((c->sysfs->dhchap_host_key) != ((
void*)0))), 0)
) {
458 ret = libnvmf_ctrl_load_fabrics_attrs(c);
459 if (ret)
460 return ret;
461 if (!c->sysfs->dhchap_host_key)
462 c->sysfs->dhchap_host_key = NO_SYSFS_ATTR;
463 }
464
465 if (SYSFS_IS_ABSENT(c->sysfs->dhchap_host_key)((void *)(c->sysfs->dhchap_host_key) == (void *)NO_SYSFS_ATTR
)
)
466 return -ENOENT2;
467
468 *val = c->sysfs->dhchap_host_key;
469 return 0;
470}
471
472__shr_public__attribute__((visibility("default"))) void libnvme_ctrl_set_dhchap_ctrl_key(
473 struct libnvme_ctrl *p,
474 const char *dhchap_ctrl_key)
475{
476 SYSFS_FREE(p->sysfs->dhchap_ctrl_key)do { if ((p->sysfs->dhchap_ctrl_key) != ((void*)0) &&
!((void *)(p->sysfs->dhchap_ctrl_key) == (void *)NO_SYSFS_ATTR
)) free(p->sysfs->dhchap_ctrl_key); (p->sysfs->dhchap_ctrl_key
) = ((void*)0); } while (0)
;
477 p->sysfs->dhchap_ctrl_key =
478 dhchap_ctrl_key ? strdup(dhchap_ctrl_key) : NO_SYSFS_ATTR;
479}
480
481__shr_public__attribute__((visibility("default"))) int libnvme_ctrl_get_dhchap_ctrl_key(
482 const struct libnvme_ctrl *p,
483 const char **val,
484 const char *dflt)
485{
486 struct libnvme_ctrl *c = (struct libnvme_ctrl *)p;
487 int ret;
488
489 *val = dflt;
490
491 if (__shr_unlikely(!SYSFS_IS_LOADED(c->sysfs->dhchap_ctrl_key))__builtin_expect(!!(!((c->sysfs->dhchap_ctrl_key) != ((
void*)0))), 0)
) {
492 ret = libnvmf_ctrl_load_fabrics_attrs(c);
493 if (ret)
494 return ret;
495 if (!c->sysfs->dhchap_ctrl_key)
496 c->sysfs->dhchap_ctrl_key = NO_SYSFS_ATTR;
497 }
498
499 if (SYSFS_IS_ABSENT(c->sysfs->dhchap_ctrl_key)((void *)(c->sysfs->dhchap_ctrl_key) == (void *)NO_SYSFS_ATTR
)
)
500 return -ENOENT2;
501
502 *val = c->sysfs->dhchap_ctrl_key;
503 return 0;
504}
505
506__shr_public__attribute__((visibility("default"))) void libnvme_ctrl_set_keyring(
507 struct libnvme_ctrl *p,
508 const char *keyring)
509{
510 SYSFS_FREE(p->sysfs->keyring)do { if ((p->sysfs->keyring) != ((void*)0) && !
((void *)(p->sysfs->keyring) == (void *)NO_SYSFS_ATTR))
free(p->sysfs->keyring); (p->sysfs->keyring) = (
(void*)0); } while (0)
;
511 p->sysfs->keyring = keyring ? strdup(keyring) : NO_SYSFS_ATTR;
512}
513
514__shr_public__attribute__((visibility("default"))) int libnvme_ctrl_get_keyring(
515 const struct libnvme_ctrl *p,
516 const char **val,
517 const char *dflt)
518{
519 struct libnvme_ctrl *c = (struct libnvme_ctrl *)p;
520 int ret;
521
522 *val = dflt;
523
524 if (__shr_unlikely(!SYSFS_IS_LOADED(c->sysfs->keyring))__builtin_expect(!!(!((c->sysfs->keyring) != ((void*)0)
)), 0)
) {
525 ret = libnvmf_ctrl_load_fabrics_attrs(c);
526 if (ret)
527 return ret;
528 if (!c->sysfs->keyring)
529 c->sysfs->keyring = NO_SYSFS_ATTR;
530 }
531
532 if (SYSFS_IS_ABSENT(c->sysfs->keyring)((void *)(c->sysfs->keyring) == (void *)NO_SYSFS_ATTR))
533 return -ENOENT2;
534
535 *val = c->sysfs->keyring;
536 return 0;
537}
538

../libnvme/src/nvme/ctrl-sysfs-custom-linux.c

1// SPDX-License-Identifier: LGPL-2.1-or-later
2/*
3 * This file is part of libnvme.
4 * Copyright (c) 2026, Dell Technologies Inc. or its subsidiaries.
5 *
6 * Authors: Martin Belanger <Martin.Belanger@dell.com>
7 */
8
9#include <dirent.h>
10#include <errno(*__errno_location ()).h>
11
12#include "cleanup-linux.h"
13#include "lib.h"
14
15#include "ctrl-sysfs.c"
16
17#ifdef CONFIG_FABRICS
18# include "ctrl-sysfs-custom-fabrics.c"
19#else
20# include "ctrl-sysfs-custom-no-fabrics.c"
21#endif
22
23int libnvme_ctrl_load_identity(struct libnvme_ctrl *c)
24{
25 c->sysfs->firmware = libnvme_get_ctrl_attr(c, "firmware_rev");
26 c->sysfs->model = libnvme_get_ctrl_attr(c, "model");
27 c->sysfs->serial = libnvme_get_ctrl_attr(c, "serial");
28 c->sysfs->cntrltype = libnvme_get_ctrl_attr(c, "cntrltype");
29 c->sysfs->cntlid = libnvme_get_ctrl_attr(c, "cntlid");
30 c->sysfs->dctype = libnvme_get_ctrl_attr(c, "dctype");
31
32 return 0;
33}
34
35int libnvme_ctrl_load_phy_slot(struct libnvme_ctrl *c)
36{
37 const char *slots_sysfs_dir = libnvme_slots_sysfs_dir(c->ctx);
38 __cleanup_free__attribute__((cleanup(shr_freep))) char *target_addr = NULL((void*)0);
39 __cleanup_dir__attribute__((cleanup(cleanup_dir))) DIR *slots_dir = NULL((void*)0);
40 struct dirent *entry;
41 int ret;
42
43 if (!c->address)
4
Assuming field 'address' is non-null
5
Taking false branch
44 return 0;
45
46 slots_dir = opendir(slots_sysfs_dir);
47 if (!slots_dir
5.1
'slots_dir' is non-null
5.1
'slots_dir' is non-null
) {
6
Taking false branch
48 ret = -errno(*__errno_location ());
49 libnvme_msg(c->ctx, LIBNVME_LOG_WARN,__libnvme_msg(c->ctx, LIBNVME_LOG_WARN, ((void*)0), "failed to open slots dir %s\n"
, slots_sysfs_dir)
50 "failed to open slots dir %s\n", slots_sysfs_dir)__libnvme_msg(c->ctx, LIBNVME_LOG_WARN, ((void*)0), "failed to open slots dir %s\n"
, slots_sysfs_dir)
;
51 return ret;
52 }
53
54 target_addr = strndup(c->address, 10);
7
Memory is allocated
55 while ((entry = readdir(slots_dir))) {
8
Loop condition is false. Execution jumps to the end of the function
56 __cleanup_free__attribute__((cleanup(shr_freep))) char *path = NULL((void*)0);
57 __cleanup_free__attribute__((cleanup(shr_freep))) char *addr = NULL((void*)0);
58
59 if (entry->d_type != DT_DIRDT_DIR ||
60 !strncmp(entry->d_name, ".", 1) ||
61 !strncmp(entry->d_name, "..", 2))
62 continue;
63
64 ret = asprintf(&path, "%s/%s", slots_sysfs_dir, entry->d_name);
65 if (ret < 0)
66 return -ENOMEM12;
67
68 /* some directories don't have an address entry */
69 addr = libnvme_get_attr(path, "address");
70 if (!addr)
71 continue;
72 if (strcmp(addr, target_addr))
73 continue;
74
75 c->sysfs->phy_slot = strdup(entry->d_name);
76 return 0;
77 }
78
79 return 0;
9
Potential leak of memory pointed to by 'target_addr'
80}