This is the mail archive of the gdb-patches@sourceware.cygnus.com mailing list for the GDB project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

generic_load: Don't barf on load ............ 0x0


Hello,

The next generic load patch stops generic_load corrupting its stack when
it is asked to load a file with a very long name.

Think about these:

  #define GENERIC_LOAD_CHUNK 256
  char buf[GENERIC_LOAD_CHUNK + 8];
  n = sscanf (filename, "%s 0x%lx", buf, &load_offset);

What I don't get is how adding 8 was going to make it better :-)

enjoy,
	Andrew
Mon Oct 18 23:36:58 1999  Andrew Cagney  <cagney@b1.cygnus.com>

	* symfile.c (generic_load): Use strtoul to scan the optional load
 	offset.  Allocate a filename of the correct size.

Index: symfile.c
===================================================================
RCS file: /cvs/cvsfiles/devo/gdb/symfile.c,v
retrieving revision 1.201
diff -p -r1.201 symfile.c
*** symfile.c	1999/10/18 13:43:45	1.201
--- symfile.c	1999/10/18 14:34:52
*************** load_command (arg, from_tty)
*** 1212,1248 ****
  static int validate_download = 0;
  
  void
! generic_load (filename, from_tty)
!      char *filename;
!      int from_tty;
  {
-   struct cleanup *old_cleanups;
    asection *s;
    bfd *loadfile_bfd;
    time_t start_time, end_time;	/* Start and end times of download */
    unsigned long data_count = 0;	/* Number of bytes transferred to memory */
    unsigned long write_count = 0;	/* Number of writes needed. */
-   int n;
    unsigned long load_offset = 0;	/* offset to add to vma for each section */
!   char buf[GENERIC_LOAD_CHUNK + 8];
  
!   /* enable user to specify address for downloading as 2nd arg to load */
!   n = sscanf (filename, "%s 0x%lx", buf, &load_offset);
!   if (n > 1)
!     filename = buf;
    else
      load_offset = 0;
  
    loadfile_bfd = bfd_openr (filename, gnutarget);
    if (loadfile_bfd == NULL)
      {
        perror_with_name (filename);
        return;
      }
    /* FIXME: should be checking for errors from bfd_close (for one thing,
       on error it does not free all the storage associated with the
       bfd).  */
!   old_cleanups = make_cleanup ((make_cleanup_func) bfd_close, loadfile_bfd);
  
    if (!bfd_check_format (loadfile_bfd, bfd_object))
      {
--- 1212,1258 ----
  static int validate_download = 0;
  
  void
! generic_load (char *args, int from_tty)
  {
    asection *s;
    bfd *loadfile_bfd;
    time_t start_time, end_time;	/* Start and end times of download */
    unsigned long data_count = 0;	/* Number of bytes transferred to memory */
    unsigned long write_count = 0;	/* Number of writes needed. */
    unsigned long load_offset = 0;	/* offset to add to vma for each section */
!   char *filename;
!   struct cleanup *old_cleanups;
!   char *offptr;
  
!   /* Parse the input argument - the user can specify a load offset as
!      a second argument. */
!   filename = xmalloc (strlen (args) + 1);
!   old_cleanups = make_cleanup (free, filename);
!   strcpy (filename, args);
!   offptr = strchr (filename, ' ');
!   if (offptr != NULL)
!     {
!       char *endptr;
!       load_offset = strtoul (offptr, &endptr, 0);
!       if (offptr == endptr)
! 	error ("Invalid download offset:%s\n", offptr);
!       *offptr = '\0';
!     }
    else
      load_offset = 0;
  
+   /* Open the file for loading. */
    loadfile_bfd = bfd_openr (filename, gnutarget);
    if (loadfile_bfd == NULL)
      {
        perror_with_name (filename);
        return;
      }
+ 
    /* FIXME: should be checking for errors from bfd_close (for one thing,
       on error it does not free all the storage associated with the
       bfd).  */
!   make_cleanup ((make_cleanup_func) bfd_close, loadfile_bfd);
  
    if (!bfd_check_format (loadfile_bfd, bfd_object))
      {

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]