Skip to content

Commit

Permalink
tests: fix linked list leak
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
JohannesKauffmann authored and jkauffmann-manter committed Dec 12, 2024
1 parent 83c6264 commit 99e7586
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 18 deletions.
6 changes: 3 additions & 3 deletions test/linux/eepromtool/eepromtool.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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");
Expand Down
6 changes: 3 additions & 3 deletions test/linux/simple_test/simple_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
6 changes: 3 additions & 3 deletions test/linux/slaveinfo/slaveinfo.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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");
Expand Down
6 changes: 3 additions & 3 deletions test/simple_ng/simple_ng.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
5 changes: 3 additions & 2 deletions test/win32/eepromtool/eepromtool.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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");
Expand Down
5 changes: 3 additions & 2 deletions test/win32/simple_test/simple_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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");
Expand Down
5 changes: 3 additions & 2 deletions test/win32/slaveinfo/slaveinfo.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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");
Expand Down

0 comments on commit 99e7586

Please sign in to comment.