Complete.Org: Mailing Lists: Archives: freeciv-dev: April 2004:
[Freeciv-Dev] (PR#8522) historian agent copies units badly
Home

[Freeciv-Dev] (PR#8522) historian agent copies units badly

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: undisclosed-recipients: ;
Subject: [Freeciv-Dev] (PR#8522) historian agent copies units badly
From: "Jason Short" <jdorje@xxxxxxxxxxxxxxxxxxxxx>
Date: Thu, 15 Apr 2004 15:00:37 -0700
Reply-to: rt@xxxxxxxxxxx

<URL: http://rt.freeciv.org/Ticket/Display.html?id=8522 >

The historian "agent" copies units by doing:

   *pold_unit = *punit;

but this isn't safe since some of the fields of the unit are pointers to 
allocated memory.  If punit is deallocated this will give memory 
corruption when pold_unit is accessed.

The only alternative is a copy_unit function:

   struct unit *copy_unit(struct unit *punit)
   {
     pnew_unit = create_unit_virtual(...);

     *pnew_unit = *punit;

     pnew_unit->field = fc_malloc(sizeof(*pnew_unit->field));
     memcpy(pnew_unit->field, punit->field, sizeof(*punit->field));
     /* etc. */

     return pnew_unit;
   }

Perhaps the only such field right now is the orders list.  But this may 
not always be true.

jason




[Prev in Thread] Current Thread [Next in Thread]
  • [Freeciv-Dev] (PR#8522) historian agent copies units badly, Jason Short <=