Patch/Bugfix for BOOTP in rtems-ss-20020205

Thomas Doerfler Thomas.Doerfler at imd-systems.de
Wed Mar 20 08:00:05 UTC 2002


Hm,

so I am back at work. Two days ago I thought I would send a 
little patch, and now we are at least at release 3...

I have written a function "bootp_strdup_realloc", that mixes 
the strdup/realloc functionality, so on first call, simply a 
strdup is called, and on subsequent "overwrites" of the 
strings, a realloc/strcpy is called.

I applied this function to all previously "strdup'ed" strings 
in bootp. So I think there should be no more memory leaks, 
even when bootpc_init gets called a second time.

Please have a look at the patch. If I get no more complaints, 
I will send this patch to the GNATS system this evening...

wkr,
	Thomas Doerfler.


> 
> On Tuesday, March 19, 2002, at 05:02 PM, Chris Johns wrote:
> 
> > Thomas Doerfler wrote:
> > >
> >
> >> Where exactly is "free" and "malloc" hidden? Is it possible to make 
> >> them available with a different name (like "__real_free" or so)?
> >
> > BSD Internal header. A hack would be to undef them at the start of the 
> > file.
> >
> >
> 
> But be sure there are no BSD kernel-style malloc/free calls in the rest 
> of that source file!
> --
> Eric Norum <eric.norum at usask.ca>
> Department of Electrical Engineering
> University of Saskatchewan
> Saskatoon, Canada.
> Phone: (306) 966-5394   FAX:   (306) 966-5407

--------------------------------------------
IMD Ingenieurbuero fuer Microcomputertechnik
Thomas Doerfler           Herbststrasse 8
D-82178 Puchheim          Germany
email:    Thomas.Doerfler at imd-systems.de
PGP public key available at: http://www.imd-
systems.de/pgp_key.htm


-------------- next part --------------
--- rtems-ss-20020205/c/src/libnetworking/nfs/bootp_subr.c	Wed Sep 19 19:30:38 2001
+++ /usr/local/projects/sandbox/zem40/rtems_patch/bootp_subr.c	Wed Mar 20 08:20:46 2002
@@ -259,10 +259,35 @@
 	}
     }
 }
 #endif
 
+/*
+ * - determine space needed to store src string
+ * - allocate or reallocate dst, so that string fits in
+ * - copy string from src to dest
+ */
+void *bootp_strdup_realloc(char *dst,const char *src)
+{
+  size_t len;
+  void *realloc(void * __r, size_t __size);
+
+  if (dst == NULL) {
+    /* first allocation, simply use strdup */
+    dst = strdup(src);
+  }
+  else {
+    /* already allocated, so use realloc/strcpy */
+    len = strlen(src) + 1;  
+    dst = realloc(dst,len);
+    if (dst != NULL) {
+      strcpy(dst,src);
+    }
+  }
+  return dst;
+}
+
 int
 bootpc_call(call,reply,procp)
      struct bootp_packet *call;
      struct bootp_packet *reply;	/* output */
      struct proc *procp;
@@ -692,11 +717,11 @@
 static char dhcp_gotnetmask = 0;
 static char dhcp_gotserver = 0;
 static char dhcp_gotlogserver = 0;
 static struct sockaddr_in dhcp_netmask;
 static struct sockaddr_in dhcp_gw;
-static char *dhcp_hostname;
+static char *dhcp_hostname = NULL;
 
 static void
 processOptions (unsigned char *optbuf, int optbufSize)
 {
   int j = 0;
@@ -799,11 +824,11 @@
       if (len>=MAXHOSTNAMELEN)
         panic ("bootpc: hostname >=%d bytes", MAXHOSTNAMELEN);
       if (sethostname (p, len) < 0)
         panic("Can't set host name");
       printf("Hostname is %s\n", p);
-      dhcp_hostname = strdup(p);
+      dhcp_hostname = bootp_strdup_realloc(dhcp_hostname,p);
       break;
 
     case 7:
       /* Log servers */
       if (len % 4) 
@@ -815,11 +840,12 @@
       break;
 
     case 15:
       /* Domain name */
       if (p[0]) {
-        rtems_bsdnet_domain_name = strdup (p);
+        rtems_bsdnet_domain_name = 
+	  bootp_strdup_realloc(rtems_bsdnet_domain_name,p);
         printf("Domain name is %s\n", rtems_bsdnet_domain_name);
       }
       break;
 
     case 16:  /* Swap server IP address. unused */
@@ -850,17 +876,19 @@
       break;
 
     case 66:
       /* DHCP server name option */
       if (p[0])
-        rtems_bsdnet_bootp_server_name = strdup (p);
+        rtems_bsdnet_bootp_server_name = 
+	  bootp_strdup_realloc(rtems_bsdnet_bootp_server_name,p);
       break;
 
     case 67:
       /* DHCP bootfile option */
       if (p[0])
-        rtems_bsdnet_bootp_boot_file_name = strdup (p);
+        rtems_bsdnet_bootp_boot_file_name = 
+	  bootp_strdup_realloc(rtems_bsdnet_bootp_boot_file_name,p);
       break;
 
     default:
       printf ("Ignoring BOOTP/DHCP option code %d\n", code);
       break;
@@ -902,12 +930,12 @@
     if (rtems_create_root_fs () < 0) {
       printf("Error creating the root filesystem.\nFile not created.\n");
       update_files = 0;
     }
 
-  memset(dhcp_hostname, 0, sizeof(dhcp_hostname));
-  
+  dhcp_hostname = NULL;
+
   /*
    * Find a network interface.
    */
   for (ifp = ifnet; ifp != 0; ifp = ifp->if_next)
     if ((ifp->if_flags &
@@ -1008,18 +1036,20 @@
   if (dhcpOptionOverload & 1) {
     processOptions (reply.file, sizeof reply.file);
   }
   else {
     if (reply.file[0])
-      rtems_bsdnet_bootp_boot_file_name = strdup (reply.file);
+      rtems_bsdnet_bootp_boot_file_name = 
+	bootp_strdup_realloc(rtems_bsdnet_bootp_boot_file_name,reply.file);
   }
   if (dhcpOptionOverload & 2) {
     processOptions (reply.sname, sizeof reply.sname);
   }
   else {
     if (reply.sname[0])
-      rtems_bsdnet_bootp_server_name = strdup (reply.sname);
+      rtems_bsdnet_bootp_server_name = 
+	bootp_strdup_realloc(rtems_bsdnet_bootp_server_name,reply.sname);
   }
   if (rtems_bsdnet_bootp_server_name)
     printf ("Server name is %s\n", rtems_bsdnet_bootp_server_name);
   if (rtems_bsdnet_bootp_boot_file_name)
     printf ("Boot file is %s\n", rtems_bsdnet_bootp_boot_file_name);


More information about the users mailing list