Author: vlendec
Date: 2006-07-18 15:09:53 +0000 (Tue, 18 Jul 2006)
New Revision: 17118

WebSVN: 
http://websvn.samba.org/cgi-bin/viewcvs.cgi?view=rev&root=samba&rev=17118

Log:
Simplify nesting a bit, fix some memleaks
Modified:
   branches/tmp/vl-messaging/source/lib/messages_dgram.c


Changeset:
Modified: branches/tmp/vl-messaging/source/lib/messages_dgram.c
===================================================================
--- branches/tmp/vl-messaging/source/lib/messages_dgram.c       2006-07-18 
14:38:39 UTC (rev 17117)
+++ branches/tmp/vl-messaging/source/lib/messages_dgram.c       2006-07-18 
15:09:53 UTC (rev 17118)
@@ -121,30 +121,44 @@
 void receive_on_socket_dgram(int socket_fd, struct message_list **queue)
 {
        DATA_BLOB dgram = read_from_dgram_socket(socket_fd);
-       if (dgram.data != NULL) {
-               struct message_rec *msg = (struct message_rec *)dgram.data;
-               if (msg->len == dgram.length) {
-                       /* save the received message */
-                       struct message_list *li, *tmp;
+       struct message_rec *msg;
+       struct message_list *li, *tmp;
 
-                       li = TALLOC_ZERO_P(NULL, struct message_list);
-                       if(li == NULL) {
-                               DEBUG(0, ("talloc failed\n"));
-                               return;
-                       }
-                       li->msg = (struct message_rec*)TALLOC_ARRAY(li, 
uint8_t, msg->len);
-                       if(li->msg == NULL) {
-                               DEBUG(0, ("talloc failed\n"));
-                               TALLOC_FREE(li);
-                               return;
-                       }                       
-                       memcpy(li->msg, msg, msg->len);
+       if (dgram.data == NULL) {
+               return;
+       }
 
-                       DLIST_ADD_END((*queue), li, tmp);
-               } else {
-                       DEBUG(5, ("Invalid message length received, got %d, "
-                                 "expected %d\n", msg->len, dgram.length));
-               }
-               SAFE_FREE(dgram.data);
+       msg = (struct message_rec *)dgram.data;
+       if (dgram.length < sizeof(msg->len)) {
+               DEBUG(5, ("Message too short: %d\n", dgram.length));
+               data_blob_free(&dgram);
+               return;
        }
+
+       if (msg->len != dgram.length) {
+               DEBUG(5, ("Invalid message length received, got %d, "
+                         "expected %d\n", msg->len, dgram.length));
+               data_blob_free(&dgram);
+               return;
+       }
+
+       /* save the received message */
+
+       if (!(li = TALLOC_ZERO_P(NULL, struct message_list))) {
+               DEBUG(0, ("talloc failed\n"));
+               data_blob_free(&dgram);
+               return;
+       }
+
+       if (!(li->msg = (struct message_rec*)TALLOC(li, msg->len))) {
+               DEBUG(0, ("talloc failed\n"));
+               TALLOC_FREE(li);
+               data_blob_free(&dgram);
+               return;
+       }                       
+
+       memcpy(li->msg, msg, msg->len);
+       data_blob_free(&dgram);
+
+       DLIST_ADD_END((*queue), li, tmp);
 }

Reply via email to