Bug Summary

File:.build-ci/../shared/fs-util.c
Warning:line 104, column 7
Potential leak of memory pointed to by 'file_path'

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 fs-util.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 shared/libshared.a.p -I shared -I ../shared -I ccan -I ../ccan -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 -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 ../shared/fs-util.c
1// SPDX-License-Identifier: LGPL-2.1-or-later
2/*
3 * This file is part of nvme-cli.
4 * Copyright (c) 2026 Dell Technologies Inc. or its subsidiaries.
5 *
6 * Authors: Martin Belanger <martin.belanger@dell.com>
7 */
8
9#include <errno(*__errno_location ()).h>
10#include <limits.h>
11#include <stdio.h>
12#include <stdlib.h>
13#include <string.h>
14#include <sys/stat.h>
15#include <unistd.h>
16
17#include "cleanup.h"
18#include "fs-util.h"
19
20int shr_mkdir_p(const char *path, mode_t mode)
21{
22 char buf[PATH_MAX4096];
23 char *p;
24 size_t len;
25 int ret;
26
27 len = strlen(path);
28 if (len >= sizeof(buf))
29 return -ENAMETOOLONG36;
30 memcpy(buf, path, len + 1);
31 if (len && buf[len - 1] == '/')
32 buf[len - 1] = '\0';
33
34 for (p = buf + 1; *p; p++) {
35 if (*p != '/')
36 continue;
37 *p = '\0';
38 ret = shr_mkdir(buf, mode);
39 if (ret < 0 && ret != -EEXIST17)
40 return ret;
41 *p = '/';
42 }
43 ret = shr_mkdir(buf, mode);
44 return (ret == 0 || ret == -EEXIST17) ? 0 : ret;
45}
46
47int shr_mkdir_from_fname(const char *file, mode_t mode)
48{
49 __cleanup_free__attribute__((cleanup(shr_freep))) char *file_copy = NULL((void*)0);
50 char *parent;
51
52 file_copy = strdup(file);
53 if (!file_copy)
54 return -ENOMEM12;
55
56 parent = shr_dirname(file_copy);
57 return shr_mkdir_p(parent, mode);
58}
59
60char *shr_basename(const char *path)
61{
62 char *p = (char *)strrchr(path, '/')_Generic (0 ? (path) : (void *) 1, const void *: (const char *
) (strrchr (path, '/')), default: strrchr (path, '/'))
;
63
64 return p ? p + 1 : (char *)path;
65}
66
67static char *join_path(const char *dir, const char *path)
68{
69 char *out;
70 size_t len;
71
72 if (!dir)
2
Assuming 'dir' is null
3
Taking true branch
73 return strdup(path);
4
Memory is allocated
74 if (!path || !*path)
75 return strdup(dir);
76
77 len = strlen(dir) + 1 + strlen(path) + 1;
78 out = malloc(len);
79 if (!out)
80 return NULL((void*)0);
81 snprintf(out, len, "%s/%s", dir, path);
82
83 return out;
84}
85
86unsigned char *shr_read_file(const char *dir, const char *path, long *size, int retries)
87{
88 __cleanup_free__attribute__((cleanup(shr_freep))) char *file_path = NULL((void*)0);
89 unsigned char *buf = NULL((void*)0);
90 FILE *file = NULL((void*)0);
91 size_t n;
92 int i;
93
94 file_path = join_path(dir, path);
1
Calling 'join_path'
5
Returned allocated memory
95 if (!file_path)
6
Assuming 'file_path' is non-null
7
Taking false branch
96 return NULL((void*)0);
97
98 for (i = 0; i < retries; i++) {
8
Assuming 'i' is >= 'retries'
9
Loop condition is false. Execution jumps to the end of the function
99 file = fopen(file_path, "rb");
100 if (file)
101 break;
102 sleep((unsigned int)(retries > 1));
103 }
104 if (!file)
10
Potential leak of memory pointed to by 'file_path'
105 return NULL((void*)0);
106
107 fseek(file, 0, SEEK_END2);
108 *size = ftell(file);
109 if (*size <= 0) {
110 fclose(file);
111 return NULL((void*)0);
112 }
113 fseek(file, 0, SEEK_SET0);
114
115 buf = malloc(*size);
116 if (!buf) {
117 fclose(file);
118 return NULL((void*)0);
119 }
120
121 n = fread(buf, 1, *size, file);
122 fclose(file);
123
124 if (n != (size_t)*size) {
125 free(buf);
126 return NULL((void*)0);
127 }
128
129 return buf;
130}