Author: sveinung
Date: Wed May 10 10:17:01 2017
New Revision: 35480

URL: http://svn.gna.org/viewcvs/freeciv?rev=35480&view=rev
Log:
JSON protocol: fix field_addr memory leak.

Don't leak the memory where a packet's field address is stored.

Reported by Andreas Røsdal

See hrm Bug #658389

Modified:
    branches/S3_0/common/generate_packets.py
    branches/S3_0/common/networking/dataio_json.c
    branches/S3_0/common/networking/packets_json.h

Modified: branches/S3_0/common/generate_packets.py
URL: 
http://svn.gna.org/viewcvs/freeciv/branches/S3_0/common/generate_packets.py?rev=35480&r1=35479&r2=35480&view=diff
==============================================================================
--- branches/S3_0/common/generate_packets.py    (original)
+++ branches/S3_0/common/generate_packets.py    Wed May 10 10:17:01 2017
@@ -1064,7 +1064,12 @@
 
         if len(self.fields) != 0:
             faddr = '''#ifdef FREECIV_JSON_CONNECTION
-  struct plocation field_addr = *plocation_field_new(NULL);
+  struct plocation field_addr;
+  {
+    struct plocation *field_addr_tmp = plocation_field_new(NULL);
+    field_addr = *field_addr_tmp;
+    FC_FREE(field_addr_tmp);
+  }
 #endif /* FREECIV_JSON_CONNECTION */
 '''
         else:
@@ -1200,7 +1205,12 @@
 
         if len(self.fields) != 0:
             faddr = '''#ifdef FREECIV_JSON_CONNECTION
-  struct plocation field_addr = *plocation_field_new(NULL);
+  struct plocation field_addr;
+  {
+    struct plocation *field_addr_tmp = plocation_field_new(NULL);
+    field_addr = *field_addr_tmp;
+    FC_FREE(field_addr_tmp);
+  }
 #endif /* FREECIV_JSON_CONNECTION */
 '''
         else:

Modified: branches/S3_0/common/networking/dataio_json.c
URL: 
http://svn.gna.org/viewcvs/freeciv/branches/S3_0/common/networking/dataio_json.c?rev=35480&r1=35479&r2=35480&view=diff
==============================================================================
--- branches/S3_0/common/networking/dataio_json.c       (original)
+++ branches/S3_0/common/networking/dataio_json.c       Wed May 10 10:17:01 2017
@@ -442,7 +442,7 @@
   int kind, range, value;
   bool survives, present, quiet;
 
-  struct plocation req_field;
+  struct plocation *req_field;
 
   /* Find the requirement object. */
   json_t *requirement = plocation_read_data(json_packet, location);
@@ -452,47 +452,49 @@
   }
 
   /* Find the requirement object fields and translate their values. */
-  req_field = *plocation_field_new("kind");
-  if (!dio_get_uint8_json(requirement, &req_field, &kind)) {
+  req_field = plocation_field_new("kind");
+  if (!dio_get_uint8_json(requirement, req_field, &kind)) {
     log_error("ERROR: Unable to get part of requirement from location: %s",
               plocation_name(location));
     return FALSE;
   }
 
-  req_field.name = "value";
-  if (!dio_get_sint32_json(requirement, &req_field, &value)) {
+  req_field->name = "value";
+  if (!dio_get_sint32_json(requirement, req_field, &value)) {
     log_error("ERROR: Unable to get part of requirement from location: %s",
               plocation_name(location));
     return FALSE;
   }
 
-  req_field.name = "range";
-  if (!dio_get_uint8_json(requirement, &req_field, &range)) {
+  req_field->name = "range";
+  if (!dio_get_uint8_json(requirement, req_field, &range)) {
     log_error("ERROR: Unable to get part of requirement from location: %s",
               plocation_name(location));
     return FALSE;
   }
 
-  req_field.name = "survives";
-  if (!dio_get_bool8_json(requirement, &req_field, &survives)) {
+  req_field->name = "survives";
+  if (!dio_get_bool8_json(requirement, req_field, &survives)) {
     log_error("ERROR: Unable to get part of requirement from location: %s",
               plocation_name(location));
     return FALSE;
   }
 
-  req_field.name = "present";
-  if (!dio_get_bool8_json(requirement, &req_field, &present)) {
+  req_field->name = "present";
+  if (!dio_get_bool8_json(requirement, req_field, &present)) {
     log_error("ERROR: Unable to get part of requirement from location: %s",
               plocation_name(location));
     return FALSE;
   }
 
-  req_field.name = "quiet";
-  if (!dio_get_bool8_json(requirement, &req_field, &quiet)) {
+  req_field->name = "quiet";
+  if (!dio_get_bool8_json(requirement, req_field, &quiet)) {
     log_error("ERROR: Unable to get part of requirement from location: %s",
               plocation_name(location));
     return FALSE;
   }
+
+  FC_FREE(req_field);
 
   /* Create a requirement with the values sent over the network. */
   *preq = req_from_values(kind, range, survives, present, quiet, value);
@@ -507,7 +509,7 @@
                                      const struct plocation *location,
                                      struct act_prob *prob)
 {
-  struct plocation ap_field;
+  struct plocation *ap_field;
 
   /* Find the action probability object. */
   json_t *action_probability = plocation_read_data(json_packet, location);
@@ -519,21 +521,23 @@
 
   /* Find the action probability object fields and translate their
    * values. */
-  ap_field = *plocation_field_new("min");
-  if (!dio_get_uint8_json(action_probability, &ap_field, &prob->min)) {
+  ap_field = plocation_field_new("min");
+  if (!dio_get_uint8_json(action_probability, ap_field, &prob->min)) {
     log_error("ERROR: Unable to get part of action probability "
               "from location: %s",
               plocation_name(location));
     return FALSE;
   }
 
-  ap_field.name = "max";
-  if (!dio_get_uint8_json(action_probability, &ap_field, &prob->max)) {
+  ap_field->name = "max";
+  if (!dio_get_uint8_json(action_probability, ap_field, &prob->max)) {
     log_error("ERROR: Unable to get part of action probability "
               "from location: %s",
               plocation_name(location));
     return FALSE;
   }
+
+  FC_FREE(ap_field);
 
   return TRUE;
 }

Modified: branches/S3_0/common/networking/packets_json.h
URL: 
http://svn.gna.org/viewcvs/freeciv/branches/S3_0/common/networking/packets_json.h?rev=35480&r1=35479&r2=35480&view=diff
==============================================================================
--- branches/S3_0/common/networking/packets_json.h      (original)
+++ branches/S3_0/common/networking/packets_json.h      Wed May 10 10:17:01 2017
@@ -26,15 +26,16 @@
 
 #define SEND_PACKET_START(packet_type) \
   unsigned char buffer[MAX_LEN_PACKET * 5]; \
-  struct plocation pid_addr; \
+  struct plocation *pid_addr; \
   char *json_buffer = NULL; \
   struct json_data_out dout; \
   dout.json = json_object(); \
   \
   dio_output_init(&(dout.raw), buffer, sizeof(buffer)); \
   dio_put_uint16_raw(&(dout.raw), 0);                   \
-  pid_addr = *plocation_field_new("pid");               \
-  dio_put_uint8_json(&dout, &pid_addr, packet_type);
+  pid_addr = plocation_field_new("pid");               \
+  dio_put_uint8_json(&dout, pid_addr, packet_type);                        \
+  FC_FREE(pid_addr);
 
 #define SEND_PACKET_END(packet_type) \
   { \


_______________________________________________
Freeciv-commits mailing list
Freeciv-commits@gna.org
https://mail.gna.org/listinfo/freeciv-commits

Reply via email to