changeset 36633:f8f6da5f5c57

stream_netstream: Simplify packet handling. Also fix a few memleak bugs, missing 0-termination for error strings and missing malloc failure checks.
author reimar
date Sat, 25 Jan 2014 16:54:59 +0000
parents 43575335eab4
children 945eab072c9d
files stream/stream_netstream.c stream/stream_netstream.h
diffstat 2 files changed, 19 insertions(+), 24 deletions(-) [+]
line wrap: on
line diff
--- a/stream/stream_netstream.c	Sat Jan 25 16:54:58 2014 +0000
+++ b/stream/stream_netstream.c	Sat Jan 25 16:54:59 2014 +0000
@@ -154,7 +154,7 @@
   case NET_STREAM_OK:
     return pack;
   case NET_STREAM_ERROR:
-    if(pack->len > sizeof(mp_net_stream_packet_t))
+    if(pack->len > 0)
       mp_msg(MSGT_STREAM,MSGL_ERR, "Fill buffer failed: %s\n",pack->data);
     else
       mp_msg(MSGT_STREAM,MSGL_ERR, "Fill buffer failed\n");
@@ -173,16 +173,18 @@
   if(!pack) {
     return -1;
   }
-  len = pack->len - sizeof(mp_net_stream_packet_t);
+  len = pack->len;
   if(len > max_len) {
     mp_msg(MSGT_STREAM,MSGL_ERR, "Got a too big a packet %d / %d\n",len,max_len);
-    free(pack);
-    return 0;
+    goto err_out;
   }
   if(len > 0)
     memcpy(buffer,pack->data,len);
   free(pack);
   return len;
+err_out:
+  free(pack);
+  return 0;
 }
 
 
@@ -252,8 +254,7 @@
     goto error;
   }
 
-  if(pack->len != sizeof(mp_net_stream_packet_t) +
-     sizeof(mp_net_stream_opened_t)) {
+  if(pack->len != sizeof(mp_net_stream_opened_t)) {
     mp_msg(MSGT_OPEN,MSGL_ERR, "Invalid open response packet len (%d bytes)\n",pack->len);
     free(pack);
     goto error;
--- a/stream/stream_netstream.h	Sat Jan 25 16:54:58 2014 +0000
+++ b/stream/stream_netstream.h	Sat Jan 25 16:54:59 2014 +0000
@@ -91,36 +91,30 @@
 
 static mp_net_stream_packet_t* read_packet(int fd) {
   uint16_t len;
-  mp_net_stream_packet_t* pack = malloc(sizeof(mp_net_stream_packet_t));
+  mp_net_stream_packet_t* pack = calloc(1, PACKET_MAX_SIZE + 1);
 
-  if(!net_read(fd,(char*)pack,sizeof(mp_net_stream_packet_t))) {
-    free(pack);
-    return NULL;
-  }
+  if(!pack || !net_read(fd,(char*)pack,sizeof(mp_net_stream_packet_t)))
+    goto err_out;
   pack->len = le2me_16(pack->len);
 
   if(pack->len < sizeof(mp_net_stream_packet_t)) {
     mp_msg(MSGT_NETST,MSGL_WARN,"Got invalid packet (too small: %d)\n",pack->len);
-    free(pack);
-    return NULL;
+    goto err_out;
   }
   if(pack->len > PACKET_MAX_SIZE) {
     mp_msg(MSGT_NETST,MSGL_WARN,"Got invalid packet (too big: %d)\n",pack->len);
-    free(pack);
-    return NULL;
+    goto err_out;
   }
+  pack->len -= sizeof(mp_net_stream_packet_t);
   len = pack->len;
-  if(len > sizeof(mp_net_stream_packet_t)) {
-    pack = realloc(pack,len);
-    if(!pack) {
-      mp_msg(MSGT_NETST,MSGL_ERR,"Failed to get memory for the packet (%d bytes)\n",len);
-      return NULL;
-    }
-    if(!net_read(fd,pack->data,len - sizeof(mp_net_stream_packet_t)))
-      return NULL;
-  }
+  if(len > 0 && !net_read(fd,pack->data,len))
+    goto err_out;
   //  printf ("Read packet %d %d %d\n",fd,pack->cmd,pack->len);
   return pack;
+
+err_out:
+  free(pack);
+  return NULL;
 }
 
 static int net_write(int fd, char* buf, int len) {