[klibc] [PATCH] ipconfig: fix memory leak, and exit if no interfaces to configure

Bryan O'Sullivan bos at serpentine.com
Tue Apr 29 14:24:49 PDT 2003


Very small patch.

main.c |   30 +++++++++++++++++++++++++-----
1 files changed, 25 insertions(+), 5 deletions(-)


diff -Nru a/ipconfig/main.c b/ipconfig/main.c
--- a/ipconfig/main.c	Tue Apr 29 13:22:55 2003
+++ b/ipconfig/main.c	Tue Apr 29 13:22:55 2003
@@ -391,10 +391,10 @@
 	dev->name = devname;
 
 	if (netdev_init_if(dev) == -1)
-		return;
+		goto bail;
 
 	if (bootp_init_if(dev) == -1)
-		return;
+		goto bail;
 
 	printf("IP-Config: %s hardware address", dev->name);
 	for (i = 0; i < dev->hwlen; i++)
@@ -406,6 +406,22 @@
 
 	if (dev->caps && netdev_up(dev) == 0)
 		add_one_dev(dev);
+	return;
+ bail:
+	free(dev);
+}
+
+static void check_for_devs(void)
+{
+	struct state *s;
+	
+	for (s = slist; s; s = s->next) {
+		if (s->dev->caps)
+			return;
+	}
+
+	fprintf(stderr, "%s: no devices to configure\n", progname);
+	exit(1);
 }
 
 int main(int argc, char *argv[])
@@ -430,8 +446,6 @@
 			}
 			local_port = port;
 			remote_port = local_port - 1;
-			printf("IP-Config: binding source port to %d, "
-			       "dest to %d\n", local_port, remote_port);
 			break;
 		case 't':
 			loop_timeout = atoi(optarg);
@@ -448,7 +462,6 @@
 		case 'd':
 			add_device(optarg);
 			break;
-
 		case '?':
 			fprintf(stderr, "%s: invalid option -%c\n",
 				progname, optopt);
@@ -456,6 +469,13 @@
 		}
 	} while (1);
 
+	check_for_devs();
+	
+	if (local_port != LOCAL_PORT) {
+		printf("IP-Config: binding source port to %d, "
+		       "dest to %d\n", local_port, remote_port);
+	}
+	
 	loop();
 
 	return 0;





More information about the klibc mailing list