diff mbox series

[FFmpeg-devel,v13,3/4] compat/w32dlfcn.h: Remove MAX_PATH limit and replace LoadLibraryExA with LoadLibraryExW

Message ID 20220609190406.6339-3-nil-admirari@mailo.com
State New
Headers show
Series [FFmpeg-devel,v13,1/4] libavutil/wchar_filename.h: Add whcartoutf8, wchartoansi and utf8toansi | expand

Checks

Context Check Description
andriy/make_x86 success Make finished
andriy/make_fate_x86 success Make fate finished

Commit Message

nihil-admirari June 9, 2022, 7:04 p.m. UTC
---
 compat/w32dlfcn.h | 80 +++++++++++++++++++++++++++++++++++++----------
 1 file changed, 64 insertions(+), 16 deletions(-)

Comments

Michael Niedermayer June 10, 2022, 6:50 p.m. UTC | #1
On Thu, Jun 09, 2022 at 10:04:05PM +0300, Nil Admirari wrote:
> ---
>  compat/w32dlfcn.h | 80 +++++++++++++++++++++++++++++++++++++----------
>  1 file changed, 64 insertions(+), 16 deletions(-)

breaks build on mingw64
CC	libavcodec/mfenc.o
In file included from /usr/share/mingw-w64/include/dshow.h:40:0,
                 from src/libavcodec/mf_utils.h:32,
                 from src/libavcodec/mfenc.c:26:
/usr/share/mingw-w64/include/strmif.h:16503:2: warning: #warning COM interfaces layout in this header has not been verified. [-Wcpp]
 #warning COM interfaces layout in this header has not been verified.
  ^~~~~~~
/usr/share/mingw-w64/include/strmif.h:16504:2: warning: #warning COM interfaces with incorrect layout may not work at all. [-Wcpp]
 #warning COM interfaces with incorrect layout may not work at all.
  ^~~~~~~
/usr/share/mingw-w64/include/strmif.h:16505:1: note: #pragma message: Interface IAMAsyncReaderTimestampScaling has unverified layout.
 __MINGW_BROKEN_INTERFACE(INTERFACE)
 ^~~~~~~~~~~~~~~~~~~~~~~~
/usr/share/mingw-w64/include/strmif.h:16533:2: warning: #warning COM interfaces layout in this header has not been verified. [-Wcpp]
 #warning COM interfaces layout in this header has not been verified.
  ^~~~~~~
/usr/share/mingw-w64/include/strmif.h:16534:2: warning: #warning COM interfaces with incorrect layout may not work at all. [-Wcpp]
 #warning COM interfaces with incorrect layout may not work at all.
  ^~~~~~~
/usr/share/mingw-w64/include/strmif.h:16535:1: note: #pragma message: Interface IAMPluginControl has unverified layout.
 __MINGW_BROKEN_INTERFACE(INTERFACE)
 ^~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/share/mingw-w64/include/dshow.h:33:0,
                 from src/libavcodec/mf_utils.h:32,
                 from src/libavcodec/mfenc.c:26:
src/libavutil/wchar_filename.h: In function ‘add_extended_prefix’:
src/libavutil/wchar_filename.h:211:9: error: ‘wcscpy_instead_use_StringCbCopyW_or_StringCchCopyW’ undeclared (first use in this function)
         wcscpy(temp_w, unc_prefix);
         ^
src/libavutil/wchar_filename.h:211:9: note: each undeclared identifier is reported only once for each function it appears in
In file included from src/compat/w32dlfcn.h:25:0,
                 from src/libavcodec/mfenc.c:32:
src/libavutil/wchar_filename.h:211:22: warning: left-hand operand of comma expression has no effect [-Wunused-value]
         wcscpy(temp_w, unc_prefix);
                      ^
src/libavutil/wchar_filename.h:211:15: warning: statement with no effect [-Wunused-value]
         wcscpy(temp_w, unc_prefix);
               ^
In file included from /usr/share/mingw-w64/include/dshow.h:33:0,
                 from src/libavcodec/mf_utils.h:32,
                 from src/libavcodec/mfenc.c:26:
src/libavutil/wchar_filename.h:212:9: error: ‘wcscat_instead_use_StringCbCatW_or_StringCchCatW’ undeclared (first use in this function); did you mean ‘wcscpy_instead_use_StringCbCopyW_or_StringCchCopyW’?
         wcscat(temp_w, path_w + 2);
         ^
In file included from src/compat/w32dlfcn.h:25:0,
                 from src/libavcodec/mfenc.c:32:
src/libavutil/wchar_filename.h:212:22: warning: left-hand operand of comma expression has no effect [-Wunused-value]
         wcscat(temp_w, path_w + 2);
                      ^
src/libavutil/wchar_filename.h:212:22: warning: statement with no effect [-Wunused-value]
         wcscat(temp_w, path_w + 2);
               ~~~~~~~^~~~~~~~~~~~~
src/libavutil/wchar_filename.h:220:22: warning: left-hand operand of comma expression has no effect [-Wunused-value]
         wcscpy(temp_w, extended_path_prefix);
                      ^
src/libavutil/wchar_filename.h:220:15: warning: statement with no effect [-Wunused-value]
         wcscpy(temp_w, extended_path_prefix);
               ^
src/libavutil/wchar_filename.h:221:22: warning: left-hand operand of comma expression has no effect [-Wunused-value]
         wcscat(temp_w, path_w);
                      ^
src/libavutil/wchar_filename.h:221:15: warning: statement with no effect [-Wunused-value]
         wcscat(temp_w, path_w);
               ^
src/ffbuild/common.mak:81: recipe for target 'libavcodec/mfenc.o' failed
make: *** [libavcodec/mfenc.o] Error 1

[...]
nihil-admirari June 13, 2022, 4:47 p.m. UTC | #2
> src/libavutil/wchar_filename.h: In function ‘add_extended_prefix’:
> src/libavutil/wchar_filename.h:211:9: error: ‘wcscpy_instead_use_StringCbCopyW_or_StringCchCopyW’
> undeclared (first use in this function)
> wcscpy(temp_w, unc_prefix);

are fixed by defining NO_DSHOW_STRSAFE:
https://ffmpeg.org/pipermail/ffmpeg-devel/2022-June/297492.html.

I don't know whether it's enough to fix COM related errors: I never got them.
diff mbox series

Patch

diff --git a/compat/w32dlfcn.h b/compat/w32dlfcn.h
index 52a94ef..6b0dd7d 100644
--- a/compat/w32dlfcn.h
+++ b/compat/w32dlfcn.h
@@ -22,9 +22,31 @@ 
 #ifdef _WIN32
 #include <windows.h>
 #include "config.h"
-#if (_WIN32_WINNT < 0x0602) || HAVE_WINRT
 #include "libavutil/wchar_filename.h"
-#endif
+
+static inline wchar_t *get_module_filename(HMODULE module)
+{
+    wchar_t *path = NULL, *new_path = NULL;
+    DWORD path_size = 0, path_len = 0;
+
+    do {
+        path_size = path_size ? 2 * path_size : MAX_PATH;
+        new_path = av_realloc_array(path, path_size, sizeof *path);
+        if (!new_path) {
+            av_free(path);
+            return NULL;
+        }
+        path = new_path;
+        path_len = GetModuleFileNameW(module, path, path_size);
+    } while (path_len && path_size <= 32768 && path_size <= path_len);
+
+    if (!path_len) {
+        av_free(path);
+        return NULL;
+    }
+    return path;
+}
+
 /**
  * Safe function used to open dynamic libs. This attempts to improve program security
  * by removing the current directory from the dll search path. Only dll's found in the
@@ -34,29 +56,53 @@ 
  */
 static inline HMODULE win32_dlopen(const char *name)
 {
+    wchar_t *name_w = NULL;
+    if (utf8towchar(name, &name_w))
+        name_w = NULL;
 #if _WIN32_WINNT < 0x0602
     // Need to check if KB2533623 is available
     if (!GetProcAddress(GetModuleHandleW(L"kernel32.dll"), "SetDefaultDllDirectories")) {
         HMODULE module = NULL;
-        wchar_t *path = NULL, *name_w = NULL;
-        DWORD pathlen;
-        if (utf8towchar(name, &name_w))
+        wchar_t *path = NULL, *new_path = NULL;
+        DWORD pathlen, pathsize, namelen;
+        if (!name_w)
             goto exit;
-        path = (wchar_t *)av_calloc(MAX_PATH, sizeof(wchar_t));
+        namelen = wcslen(name_w);
         // Try local directory first
-        pathlen = GetModuleFileNameW(NULL, path, MAX_PATH);
-        pathlen = wcsrchr(path, '\\') - path;
-        if (pathlen == 0 || pathlen + wcslen(name_w) + 2 > MAX_PATH)
+        path = get_module_filename(NULL);
+        if (!path)
             goto exit;
-        path[pathlen] = '\\';
+        new_path = wcsrchr(path, '\\');
+        if (!new_path)
+            goto exit;
+        pathlen = new_path - path;
+        pathsize = pathlen + namelen + 2;
+        new_path = av_realloc_array(path, pathsize, sizeof *path);
+        if (!new_path)
+            goto exit;
+        path = new_path;
         wcscpy(path + pathlen + 1, name_w);
         module = LoadLibraryExW(path, NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
         if (module == NULL) {
             // Next try System32 directory
-            pathlen = GetSystemDirectoryW(path, MAX_PATH);
-            if (pathlen == 0 || pathlen + wcslen(name_w) + 2 > MAX_PATH)
+            pathlen = GetSystemDirectoryW(path, pathsize);
+            if (!pathlen)
                 goto exit;
-            path[pathlen] = '\\';
+            // Buffer is not enough in two cases:
+            // 1. system directory + \ + module name
+            // 2. system directory even without module name.
+            if (pathlen + namelen + 2 > pathsize) {
+                pathsize = pathlen + namelen + 2;
+                new_path = av_realloc_array(path, pathsize, sizeof *path);
+                if (!new_path)
+                    goto exit;
+                path = new_path;
+                // Query again to handle case #2.
+                pathlen = GetSystemDirectoryW(path, pathsize);
+                if (!pathlen)
+                    goto exit;
+            }
+            path[pathlen] = L'\\';
             wcscpy(path + pathlen + 1, name_w);
             module = LoadLibraryExW(path, NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
         }
@@ -73,15 +119,17 @@  exit:
 #   define LOAD_LIBRARY_SEARCH_SYSTEM32        0x00000800
 #endif
 #if HAVE_WINRT
-    wchar_t *name_w = NULL;
     int ret;
-    if (utf8towchar(name, &name_w))
+    if (!name_w)
         return NULL;
     ret = LoadPackagedLibrary(name_w, 0);
     av_free(name_w);
     return ret;
 #else
-    return LoadLibraryExA(name, NULL, LOAD_LIBRARY_SEARCH_APPLICATION_DIR | LOAD_LIBRARY_SEARCH_SYSTEM32);
+    /* filename may be be in CP_ACP */
+    if (!name_w)
+        return LoadLibraryExA(name, NULL, LOAD_LIBRARY_SEARCH_APPLICATION_DIR | LOAD_LIBRARY_SEARCH_SYSTEM32);
+    return LoadLibraryExW(name_w, NULL, LOAD_LIBRARY_SEARCH_APPLICATION_DIR | LOAD_LIBRARY_SEARCH_SYSTEM32);
 #endif
 }
 #define dlopen(name, flags) win32_dlopen(name)