Patch/Bugfix for BOOTP in rtems-ss-20020205

Thomas Doerfler Thomas.Doerfler at imd-systems.de
Tue Mar 19 19:47:20 UTC 2002


Hi Till,

I got your ideas. I have finalized the patch 
(hopefully), find it attached to this mail.

Joel, I also send this patch the the GNATS 
system...

wkr,
	Thomas.

> Thomas Doerfler wrote:
> 
> > Hi Chris,
> >
> > ok, here is a corrected verison of my patch. Thank you for
> > your comments!
> >
> > wkr,
> >         Thomas.
> 
> OOps - I was too fast with my last posting. IMO, it is not easily
> asserted that processOptions() is NOT called multiple times -
> hence you could generate a memory leak. Therefore, I would
> free() before strdup() [note it's to call free with a 0 arg
> - your patch could also omit the NULL test]
> 
> My suggested version of the patch would read:
> 
> -- Till
> 
> 
> diff -c bootp_subr.c.orig bootp_subr.c
> *** bootp_subr.c.orig Tue Mar 19 11:24:42 2002
> --- bootp_subr.c Tue Mar 19 11:34:31 2002
> ***************
> *** 694,700 ****
>   static char dhcp_gotlogserver = 0;
>   static struct sockaddr_in dhcp_netmask;
>   static struct sockaddr_in dhcp_gw;
> ! static char *dhcp_hostname;
> 
>   static void
>   processOptions (unsigned char *optbuf, int optbufSize)
> --- 694,700 ----
>   static char dhcp_gotlogserver = 0;
>   static struct sockaddr_in dhcp_netmask;
>   static struct sockaddr_in dhcp_gw;
> ! static char *dhcp_hostname=0;
> 
>   static void
>   processOptions (unsigned char *optbuf, int optbufSize)
> ***************
> *** 801,806 ****
> --- 801,807 ----
>         if (sethostname (p, len) < 0)
>           panic("Can't set host name");
>         printf("Hostname is %s\n", p);
> +       free(dhcp_hostname);
>         dhcp_hostname = strdup(p);
>         break;
> 
> ***************
> *** 904,910 ****
>         update_files = 0;
>       }
> 
> !   memset(dhcp_hostname, 0, sizeof(dhcp_hostname));
> 
>     /*
>      * Find a network interface.
> --- 905,912 ----
>         update_files = 0;
>       }
> 
> !   free(dhcp_hostname);
> !   dhcp_hostname=0;
> 
>     /*
>      * Find a network interface.
> 
> 
> >
> >
> > > Thomas Doerfler wrote:
> > > >
> > > > Hm, good question. At least it would not hurt. But the line I
> > > > complaint is at the start of bootpc_init, long before any
> > > > bootp options are processed. So at least at the first run of
> > > > bootpc_init, "dhcp_hostname" is definitely NULL.
> > > >
> > >
> > > Any future runs if called could have dhcp_hostname set so we should free
> > > the memory. It is allocated using strdup.
> > >
> > > > The question is: can bootpc_init be called multiple times?
> > > > Normally it will only be called once after system boot, but
> > > > are there other situations?
> > >
> > > A system may have some form of redundant network configuration and so
> > > need to obtain a network address. This is just an example not a
> > > recommendation for a network design.
> > >
> > > --
> > >  Chris Johns, cjohns at cybertec.com.au
> >
> > --------------------------------------------
> > 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
> >
> >   ------------------------------------------------------------------------
> > --- 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  Tue Mar 19 12:39:24 2002
> > @@ -902,12 +902,15 @@
> >      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));
> > -
> > +  if (dhcp_hostname != NULL) {
> > +    free(dhcp_hostname);
> > +    dhcp_hostname = NULL;
> > +  }
> > +
> >    /*
> >     * Find a network interface.
> >     */
> >    for (ifp = ifnet; ifp != 0; ifp = ifp->if_next)
> >      if ((ifp->if_flags &

--------------------------------------------
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	Tue Mar 19 20:43:55 2002
@@ -692,11 +692,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,10 +799,12 @@
       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);
+      if (dhcp_hostname != NULL)
+	free(dhcp_hostname);
       dhcp_hostname = strdup(p);
       break;
 
     case 7:
       /* Log servers */
@@ -902,12 +904,15 @@
     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));
-  
+  if (dhcp_hostname != NULL) {
+    free(dhcp_hostname);
+    dhcp_hostname = NULL;
+  }
+
   /*
    * Find a network interface.
    */
   for (ifp = ifnet; ifp != 0; ifp = ifp->if_next)
     if ((ifp->if_flags &


More information about the users mailing list