Index: stream/stream_file.c
===================================================================
--- stream/stream_file.c	(revision 36298)
+++ stream/stream_file.c	(working copy)
@@ -168,6 +168,11 @@
     m_struct_free(&stream_opts,opts);
     return STREAM_ERROR;
   }
+  if(CONFIG_PATH_MAX_CHECK && strlen(filename) >= PATH_MAX) {
+    mp_msg(MSGT_OPEN,MSGL_ERR, "[file] File name too long\n");
+    m_struct_free(&stream_opts,opts);
+    return STREAM_ERROR;
+  }

 #if HAVE_DOS_PATHS
   // extract '/' from '/x:/path'
Index: sub/vobsub.c
===================================================================
--- sub/vobsub.c	(revision 36298)
+++ sub/vobsub.c	(working copy)
@@ -47,6 +47,20 @@
 // overridden if slang match any of vobsub streams.
 static int vobsubid = -2;

+#if CONFIG_PATH_LIMIT_CHECK
+static inline FILE *mplayer_fopen(const char *name, const char *mode)
+{
+    if (strlen(name) >= PATH_MAX) {
+        errno = ENAMETOOLONG;
+        return NULL;
+    }
+
+    return fopen(name, mode);
+}
+
+#define fopen mplayer_fopen
+#endif
+
 /**********************************************************************
  * RAR stream handling
  * The RAR file must have the same basename as the file to open
@@ -63,6 +77,8 @@
                               const char *const mode)
 {
     rar_stream_t *stream;
+    if (CONFIG_PATH_MAX_CHECK && strlen(filename) >= PATH_MAX)
+        return NULL;
     /* unrar_exec can only read */
     if (strcmp("r", mode) && strcmp("rb", mode)) {
         errno = EINVAL;
Index: sub/subreader.c
===================================================================
--- sub/subreader.c	(revision 36298)
+++ sub/subreader.c	(working copy)
@@ -88,6 +88,32 @@
 unsigned long previous_sub_end;
 #endif

+#if CONFIG_PATH_MAX_CHECK
+static inline FILE *mplayer_fopen(const char *name, const char *mode)
+{
+    if (strlen(name) >= PATH_MAX) {
+        errno = ENAMETOOLONG;
+        return NULL;
+    }
+
+    return fopen(name, mode);
+}
+
+#define fopen(n, m) mplayer_fopen(n, m)
+
+static inline DIR *mplayer_opendir(const char *name)
+{
+    if (strlen(name) >= PATH_MAX) {
+        errno = ENOENT;
+        return NULL;
+    }
+
+    return opendir(name);
+}
+
+#define opendir(n) mplayer_opendir(n)
+#endif
+
 static int eol(char p) {
 	return p=='\r' || p=='\n' || p=='\0';
 }
Index: configure
===================================================================
--- configure	(revision 36298)
+++ configure	(working copy)
@@ -855,6 +855,7 @@
 _priority=no
 def_dos_paths="#define HAVE_DOS_PATHS 0"
 def_stream_cache="#define CONFIG_STREAM_CACHE 1"
+def_path_max_check="#define CONFIG_PATH_MAX_CHECK 0"
 def_priority="#undef CONFIG_PRIORITY"
 def_pthread_cache="#undef PTHREAD_CACHE"
 shmem=no
@@ -1780,6 +1781,7 @@
   _priority=yes
   def_dos_paths="#define HAVE_DOS_PATHS 1"
   def_priority="#define CONFIG_PRIORITY 1"
+  def_path_max_check="#define CONFIG_PATH_MAX_CHECK 1"
 fi

 if wine ; then
@@ -8724,6 +8726,7 @@
 $def_macosx_finder
 $def_maemo
 $def_memalign_hack
+$def_path_max_check
 $def_priority
 $def_quicktime
 $def_restrict_keyword
