wlantest: Properly free allocated memory on error exit paths

In the cases when a failure is experienced, the value "-1" was returned
from the main() function without doing any cleanup or deinit.

For example, if wlantest was started with the following set of command
line arguments then later when returning after a failure from main()
function, the memory allocated as part of handling the "-p" getopt
command line option was not freed. To fix memory leaks in this case,
properly free the previously allocated memory with the help of
wlantest_deinit() before returning from main().

$ sudo valgrind --leak-check=full --show-leak-kinds=all --verbose \
> --track-origins=yes --log-file=valgrind-out.txt \
> ./wlantest -i hwsim0 -dd -c -p "asdfasdfasdfasdf" -W "abcd"
Invalid WEP key 'abcd'

Memory leak reported by Valgrind when running wlantest as mentioned above.

==513454== HEAP SUMMARY:
==513454==     in use at exit: 128 bytes in 1 blocks
==513454==   total heap usage: 4 allocs, 3 frees, 5,720 bytes allocated
==513454==
==513454== Searching for pointers to 1 not-freed blocks
==513454== Checked 76,936 bytes
==513454==
==513454== 128 bytes in 1 blocks are definitely lost in loss record 1 of 1
==513454==    at 0x483DD99: calloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==513454==    by 0x1396CA: os_zalloc (in /home/ubuntu/hostap/wlantest/wlantest)
==513454==    by 0x10C345: add_passphrase (wlantest.c:125)
==513454==    by 0x10C345: main (wlantest.c:425)
==513454==
==513454== LEAK SUMMARY:
==513454==    definitely lost: 128 bytes in 1 blocks
==513454==    indirectly lost: 0 bytes in 0 blocks
==513454==      possibly lost: 0 bytes in 0 blocks
==513454==    still reachable: 0 bytes in 0 blocks
==513454==         suppressed: 0 bytes in 0 blocks
==513454==
==513454== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)

Signed-off-by: Gokul Sivakumar <gokulkumar792@gmail.com>
This commit is contained in:
Gokul Sivakumar 2021-11-03 22:20:20 +05:30 committed by Jouni Malinen
parent 97607de5e6
commit 30cf0d107f

View file

@ -364,7 +364,7 @@ int wlantest_relog(struct wlantest *wt)
int main(int argc, char *argv[])
{
int c;
int c, ret = 0;
const char *read_file = NULL;
const char *read_wired_file = NULL;
const char *ifname = NULL;
@ -372,6 +372,7 @@ int main(int argc, char *argv[])
const char *logfile = NULL;
struct wlantest wt;
int ctrl_iface = 0;
bool eloop_init_done = false;
wpa_debug_level = MSG_INFO;
wpa_debug_show_keys = 1;
@ -397,15 +398,18 @@ int main(int argc, char *argv[])
wt.ethernet = 1;
break;
case 'f':
if (add_pmk_file(&wt, optarg) < 0)
return -1;
if (add_pmk_file(&wt, optarg) < 0) {
ret = -1;
goto deinit;
}
break;
case 'F':
wt.assume_fcs = 1;
break;
case 'h':
usage();
return 0;
ret = 0;
goto deinit;
case 'i':
ifname = optarg;
break;
@ -440,54 +444,54 @@ int main(int argc, char *argv[])
wpa_debug_timestamp = 1;
break;
case 'T':
if (add_ptk_file(&wt, optarg) < 0)
return -1;
if (add_ptk_file(&wt, optarg) < 0) {
ret = -1;
goto deinit;
}
break;
case 'w':
wt.write_file = optarg;
break;
case 'W':
if (add_wep(&wt, optarg) < 0)
return -1;
if (add_wep(&wt, optarg) < 0) {
ret = -1;
goto deinit;
}
break;
default:
usage();
return -1;
ret = -1;
goto deinit;
}
}
if (ifname == NULL && ifname_wired == NULL &&
read_file == NULL && read_wired_file == NULL) {
usage();
return 0;
ret = 0;
goto deinit;
}
if (eloop_init())
return -1;
if (eloop_init()) {
ret = -1;
goto deinit;
}
eloop_init_done = true;
if (logfile)
wpa_debug_open_file(logfile);
if (wt.write_file && write_pcap_init(&wt, wt.write_file) < 0)
return -1;
if (wt.pcapng_file && write_pcapng_init(&wt, wt.pcapng_file) < 0)
return -1;
if (read_wired_file && read_wired_cap_file(&wt, read_wired_file) < 0)
return -1;
if (read_file && read_cap_file(&wt, read_file) < 0)
return -1;
if (ifname && monitor_init(&wt, ifname) < 0)
return -1;
if (ifname_wired && monitor_init_wired(&wt, ifname_wired) < 0)
return -1;
if (ctrl_iface && ctrl_init(&wt) < 0)
return -1;
if ((wt.write_file && write_pcap_init(&wt, wt.write_file) < 0) ||
(wt.pcapng_file && write_pcapng_init(&wt, wt.pcapng_file) < 0) ||
(read_wired_file &&
read_wired_cap_file(&wt, read_wired_file) < 0) ||
(read_file && read_cap_file(&wt, read_file) < 0) ||
(ifname && monitor_init(&wt, ifname) < 0) ||
(ifname_wired && monitor_init_wired(&wt, ifname_wired) < 0) ||
(ctrl_iface && ctrl_init(&wt) < 0)) {
ret = -1;
goto deinit;
}
eloop_register_signal_terminate(wlantest_terminate, &wt);
@ -497,11 +501,13 @@ int main(int argc, char *argv[])
"fcs_error=%u",
wt.rx_mgmt, wt.rx_ctrl, wt.rx_data, wt.fcs_error);
deinit:
wlantest_deinit(&wt);
wpa_debug_close_file();
eloop_destroy();
if (eloop_init_done)
eloop_destroy();
os_program_deinit();
return 0;
return ret;
}