[vlc-commits] net_Gets: rewrite, deal with errors

Rémi Denis-Courmont git at videolan.org
Sun Jul 21 18:47:47 CEST 2013


vlc/vlc-2.1 | branch: master | Rémi Denis-Courmont <remi at remlab.net> | Sun Jul 21 19:09:02 2013 +0300| [966bf6c1da96747744526e802370f82e9f9914fa] | committer: Rémi Denis-Courmont

net_Gets: rewrite, deal with errors

> http://git.videolan.org/gitweb.cgi/vlc/vlc-2.1.git/?a=commit;h=966bf6c1da96747744526e802370f82e9f9914fa
---

 src/network/io.c |   55 +++++++++++++++++++++++++++---------------------------
 1 file changed, 28 insertions(+), 27 deletions(-)

diff --git a/src/network/io.c b/src/network/io.c
index a55c44c..9b13b2a 100644
--- a/src/network/io.c
+++ b/src/network/io.c
@@ -440,46 +440,47 @@ error:
  * This function is not thread-safe; the same file descriptor I/O cannot be
  * read by another thread at the same time (although it can be written to).
  *
+ * @note This only works with stream-oriented file descriptors, not with
+ * datagram or packet-oriented ones.
+ *
  * @return nul-terminated heap-allocated string, or NULL on I/O error.
  */
-char *net_Gets( vlc_object_t *p_this, int fd, const v_socket_t *p_vs )
+char *net_Gets(vlc_object_t *obj, int fd, const v_socket_t *vs)
 {
-    char *psz_line = NULL, *ptr = NULL;
-    size_t  i_line = 0, i_max = 0;
-
+    char *buf = NULL;
+    size_t bufsize = 0, buflen = 0;
 
-    for( ;; )
+    for (;;)
     {
-        if( i_line == i_max )
+        if (buflen == bufsize)
         {
-            i_max += 1024;
-            psz_line = xrealloc( psz_line, i_max );
-            ptr = psz_line + i_line;
-        }
+            if (unlikely(bufsize >= (1 << 10)))
+                goto error; /* put sane buffer size limit */
 
-        if( net_Read( p_this, fd, p_vs, ptr, 1, true ) != 1 )
-        {
-            if( i_line == 0 )
-            {
-                free( psz_line );
-                return NULL;
-            }
-            break;
+            char *newbuf = realloc(buf, bufsize + 1024);
+            if (unlikely(newbuf == NULL))
+                goto error;
+            buf = newbuf;
+            bufsize += 1024;
         }
 
-        if ( *ptr == '\n' )
+        ssize_t val = net_Read(obj, fd, vs, buf + buflen, 1, false);
+        if (val < 1)
+            goto error;
+
+        if (buf[buflen] == '\n')
             break;
 
-        i_line++;
-        ptr++;
+        buflen++;
     }
 
-    *ptr-- = '\0';
-
-    if( ( ptr >= psz_line ) && ( *ptr == '\r' ) )
-        *ptr = '\0';
-
-    return psz_line;
+    buf[--buflen] = '\0';
+    if (buflen > 0 && buf[buflen - 1] == '\r')
+        buf[buflen] = '\0';
+    return buf;
+error:
+    free(buf);
+    return NULL;
 }
 
 #undef net_Printf



More information about the vlc-commits mailing list