[MPlayer-cvslog] r36729 - in trunk/stream: stream_netstream.c stream_netstream.h

reimar subversion at mplayerhq.hu
Sat Jan 25 17:54:59 CET 2014


Author: reimar
Date: Sat Jan 25 17:54:59 2014
New Revision: 36729

Log:
stream_netstream: Simplify packet handling.

Also fix a few memleak bugs, missing 0-termination for error
strings and missing malloc failure checks.

Modified:
   trunk/stream/stream_netstream.c
   trunk/stream/stream_netstream.h

Modified: trunk/stream/stream_netstream.c
==============================================================================
--- trunk/stream/stream_netstream.c	Sat Jan 25 17:54:58 2014	(r36728)
+++ trunk/stream/stream_netstream.c	Sat Jan 25 17:54:59 2014	(r36729)
@@ -154,7 +154,7 @@ static mp_net_stream_packet_t* send_net_
   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 @@ static int fill_buffer(stream_t *s, char
   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 @@ static int open_s(stream_t *stream,int m
     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;

Modified: trunk/stream/stream_netstream.h
==============================================================================
--- trunk/stream/stream_netstream.h	Sat Jan 25 17:54:58 2014	(r36728)
+++ trunk/stream/stream_netstream.h	Sat Jan 25 17:54:59 2014	(r36729)
@@ -91,36 +91,30 @@ static int net_read(int fd, char* buf, i
 
 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) {


More information about the MPlayer-cvslog mailing list