From 0a5315e1931baecd6d119d370a4d3001f1b1abec Mon Sep 17 00:00:00 2001 From: Teemu Toivola Date: Mon, 7 Dec 2015 23:36:36 +0200 Subject: [PATCH] don't list interfaces having names longer than what can be handled, add tests with interface names longer that what the kernel supports --- src/ifinfo.c | 67 ++++++++++++++++++++++---------------------- tests/ifinfo_tests.c | 46 ++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+), 33 deletions(-) diff --git a/src/ifinfo.c b/src/ifinfo.c index 70003f8..9904a5d 100644 --- a/src/ifinfo.c +++ b/src/ifinfo.c @@ -102,31 +102,31 @@ int getiflist(char **ifacelist, int showspeed) /* make list of interfaces */ while ((di=readdir(dp))) { - if (di->d_name[0]!='.') { - *ifacelist = realloc(*ifacelist, ( ( strlen(*ifacelist) + strlen(di->d_name) + 2 ) * sizeof(char)) ); + if (di->d_name[0] == '.' || strlen(di->d_name) > 31) { + continue; + } + *ifacelist = realloc(*ifacelist, ( ( strlen(*ifacelist) + strlen(di->d_name) + 2 ) * sizeof(char)) ); + if (*ifacelist == NULL) { + panicexit(__FILE__, __LINE__); + } + strncat(*ifacelist, di->d_name, strlen(di->d_name)); + strcat(*ifacelist, " "); + if (!showspeed) { + continue; + } + speed = getifspeed(di->d_name); + if (speed > 0) { + snprintf(temp, 64, "(%u Mbit) ", speed); + *ifacelist = realloc(*ifacelist, ( ( strlen(*ifacelist) + strlen(temp) + 1 ) * sizeof(char)) ); if (*ifacelist == NULL) { panicexit(__FILE__, __LINE__); } - strncat(*ifacelist, di->d_name, strlen(di->d_name)); - strcat(*ifacelist, " "); - if (!showspeed) { - continue; - } - speed = getifspeed(di->d_name); - if (speed > 0) { - snprintf(temp, 64, "(%u Mbit) ", speed); - *ifacelist = realloc(*ifacelist, ( ( strlen(*ifacelist) + strlen(temp) + 1 ) * sizeof(char)) ); - if (*ifacelist == NULL) { - panicexit(__FILE__, __LINE__); - } - strncat(*ifacelist, temp, strlen(temp)); - } + strncat(*ifacelist, temp, strlen(temp)); } } closedir(dp); return 1; - } } @@ -135,25 +135,26 @@ int getiflist(char **ifacelist, int showspeed) /* make list of interfaces */ for (ifa = ifap; ifa; ifa = ifa->ifa_next) { - if (ifa->ifa_addr->sa_family == AF_LINK) { - *ifacelist = realloc(*ifacelist, ( ( strlen(*ifacelist) + strlen(ifa->ifa_name) + 2 ) * sizeof(char)) ); + if (ifa->ifa_addr->sa_family != AF_LINK || strlen(ifa->ifa_name) > 31) { + continue; + } + *ifacelist = realloc(*ifacelist, ( ( strlen(*ifacelist) + strlen(ifa->ifa_name) + 2 ) * sizeof(char)) ); + if (*ifacelist == NULL) { + panicexit(__FILE__, __LINE__); + } + strncat(*ifacelist, ifa->ifa_name, strlen(ifa->ifa_name)); + strcat(*ifacelist, " "); + if (!showspeed) { + continue; + } + speed = getifspeed(ifa->ifa_name); + if (speed > 0) { + snprintf(temp, 64, "(%u Mbit) ", speed); + *ifacelist = realloc(*ifacelist, ( ( strlen(*ifacelist) + strlen(temp) + 1 ) * sizeof(char)) ); if (*ifacelist == NULL) { panicexit(__FILE__, __LINE__); } - strncat(*ifacelist, ifa->ifa_name, strlen(ifa->ifa_name)); - strcat(*ifacelist, " "); - if (!showspeed) { - continue; - } - speed = getifspeed(ifa->ifa_name); - if (speed > 0) { - snprintf(temp, 64, "(%u Mbit) ", speed); - *ifacelist = realloc(*ifacelist, ( ( strlen(*ifacelist) + strlen(temp) + 1 ) * sizeof(char)) ); - if (*ifacelist == NULL) { - panicexit(__FILE__, __LINE__); - } - strncat(*ifacelist, temp, strlen(temp)); - } + strncat(*ifacelist, temp, strlen(temp)); } } diff --git a/tests/ifinfo_tests.c b/tests/ifinfo_tests.c index 31b4c83..a5a75a1 100644 --- a/tests/ifinfo_tests.c +++ b/tests/ifinfo_tests.c @@ -422,6 +422,28 @@ START_TEST(getiflist_proc_multiple_interfaces) } END_TEST +START_TEST(getiflist_proc_long_interface_names) +{ + char *ifacelist; + + linuxonly; + + ck_assert_int_eq(remove_directory(TESTDIR), 1); + fake_proc_net_dev("w", "random", 0, 0, 0, 0); + fake_proc_net_dev("a", "interfaces", 0, 0, 0, 0); + fake_proc_net_dev("a", "having", 0, 0, 0, 0); + fake_proc_net_dev("a", "toomuchfun", 0, 0, 0, 0); + fake_proc_net_dev("a", "longinterfaceislong", 0, 0, 0, 0); + fake_proc_net_dev("a", "longestinterfaceislongerthanshouldbeexpectedanywhereinanormallyfunctioningenvironment", 0, 0, 0, 0); + fake_proc_net_dev("a", "a", 0, 0, 0, 0); + + ck_assert_int_eq(getiflist(&ifacelist, 0), 1); + ck_assert_str_eq(ifacelist, "lo random interfaces having toomuchfun longinterfaceislong a "); + + free(ifacelist); +} +END_TEST + START_TEST(getiflist_sysclassnet_one_interface) { char *ifacelist; @@ -474,6 +496,28 @@ START_TEST(getiflist_sysclassnet_multiple_interfaces) } END_TEST +START_TEST(getiflist_sysclassnet_long_interface_names) +{ + char *ifacelist; + + linuxonly; + + ck_assert_int_eq(remove_directory(TESTDIR), 1); + fake_sys_class_net("random", 0, 0, 0, 0, 0); + fake_sys_class_net("interfaces", 0, 0, 0, 0, 0); + fake_sys_class_net("having", 0, 0, 0, 0, 0); + fake_sys_class_net("toomuchfun", 0, 0, 0, 0, 0); + fake_sys_class_net("longinterfaceislong", 0, 0, 0, 0, 0); + fake_sys_class_net("longestinterfaceislongerthanshouldbeexpectedanywhereinanormallyfunctioningenvironment", 0, 0, 0, 0, 0); + fake_sys_class_net("a", 0, 0, 0, 0, 0); + + ck_assert_int_eq(getiflist(&ifacelist, 0), 1); + ck_assert_int_eq(strlen(ifacelist), 58); + + free(ifacelist); +} +END_TEST + START_TEST(readproc_no_file) { linuxonly; @@ -587,9 +631,11 @@ void add_ifinfo_tests(Suite *s) tcase_add_test(tc_ifinfo, getiflist_proc_one_interface); tcase_add_test(tc_ifinfo, getiflist_proc_one_interface_with_speed); tcase_add_test(tc_ifinfo, getiflist_proc_multiple_interfaces); + tcase_add_test(tc_ifinfo, getiflist_proc_long_interface_names); tcase_add_test(tc_ifinfo, getiflist_sysclassnet_one_interface); tcase_add_test(tc_ifinfo, getiflist_sysclassnet_one_interface_with_speed); tcase_add_test(tc_ifinfo, getiflist_sysclassnet_multiple_interfaces); + tcase_add_test(tc_ifinfo, getiflist_sysclassnet_long_interface_names); tcase_add_test(tc_ifinfo, readproc_no_file); tcase_add_test(tc_ifinfo, readproc_not_found); tcase_add_test(tc_ifinfo, readproc_success); -- 2.40.0