[vlc-devel] [PATCH] sout: file: truncate path if too long

Thomas Guillem thomas at gllm.fr
Mon Dec 16 14:15:49 CET 2019


This fixes sout chain fail when the input path/url is too long. This can easily
happen when the source is a network url that can be longer than local pathes.
---
 modules/access_output/file.c | 32 ++++++++++++++++++++++++++++++++
 1 file changed, 32 insertions(+)

diff --git a/modules/access_output/file.c b/modules/access_output/file.c
index c983e3a8f09..86fe032dd0b 100644
--- a/modules/access_output/file.c
+++ b/modules/access_output/file.c
@@ -36,6 +36,8 @@
 #include <errno.h>
 #include <sys/stat.h>
 #include <unistd.h>
+#include <limits.h>
+#include <string.h>
 #ifdef __OS2__
 #   include <io.h>      /* setmode() */
 #endif
@@ -298,6 +300,36 @@ static int Open( vlc_object_t *p_this )
         if (var_GetBool (p_access, SOUT_CFG_PREFIX"sync"))
             flags |= O_SYNC;
 #endif
+
+#ifndef NAME_MAX
+ #ifdef (MAX_PATH)
+  #define NAME_MAX MAX_PATH /* win32 */
+ #else
+  #define NAME_MAX 256
+ #endif
+#endif
+        /* Check if the path need to be truncated */
+        size_t path_len = strlen(path);
+        if (path_len > NAME_MAX)
+        {
+            /* Allocate the buf if needed since p_access->psz_path is const */
+            if (path == p_access->psz_path)
+            {
+                buf = strdup(path);
+                if (!buf)
+                    return VLC_ENOMEM;
+                path = buf;
+            }
+
+            /* Don't loose the extension if any */
+            const char *ext = strrchr(path, '.');
+            size_t ext_len = ext ? strlen(ext) : 0;
+            if (ext && ext_len <= 4)
+                strcpy(&buf[NAME_MAX - ext_len - 1], ext);
+            else
+                buf[NAME_MAX] = 0;
+        }
+
         do
         {
             fd = vlc_open (path, flags, 0666);
-- 
2.20.1



More information about the vlc-devel mailing list