Complete.Org: Mailing Lists: Archives: freeciv-dev: July 2005:
[Freeciv-Dev] (PR#13515) fix NULL problems in unittype dereferences
Home

[Freeciv-Dev] (PR#13515) fix NULL problems in unittype dereferences

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
Subject: [Freeciv-Dev] (PR#13515) fix NULL problems in unittype dereferences
From: "Jason Short" <jdorje@xxxxxxxxxxxxxxxxxxxxx>
Date: Fri, 22 Jul 2005 17:28:16 -0700
Reply-to: bugs@xxxxxxxxxxx

<URL: http://bugs.freeciv.org/Ticket/Display.html?id=13515 >

This patch fixes several NULL-dereferencing bugs.  These were caused by
the unittype-ptr patch but it's not at all clear to me the code was
correct before.  In particular this might be buggy in 2.0.

Previous code:

  ut = ai_choose_xxx(...);
  if (ut >= 0) {
    /* blah */
  }

*or*

  ut = ai_choose_xxx(...);
  if (ut < U_LAST) {
    /* blah */
  }

which is rather bugprone since it's easy to mix the two cases.

New code:

  ut = ai_choose_xxx(...);
  if (ut) {
    /* blah */
  }

which is better except that it was mis-transcribed in several places.

-jason

Index: ai/advmilitary.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/ai/advmilitary.c,v
retrieving revision 1.193
diff -p -u -r1.193 advmilitary.c
--- ai/advmilitary.c    22 Jul 2005 16:18:04 -0000      1.193
+++ ai/advmilitary.c    23 Jul 2005 00:25:04 -0000
@@ -1400,7 +1400,7 @@ void military_advisor_choose_build(struc
 
   /* Consider making a land bodyguard */
   punittype = ai_choose_bodyguard(pcity, LAND_MOVING, L_DEFEND_GOOD);
-  if (punittype >= 0) {
+  if (punittype) {
     ai_unit_consider_bodyguard(pcity, punittype, choice);
   }
 
@@ -1418,7 +1418,7 @@ void military_advisor_choose_build(struc
 
   /* Consider making a sea bodyguard */
   punittype = ai_choose_bodyguard(pcity, SEA_MOVING, L_DEFEND_GOOD);
-  if (punittype >= 0) {
+  if (punittype) {
     ai_unit_consider_bodyguard(pcity, punittype, choice);
   }
 
@@ -1428,7 +1428,7 @@ void military_advisor_choose_build(struc
   /* Check if we want a sailing attacker. Have to put sailing first
      before we mung the seamap */
   punittype = ai_choose_attacker(pcity, SEA_MOVING);
-  if (punittype >= 0) {
+  if (punittype) {
     virtualunit = create_unit_virtual(pplayer, pcity, punittype,
                                       do_make_unit_veteran(pcity, punittype));
     kill_something_with(pplayer, pcity, virtualunit, choice);
Index: ai/aidiplomat.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/ai/aidiplomat.c,v
retrieving revision 1.55
diff -p -u -r1.55 aidiplomat.c
--- ai/aidiplomat.c     22 Jul 2005 16:18:04 -0000      1.55
+++ ai/aidiplomat.c     23 Jul 2005 00:25:04 -0000
@@ -147,7 +147,7 @@ void ai_choose_diplomat_offensive(struct
   struct unit_type *ut = best_role_unit(pcity, F_DIPLOMAT);
   struct ai_data *ai = ai_data_get(pplayer);
 
-  if (ut) {
+  if (!ut) {
     /* We don't know diplomats yet! */
     return;
   }

[Prev in Thread] Current Thread [Next in Thread]
  • [Freeciv-Dev] (PR#13515) fix NULL problems in unittype dereferences, Jason Short <=