[maemo-commits] [maemo-commits] r15412 - in projects/haf/trunk/maemo-launcher: . launcher

From: subversion at stage.maemo.org subversion at stage.maemo.org
Date: Wed Apr 16 01:35:29 EEST 2008
Author: guillem
Date: 2008-04-16 01:35:28 +0300 (Wed, 16 Apr 2008)
New Revision: 15412

Modified:
   projects/haf/trunk/maemo-launcher/ChangeLog
   projects/haf/trunk/maemo-launcher/launcher/comm_msg.c
Log:
Cleanup memory pack and unpack


Modified: projects/haf/trunk/maemo-launcher/ChangeLog
===================================================================
--- projects/haf/trunk/maemo-launcher/ChangeLog	2008-04-15 22:35:26 UTC (rev 15411)
+++ projects/haf/trunk/maemo-launcher/ChangeLog	2008-04-15 22:35:28 UTC (rev 15412)
@@ -1,5 +1,12 @@
 2008-02-21  Guillem Jover  <guillem.jover at nokia.com>
 
+	* launcher/comm_msg.c (comm_msg_pack_mem): Get rid of cur variable,
+	and change msg->used after having copied the data.
+	(comm_msg_unpack_mem): Likewise. Add a check to make sure it does not
+	read outside the buffer. Remove error message.
+
+2008-02-21  Guillem Jover  <guillem.jover at nokia.com>
+
 	* launcher/comm_msg.h (comm_msg_grow): Add a need_size argument.
 	(comm_msg_grow): Likewise. Remove implicit end_size calculations.
 	Fix all callers.

Modified: projects/haf/trunk/maemo-launcher/launcher/comm_msg.c
===================================================================
--- projects/haf/trunk/maemo-launcher/launcher/comm_msg.c	2008-04-15 22:35:26 UTC (rev 15411)
+++ projects/haf/trunk/maemo-launcher/launcher/comm_msg.c	2008-04-15 22:35:28 UTC (rev 15412)
@@ -108,27 +108,25 @@
 static bool
 comm_msg_pack_mem(comm_msg_t *msg, const void *buf, uint32_t size)
 {
-  void *cur = msg->buf + msg->used;
   uint32_t aligned_size = WORD_ALIGN(size);
   uint32_t pad_size = aligned_size - size;
 
   if (!comm_msg_grow(msg, aligned_size + sizeof(uint32_t)))
     return false;
 
-  msg->used += aligned_size + sizeof(uint32_t);
-
   /* Pack the size. */
-  memcpy(cur, &aligned_size, sizeof(uint32_t));
+  memcpy(msg->buf + msg->used, &aligned_size, sizeof(uint32_t));
+  msg->used += sizeof(uint32_t);
 
   /* Pack the data. */
-  cur += sizeof(uint32_t);
-  memcpy(cur, buf, size);
+  memcpy(msg->buf + msg->used, buf, size);
+  msg->used += size;
 
   if (pad_size)
   {
     /* Add padding, if needed. */
-    cur += size;
-    memset(cur, 0, pad_size);
+    memset(msg->buf + msg->used, 0, pad_size);
+    msg->used += pad_size;
   }
 
   return true;
@@ -137,25 +135,29 @@
 static const void *
 comm_msg_unpack_mem(comm_msg_t *msg, uint32_t *size)
 {
-  void *cur = msg->buf + msg->read;
+  void *mem;
   int old_read = msg->read;
   uint32_t new_size;
 
+  if (msg->read + sizeof(uint32_t) > msg->used)
+    return NULL;
+
   /* Unpack the size. */
-  memcpy(&new_size, cur, sizeof(uint32_t));
+  memcpy(&new_size, msg->buf + msg->read, sizeof(uint32_t));
+  msg->read += sizeof(uint32_t);
 
-  msg->read += new_size + sizeof(uint32_t);
+  /* Keep a pointer to the data. */
+  mem = msg->buf + msg->read;
+  msg->read += new_size;
 
   if (msg->read > msg->used) {
-    error("trying to unpack more than available, unwinding action\n");
     msg->read = old_read;
     return NULL;
   }
 
   *size = new_size;
 
-  /* Return a pointer to the data. */
-  return cur + sizeof(uint32_t);
+  return mem;
 }
 
 bool


More information about the maemo-commits mailing list