[vlc-devel] [PATCH 5b/5] stream_ReadLine: supplement API with maximum length parameter

Pierre Ynard linkfanel at yahoo.fr
Sun Sep 6 04:49:32 CEST 2020


Very long lines are occasionally encountered in text web resources such
as HTML, JSON or other API data, which the current hardcoded limit can't
accommodate. This allows the caller to configure that limit to do so.

The default remains unchanged at 200 kB. As foremost consumers of those
web resources, lua streams raise the limit to 4 MB, which should be
sufficient to support more than most of them, but still reasonable to
prevent any issue.

This patch applies on top of patch 4, as an alternative to patch 5a.
This approach preserves the current API while giving all callers a
convenient way to further adjust the length limit to what they strictly
need.

This particular variant is presented for readability, but other
possibilities include marking the old vlc_stream_ReadLine() as
deprecated, or outright adding the new argument to it and breaking that
API.

Fixes #24957 (YouTube playback)


diff --git a/include/vlc_stream.h b/include/vlc_stream.h
index bc6c0d3..1d3af28 100644
--- a/include/vlc_stream.h
+++ b/include/vlc_stream.h
@@ -310,7 +310,8 @@ static inline int vlc_stream_Control(stream_t *s, int query, ...)
 }
 
 VLC_API block_t *vlc_stream_Block(stream_t *s, size_t);
-VLC_API char *vlc_stream_ReadLine(stream_t *);
+VLC_API char *vlc_stream_ReadLine2(stream_t *, size_t);
+#define vlc_stream_ReadLine(s) vlc_stream_ReadLine2(s, 0)
 
 /**
  * Reads a directory.
diff --git a/modules/lua/libs/stream.c b/modules/lua/libs/stream.c
index 29ac68a..f9d7502 100644
--- a/modules/lua/libs/stream.c
+++ b/modules/lua/libs/stream.c
@@ -138,7 +138,8 @@ static int vlclua_stream_read( lua_State *L )
 static int vlclua_stream_readline( lua_State *L )
 {
     stream_t **pp_stream = (stream_t **)luaL_checkudata( L, 1, "stream" );
-    char *psz_line = vlc_stream_ReadLine( *pp_stream );
+    /* TODO: expose max as optional argument */
+    char *psz_line = vlc_stream_ReadLine2( *pp_stream, STREAM_LINE_MAX );
     if( psz_line )
     {
         lua_pushstring( L, psz_line );
diff --git a/modules/lua/stream_filter.c b/modules/lua/stream_filter.c
index cc4bd29..24977b2 100644
--- a/modules/lua/stream_filter.c
+++ b/modules/lua/stream_filter.c
@@ -86,7 +86,8 @@ static int vlclua_demux_read( lua_State *L )
 static int vlclua_demux_readline( lua_State *L )
 {
     stream_t *s = (stream_t *)vlclua_get_this(L);
-    char *line = vlc_stream_ReadLine(s->s);
+    /* TODO: expose max as optional argument */
+    char *line = vlc_stream_ReadLine2(s->s, STREAM_LINE_MAX);
 
     if (line != NULL)
     {
diff --git a/modules/lua/vlc.h b/modules/lua/vlc.h
index 36bcc25..20927ca 100644
--- a/modules/lua/vlc.h
+++ b/modules/lua/vlc.h
@@ -73,6 +73,7 @@ int ReadMeta( demux_meta_t * );
 int FetchMeta( meta_fetcher_t * );
 int FindArt( meta_fetcher_t * );
 
+#define STREAM_LINE_MAX (2048*2048)
 int Import_LuaPlaylist( vlc_object_t * );
 void Close_LuaPlaylist( vlc_object_t * );
 
diff --git a/src/input/stream.c b/src/input/stream.c
index 01d37c7..88b9c65 100644
--- a/src/input/stream.c
+++ b/src/input/stream.c
@@ -180,18 +180,20 @@ stream_t *(vlc_stream_NewMRL)(vlc_object_t* parent, const char* mrl )
 /**
  * Read from the stream until first newline.
  * \param s Stream handle to read from
+ * \param max Maximum line length, fail if no newline is encountered. 0 for default limit.
  * \return A pointer to the allocated output string. You need to free this when you are done.
  */
 #define STREAM_PROBE_LINE 2048
 #define STREAM_LINE_MAX (2048*100)
-char *vlc_stream_ReadLine( stream_t *s )
+char *vlc_stream_ReadLine2( stream_t *s, size_t i_max )
 {
     stream_priv_t *priv = (stream_priv_t *)s;
     char *p_line = NULL;
     size_t i_line = 0;
     bool b_data = false;
 
-    size_t i_max = STREAM_LINE_MAX;
+    if( i_max == 0 )
+        i_max = STREAM_LINE_MAX;
     /* Prevent integer overflows below */
     i_max = __MIN( i_max, SIZE_MAX / 3 );
 
diff --git a/src/libvlccore.sym b/src/libvlccore.sym
index 31fcd37..623fee9 100644
--- a/src/libvlccore.sym
+++ b/src/libvlccore.sym
@@ -365,7 +365,7 @@ vlc_stream_MemoryNew
 vlc_stream_Peek
 vlc_stream_Read
 vlc_stream_ReadBlock
-vlc_stream_ReadLine
+vlc_stream_ReadLine2
 vlc_stream_ReadPartial
 vlc_stream_Seek
 vlc_stream_Tell
-- 
Pierre Ynard
"Une âme dans un corps, c'est comme un dessin sur une feuille de papier."


More information about the vlc-devel mailing list