Skip to content

Commit 9dbc16e

Browse files
committed
src: avoid leak parsing command line arguemnts in "nl-{class,qdisc}-add"
"id" could be overwritten, this causes coverity warnings. While not severe, fix the coverity warnings by using _nl_clear_free(). Also use _nl_auto_free to release the memory. Error: CLANG_WARNING: [#def79] libnl-3.9.0/src/nl-class-add.c:73:7: warning[unix.Malloc]: Potential leak of memory pointed to by 'id' # 71| # 72| for (;;) { # 73|-> int c, optidx = 0; # 74| enum { # 75| ARG_UPDATE = 257, Error: GCC_ANALYZER_WARNING (CWE-401): [#def80] libnl-3.9.0/src/nl-class-add.c: scope_hint: In function ‘main’ libnl-3.9.0/src/nl-class-add.c:109:32: warning[-Wanalyzer-malloc-leak]: leak of ‘id’ # 107| case 'd': nl_cli_tc_parse_dev(tc, link_cache, optarg); break; # 108| case 'p': nl_cli_tc_parse_parent(tc, optarg); break; # 109|-> case 'i': id = strdup(optarg); break; # 110| case ARG_UPDATE: flags = NLM_F_CREATE; break; # 111| case ARG_UPDATE_ONLY: flags = 0; break;
1 parent 3d4d650 commit 9dbc16e

File tree

2 files changed

+6
-6
lines changed

2 files changed

+6
-6
lines changed

src/nl-class-add.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ int main(int argc, char *argv[])
5959
struct nl_cli_tc_module *tm;
6060
struct rtnl_tc_ops *ops;
6161
int err, flags = NLM_F_CREATE | NLM_F_EXCL;
62-
char *kind, *id = NULL;
62+
const char *kind;
63+
_nl_auto_free char *id = NULL;
6364

6465
sock = nl_cli_alloc_socket();
6566
nl_cli_connect(sock, NETLINK_ROUTE);
@@ -106,7 +107,7 @@ int main(int argc, char *argv[])
106107
case 'v': nl_cli_print_version(); break;
107108
case 'd': nl_cli_tc_parse_dev(tc, link_cache, optarg); break;
108109
case 'p': nl_cli_tc_parse_parent(tc, optarg); break;
109-
case 'i': id = strdup(optarg); break;
110+
case 'i': _nl_clear_free(&id); id = strdup(optarg); break;
110111
case ARG_UPDATE: flags = NLM_F_CREATE; break;
111112
case ARG_UPDATE_ONLY: flags = 0; break;
112113
case ARG_MTU: nl_cli_tc_parse_mtu(tc, optarg); break;
@@ -127,7 +128,6 @@ int main(int argc, char *argv[])
127128

128129
if (id) {
129130
nl_cli_tc_parse_handle(tc, id, 1);
130-
free(id);
131131
}
132132

133133
kind = argv[optind++];

src/nl-qdisc-add.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ int main(int argc, char *argv[])
5656
struct nl_cli_tc_module *tm;
5757
struct rtnl_tc_ops *ops;
5858
int err, flags = NLM_F_CREATE | NLM_F_EXCL;
59-
char *kind, *id = NULL;
59+
const char *kind;
60+
_nl_auto_free char *id = NULL;
6061

6162
sock = nl_cli_alloc_socket();
6263
nl_cli_connect(sock, NETLINK_ROUTE);
@@ -99,7 +100,7 @@ int main(int argc, char *argv[])
99100
case 'v': nl_cli_print_version(); break;
100101
case 'd': nl_cli_tc_parse_dev(tc, link_cache, optarg); break;
101102
case 'p': nl_cli_tc_parse_parent(tc, optarg); break;
102-
case 'i': id = strdup(optarg); break;
103+
case 'i': _nl_clear_free(&id); id = strdup(optarg); break;
103104
case ARG_UPDATE: flags = NLM_F_CREATE; break;
104105
case ARG_REPLACE: flags = NLM_F_CREATE | NLM_F_REPLACE; break;
105106
case ARG_UPDATE_ONLY: flags = 0; break;
@@ -118,7 +119,6 @@ int main(int argc, char *argv[])
118119

119120
if (id) {
120121
nl_cli_tc_parse_handle(tc, id, 1);
121-
free(id);
122122
}
123123

124124
kind = argv[optind++];

0 commit comments

Comments
 (0)