Bug Summary

File:build-scan/../src/network/wait-online/link.c
Warning:line 34, column 25
Potential leak of memory pointed to by 'l'

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple x86_64-unknown-linux-gnu -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name link.c -analyzer-store=region -analyzer-opt-analyze-nested-blocks -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 static -mframe-pointer=all -relaxed-aliasing -menable-no-infs -menable-no-nans -menable-unsafe-fp-math -fno-signed-zeros -mreassociate -freciprocal-math -fdenormal-fp-math=preserve-sign,preserve-sign -ffp-contract=fast -fno-rounding-math -ffast-math -ffinite-math-only -mconstructor-aliases -munwind-tables -target-cpu x86-64 -tune-cpu generic -fno-split-dwarf-inlining -debugger-tuning=gdb -resource-dir /usr/lib64/clang/12.0.0 -include config.h -I systemd-networkd-wait-online.p -I . -I .. -I src/basic -I ../src/basic -I src/shared -I ../src/shared -I src/systemd -I ../src/systemd -I src/journal -I ../src/journal -I src/journal-remote -I ../src/journal-remote -I src/nspawn -I ../src/nspawn -I src/resolve -I ../src/resolve -I src/timesync -I ../src/timesync -I ../src/time-wait-sync -I src/login -I ../src/login -I src/udev -I ../src/udev -I src/libudev -I ../src/libudev -I src/core -I ../src/core -I ../src/libsystemd/sd-bus -I ../src/libsystemd/sd-device -I ../src/libsystemd/sd-hwdb -I ../src/libsystemd/sd-id128 -I ../src/libsystemd/sd-netlink -I ../src/libsystemd/sd-network -I src/libsystemd-network -I ../src/libsystemd-network -D _FILE_OFFSET_BITS=64 -internal-isystem /usr/local/include -internal-isystem /usr/lib64/clang/12.0.0/include -internal-externc-isystem /include -internal-externc-isystem /usr/include -Wwrite-strings -Wno-unused-parameter -Wno-missing-field-initializers -Wno-unused-result -Wno-format-signedness -Wno-error=nonnull -std=gnu99 -fconst-strings -fdebug-compilation-dir /home/mrc0mmand/repos/@redhat-plumbers/systemd-rhel8/build-scan -ferror-limit 19 -fvisibility hidden -stack-protector 2 -fgnuc-version=4.2.1 -fcolor-diagnostics -analyzer-output=html -faddrsig -o /tmp/scan-build-2021-07-16-221226-1465241-1 -x c ../src/network/wait-online/link.c
1/* SPDX-License-Identifier: LGPL-2.1+ */
2
3#include "sd-network.h"
4
5#include "alloc-util.h"
6#include "hashmap.h"
7#include "link.h"
8#include "manager.h"
9#include "string-util.h"
10
11int link_new(Manager *m, Link **ret, int ifindex, const char *ifname) {
12 _cleanup_(link_freep)__attribute__((cleanup(link_freep))) Link *l = NULL((void*)0);
13 int r;
14
15 assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("m"), "../src/network/wait-online/link.c"
, 15, __PRETTY_FUNCTION__); } while (0)
;
1
Assuming 'm' is non-null
2
Taking false branch
3
Loop condition is false. Exiting loop
16 assert(ifindex > 0)do { if ((__builtin_expect(!!(!(ifindex > 0)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("ifindex > 0"), "../src/network/wait-online/link.c"
, 16, __PRETTY_FUNCTION__); } while (0)
;
4
Assuming 'ifindex' is > 0
5
Taking false branch
6
Loop condition is false. Exiting loop
17
18 r = hashmap_ensure_allocated(&m->links, NULL)internal_hashmap_ensure_allocated(&m->links, ((void*)0
) )
;
19 if (r < 0)
7
Assuming 'r' is >= 0
8
Taking false branch
20 return r;
21
22 r = hashmap_ensure_allocated(&m->links_by_name, &string_hash_ops)internal_hashmap_ensure_allocated(&m->links_by_name, &
string_hash_ops )
;
23 if (r < 0)
9
Assuming 'r' is >= 0
10
Taking false branch
24 return r;
25
26 l = new0(Link, 1)((Link*) calloc((1), sizeof(Link)));
11
Memory is allocated
27 if (!l)
12
Assuming 'l' is non-null
13
Taking false branch
28 return -ENOMEM12;
29
30 l->manager = m;
31
32 l->ifname = strdup(ifname);
33 if (!l->ifname)
14
Assuming field 'ifname' is null
15
Taking true branch
34 return -ENOMEM12;
16
Potential leak of memory pointed to by 'l'
35
36 r = hashmap_put(m->links_by_name, l->ifname, l);
37 if (r < 0)
38 return r;
39
40 l->ifindex = ifindex;
41
42 r = hashmap_put(m->links, INT_TO_PTR(ifindex)((void *) ((intptr_t) (ifindex))), l);
43 if (r < 0)
44 return r;
45
46 if (ret)
47 *ret = l;
48 l = NULL((void*)0);
49
50 return 0;
51}
52
53Link *link_free(Link *l) {
54
55 if (!l)
56 return NULL((void*)0);
57
58 if (l->manager) {
59 hashmap_remove(l->manager->links, INT_TO_PTR(l->ifindex)((void *) ((intptr_t) (l->ifindex))));
60 hashmap_remove(l->manager->links_by_name, l->ifname);
61 }
62
63 free(l->ifname);
64 return mfree(l);
65 }
66
67int link_update_rtnl(Link *l, sd_netlink_message *m) {
68 const char *ifname;
69 int r;
70
71 assert(l)do { if ((__builtin_expect(!!(!(l)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("l"), "../src/network/wait-online/link.c"
, 71, __PRETTY_FUNCTION__); } while (0)
;
72 assert(l->manager)do { if ((__builtin_expect(!!(!(l->manager)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("l->manager"), "../src/network/wait-online/link.c"
, 72, __PRETTY_FUNCTION__); } while (0)
;
73 assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("m"), "../src/network/wait-online/link.c"
, 73, __PRETTY_FUNCTION__); } while (0)
;
74
75 r = sd_rtnl_message_link_get_flags(m, &l->flags);
76 if (r < 0)
77 return r;
78
79 r = sd_netlink_message_read_string(m, IFLA_IFNAME, &ifname);
80 if (r < 0)
81 return r;
82
83 if (!streq(l->ifname, ifname)(strcmp((l->ifname),(ifname)) == 0)) {
84 char *new_ifname;
85
86 new_ifname = strdup(ifname);
87 if (!new_ifname)
88 return -ENOMEM12;
89
90 hashmap_remove(l->manager->links_by_name, l->ifname);
91 free(l->ifname);
92 l->ifname = new_ifname;
93
94 r = hashmap_put(l->manager->links_by_name, l->ifname, l);
95 if (r < 0)
96 return r;
97 }
98
99 return 0;
100}
101
102int link_update_monitor(Link *l) {
103 assert(l)do { if ((__builtin_expect(!!(!(l)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("l"), "../src/network/wait-online/link.c"
, 103, __PRETTY_FUNCTION__); } while (0)
;
104
105 l->required_for_online = sd_network_link_get_required_for_online(l->ifindex) != 0;
106
107 l->operational_state = mfree(l->operational_state);
108
109 sd_network_link_get_operational_state(l->ifindex, &l->operational_state);
110
111 l->state = mfree(l->state);
112
113 sd_network_link_get_setup_state(l->ifindex, &l->state);
114
115 return 0;
116}