From 99e758652e9d5f3afd484d3d3634c86eb3cea6d7 Mon Sep 17 00:00:00 2001 From: Johannes Kauffmann Date: Tue, 23 Jan 2024 09:04:54 +0100 Subject: [PATCH] tests: fix linked list leak Don't overwrite the original pointer returned by ec_find_adapters(), otherwise the linked list is leaked. Instead, save original pointer to the head of the list use and `adapters` as a temporary variable. Pass the original pointer to ec_free_adapters(). For win32, ec_free_adapters() was missing entirely. --- test/linux/eepromtool/eepromtool.c | 6 +++--- test/linux/simple_test/simple_test.c | 6 +++--- test/linux/slaveinfo/slaveinfo.c | 6 +++--- test/simple_ng/simple_ng.c | 6 +++--- test/win32/eepromtool/eepromtool.c | 5 +++-- test/win32/simple_test/simple_test.c | 5 +++-- test/win32/slaveinfo/slaveinfo.c | 5 +++-- 7 files changed, 21 insertions(+), 18 deletions(-) diff --git a/test/linux/eepromtool/eepromtool.c b/test/linux/eepromtool/eepromtool.c index 63b3213b..49ff1139 100644 --- a/test/linux/eepromtool/eepromtool.c +++ b/test/linux/eepromtool/eepromtool.c @@ -456,7 +456,7 @@ int main(int argc, char *argv[]) } else { - ec_adaptert * adapter = NULL; + ec_adaptert * adapter, head = NULL; printf("Usage: eepromtool ifname slave OPTION fname|alias\n"); printf("ifname = eth0 for example\n"); @@ -469,13 +469,13 @@ int main(int argc, char *argv[]) printf(" -wi write EEPROM, input Intel Hex format\n"); printf ("\nAvailable adapters:\n"); - adapter = ec_find_adapters (); + head = adapter = ec_find_adapters (); while (adapter != NULL) { printf (" - %s (%s)\n", adapter->name, adapter->desc); adapter = adapter->next; } - ec_free_adapters(adapter); + ec_free_adapters(head); } printf("End program\n"); diff --git a/test/linux/simple_test/simple_test.c b/test/linux/simple_test/simple_test.c index 8b9f70d1..30fd2234 100644 --- a/test/linux/simple_test/simple_test.c +++ b/test/linux/simple_test/simple_test.c @@ -241,17 +241,17 @@ int main(int argc, char *argv[]) } else { - ec_adaptert * adapter = NULL; + ec_adaptert * adapter, head = NULL; printf("Usage: simple_test ifname1\nifname = eth0 for example\n"); printf ("\nAvailable adapters:\n"); - adapter = ec_find_adapters (); + head = adapter = ec_find_adapters (); while (adapter != NULL) { printf (" - %s (%s)\n", adapter->name, adapter->desc); adapter = adapter->next; } - ec_free_adapters(adapter); + ec_free_adapters(head); } printf("End program\n"); diff --git a/test/linux/slaveinfo/slaveinfo.c b/test/linux/slaveinfo/slaveinfo.c index 88558b18..1ba4c929 100644 --- a/test/linux/slaveinfo/slaveinfo.c +++ b/test/linux/slaveinfo/slaveinfo.c @@ -698,7 +698,7 @@ char ifbuf[1024]; int main(int argc, char *argv[]) { - ec_adaptert * adapter = NULL; + ec_adaptert * adapter, head = NULL; printf("SOEM (Simple Open EtherCAT Master)\nSlaveinfo\n"); if (argc > 1) @@ -714,13 +714,13 @@ int main(int argc, char *argv[]) printf("Usage: slaveinfo ifname [options]\nifname = eth0 for example\nOptions :\n -sdo : print SDO info\n -map : print mapping\n"); printf ("Available adapters\n"); - adapter = ec_find_adapters (); + head = adapter = ec_find_adapters (); while (adapter != NULL) { printf ("Description : %s, Device to use for wpcap: %s\n", adapter->desc,adapter->name); adapter = adapter->next; } - ec_free_adapters(adapter); + ec_free_adapters(head); } printf("End program\n"); diff --git a/test/simple_ng/simple_ng.c b/test/simple_ng/simple_ng.c index 835d7bcd..281c375e 100644 --- a/test/simple_ng/simple_ng.c +++ b/test/simple_ng/simple_ng.c @@ -286,18 +286,18 @@ main(int argc, char *argv[]) Fieldbus fieldbus; if (argc != 2) { - ec_adaptert * adapter = NULL; + ec_adaptert * adapter, head = NULL; printf("Usage: simple_ng IFNAME1\n" "IFNAME1 is the NIC interface name, e.g. 'eth0'\n"); printf("\nAvailable adapters:\n"); - adapter = ec_find_adapters(); + head = adapter = ec_find_adapters(); while (adapter != NULL) { printf(" - %s (%s)\n", adapter->name, adapter->desc); adapter = adapter->next; } - ec_free_adapters(adapter); + ec_free_adapters(head); return 1; } diff --git a/test/win32/eepromtool/eepromtool.c b/test/win32/eepromtool/eepromtool.c index 6e9092c2..13db1b6b 100644 --- a/test/win32/eepromtool/eepromtool.c +++ b/test/win32/eepromtool/eepromtool.c @@ -348,7 +348,7 @@ void eepromtool(char *ifname, int slave, int mode, char *fname) int main(int argc, char *argv[]) { - ec_adaptert * adapter = NULL; + ec_adaptert * adapter, head = NULL; printf("SOEM (Simple Open EtherCAT Master)\nEEPROM tool\n"); if (argc > 4) @@ -374,12 +374,13 @@ int main(int argc, char *argv[]) printf(" -wi write EEPROM, input Intel Hex format\n"); /* Print the list */ printf ("Available adapters\n"); - adapter = ec_find_adapters (); + head = adapter = ec_find_adapters (); while (adapter != NULL) { printf ("Description : %s, Device to use for wpcap: %s\n", adapter->desc,adapter->name); adapter = adapter->next; } + ec_free_adapters(adapter); } printf("End program\n"); diff --git a/test/win32/simple_test/simple_test.c b/test/win32/simple_test/simple_test.c index 5cb1c191..b31d9514 100644 --- a/test/win32/simple_test/simple_test.c +++ b/test/win32/simple_test/simple_test.c @@ -344,7 +344,7 @@ char ifbuf[1024]; int main(int argc, char *argv[]) { - ec_adaptert * adapter = NULL; + ec_adaptert * adapter, head = NULL; printf("SOEM (Simple Open EtherCAT Master)\nSimple test\n"); if (argc > 1) @@ -360,12 +360,13 @@ int main(int argc, char *argv[]) printf("Usage: simple_test ifname1\n"); /* Print the list */ printf ("Available adapters\n"); - adapter = ec_find_adapters (); + head = adapter = ec_find_adapters (); while (adapter != NULL) { printf ("Description : %s, Device to use for wpcap: %s\n", adapter->desc,adapter->name); adapter = adapter->next; } + ec_free_adapters(adapter); } printf("End program\n"); diff --git a/test/win32/slaveinfo/slaveinfo.c b/test/win32/slaveinfo/slaveinfo.c index 9485c5ad..9f4bf5fa 100644 --- a/test/win32/slaveinfo/slaveinfo.c +++ b/test/win32/slaveinfo/slaveinfo.c @@ -617,7 +617,7 @@ char ifbuf[1024]; int main(int argc, char *argv[]) { - ec_adaptert * adapter = NULL; + ec_adaptert * adapter, head = NULL; printf("SOEM (Simple Open EtherCAT Master)\nSlaveinfo\n"); if (argc > 1) @@ -633,12 +633,13 @@ int main(int argc, char *argv[]) printf("Usage: slaveinfo ifname [options]\nifname = eth0 for example\nOptions :\n -sdo : print SDO info\n -map : print mapping\n"); /* Print the list */ printf ("Available adapters\n"); - adapter = ec_find_adapters (); + head = adapter = ec_find_adapters (); while (adapter != NULL) { printf ("Description : %s, Device to use for wpcap: %s\n", adapter->desc,adapter->name); adapter = adapter->next; } + ec_free_adapters(head); } printf("End program\n");