Skip to content

Commit 298ce48

Browse files
rdmarkandylemin
authored andcommitted
getzones: protect against potential memory leak when exiting in optarg
Depending on the optarg input string, the loop may exit after the requested_zone variable has been dynamically allocated, so let's free the memory every time
1 parent c9b2f6e commit 298ce48

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

bin/getzones/getzones.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,11 @@ int main(int argc, char *argv[])
8686

8787
if ((charset_t) -1 == chMac) {
8888
fprintf(stderr, "Invalid Mac charset.\n");
89+
90+
if (requested_zone != NULL) {
91+
free(requested_zone);
92+
}
93+
8994
exit(1);
9095
}
9196

@@ -100,11 +105,21 @@ int main(int argc, char *argv[])
100105

101106
if (*end_of_digits != '\0') {
102107
fprintf(stderr, "Network must be a number between 0 and 65535.\n");
108+
109+
if (requested_zone != NULL) {
110+
free(requested_zone);
111+
}
112+
103113
exit(1);
104114
}
105115

106116
if (requested_network < 0 || requested_network > 65535) {
107117
fprintf(stderr, "Network must be a number between 0 and 65535.\n");
118+
119+
if (requested_zone != NULL) {
120+
free(requested_zone);
121+
}
122+
108123
exit(1);
109124
}
110125

0 commit comments

Comments
 (0)