Bug Summary

File:.build-ci/../shared/machine-id-util-linux.c
Warning:line 46, column 6
Opened stream never closed. Potential resource leak

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 machine-id-util-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 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=gnu11 -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-09-23-073103-589-1 -x c ../shared/machine-id-util-linux.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 <stdio.h>
11#include <string.h>
12
13#include "cleanup-util.h"
14#include "machine-id-util.h"
15#include "sha256-util.h"
16
17#define MACHINE_ID_LEN_STRING32 32
18
19static int parse_hex_digit(char c)
20{
21 if (c >= '0' && c <= '9')
22 return c - '0';
23 if (c >= 'a' && c <= 'f')
24 return c - 'a' + 10;
25 if (c >= 'A' && c <= 'F')
26 return c - 'A' + 10;
27
28 return -EINVAL22;
29}
30
31static int read_machine_id(const char *path, unsigned char id[SHR_UUID_LEN16])
32{
33 char buf[MACHINE_ID_LEN_STRING32 + 2] = {};
34 __cleanup_file__attribute__((cleanup(shr_cleanup_file))) FILE *f = NULL((void*)0);
35 bool_Bool all_zero = true1;
36 size_t len, i;
37
38 f = fopen(path, "re");
6
Stream opened here
7
Assuming that 'fopen' is successful
39 if (!f
7.1
'f' is non-null
)
8
Taking false branch
40 return -errno(*__errno_location ());
41
42 /*
43 * An empty file, and the "uninitialized" marker systemd writes during
44 * the first boot of an image, are both too short to parse.
45 */
46 len = fread(buf, 1, sizeof(buf) - 1, f);
9
Opened stream never closed. Potential resource leak
47 if (len < MACHINE_ID_LEN_STRING32)
48 return -EINVAL22;
49
50 for (i = 0; i < SHR_UUID_LEN16; i++) {
51 int hi = parse_hex_digit(buf[i * 2]);
52 int lo = parse_hex_digit(buf[i * 2 + 1]);
53
54 if (hi < 0 || lo < 0)
55 return -EINVAL22;
56
57 id[i] = (hi << 4) | lo;
58 if (id[i])
59 all_zero = false0;
60 }
61
62 return all_zero ? -EINVAL22 : 0;
63}
64
65int shr_machine_id_app_specific(const char *path,
66 const unsigned char app_id[SHR_UUID_LEN16],
67 unsigned char out[SHR_UUID_LEN16])
68{
69 unsigned char hmac[SHR_SHA256_DIGEST_SIZE32];
70 unsigned char id[SHR_UUID_LEN16];
71 int ret;
72
73 if (!path || !app_id || !out)
1
Assuming 'path' is non-null
2
Assuming 'app_id' is non-null
3
Assuming 'out' is non-null
4
Taking false branch
74 return -EINVAL22;
75
76 ret = read_machine_id(path, id);
5
Calling 'read_machine_id'
77 if (ret)
78 return ret;
79
80 shr_hmac_sha256_raw(id, sizeof(id), app_id, SHR_UUID_LEN16, hmac);
81
82 /* Keep the first half only. */
83 memcpy(out, hmac, SHR_UUID_LEN16);
84
85 /* RFC 9562: version 4, variant DCE. */
86 out[6] = (out[6] & 0x0f) | 0x40;
87 out[8] = (out[8] & 0x3f) | 0x80;
88
89 return 0;
90}