Complete.Org: Mailing Lists: Archives: freeciv-dev: February 2003:
[Freeciv-Dev] (PR#3474) another valgrind warning: uninitialized fields i
Home

[Freeciv-Dev] (PR#3474) another valgrind warning: uninitialized fields i

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: undisclosed-recipients:;
Subject: [Freeciv-Dev] (PR#3474) another valgrind warning: uninitialized fields in unit packet
From: "Jason Short" <jdorje@xxxxxxxxxxxxxxxxxxxxx>
Date: Wed, 19 Feb 2003 01:12:53 -0800
Reply-to: rt@xxxxxxxxxxxxxx

I was using valgrind to try to track down a bug, and instead it 
generated pages of warnings telling me that I was sending uninitialized 
data over the network.

Aside from the convenience of not having to see all these warnings, I 
think it is inelegant and potentially dangerous to send unset data over 
the network.  The attached patch initializes all the unused fields to 0. 
  Doing it field-by-field instead of with memset makes the code a lot 
clearer, IMO.

As a side effect of this patch, the fuel is always sent as 0 which 
shaves one byte off of the packet (most of the time).

jason

Index: client/civclient.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/civclient.c,v
retrieving revision 1.163
diff -u -r1.163 civclient.c
--- client/civclient.c  2003/02/13 00:48:45     1.163
+++ client/civclient.c  2003/02/19 09:06:48
@@ -530,6 +530,27 @@
 {
   struct packet_unit_info info;
 
+  /* Unused fields.  These aren't a part of the packet, but are
+   * initialized for completness - we don't want to send uninitialized
+   * data over the network.  (Note we could just use memset instead).
+   * They're set above the applicable fields for safety. */
+  info.carried = FALSE;
+  info.ai = FALSE;
+  info.paradropped = FALSE;
+  info.connecting = FALSE;
+  info.hp = 0;
+  info.upkeep = 0;
+  info.upkeep_food = 0;
+  info.upkeep_gold = 0;
+  info.unhappiness = 0;
+  info.activity_count = 0;
+  info.goto_dest_x = 0;
+  info.goto_dest_y = 0;
+  info.info_city_id = 0;
+  info.serial_num = 0;
+  info.fuel = 0;
+
+  /* These fields are used. */
   info.id=punit->id;
   info.owner=punit->owner;
   info.x=punit->x;

[Prev in Thread] Current Thread [Next in Thread]
  • [Freeciv-Dev] (PR#3474) another valgrind warning: uninitialized fields in unit packet, Jason Short <=