From patchwork Mon Jun 20 10:29:58 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nil Admirari X-Patchwork-Id: 36351 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:1a22:b0:84:42e0:ad30 with SMTP id cj34csp1582582pzb; Mon, 20 Jun 2022 03:30:49 -0700 (PDT) X-Google-Smtp-Source: AGRyM1tVtJBYcMZyao5nsGxXex/8/xg6cBtg06fgnbENVJZkeK0aT41GUI6rAwScoHwxGSj0ECgk X-Received: by 2002:aa7:c9d2:0:b0:42e:1776:63e0 with SMTP id i18-20020aa7c9d2000000b0042e177663e0mr27950603edt.185.1655721049668; Mon, 20 Jun 2022 03:30:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1655721049; cv=none; d=google.com; s=arc-20160816; b=WPzL4wXqqf44fbcV86jIBZO7fwgXJav5pSiE43j4Q8IoV7IPbkGdPp9iazGddl5nXk GWtWITMGapkvnbRuVUmoy5pq6tYTNfrcsJ5vGvm/BVGIgO721tCZUDDFIz/UREfGeaJw cuuhzxwK3yzb+lmiFV5FopMUOa2JZi8lPvslFSgd4JNuQWq8phMBohjvp8dHb8ZKIdYF 1lQtXYe0Ij201VUar6ajesYEgSvhjODOEW8uQlKNMzYrjO9rjEQPxjrWvxeeYq7SyWsr DFX2GlRPIdCtWzL0TAaVYHuDB19j7ZNgKhXN9TYjhhwx37RcqESsAM9EoNLV0pwsnnAF YWuw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:mime-version:references:in-reply-to:message-id :date:to:from:dkim-signature:delivered-to; bh=IaVfbkAMPJmo5ccLZrHX7LP9Zp7x7cao/96Qr/z2YfE=; b=JT22fb/hszM5uSa1E+NADuuyMmzDk4JiHTS5HtwyzZ8qOZvvbilQSWHPw48h+CJbjJ AHC908dhqrsUT1HzOc6r5DOYqB1vSb3e1VjljlYR29/nWrVDdZQA6bZjtBKE1Wpim7pZ qU6Cqa0NOLmWvZWWQAWNrnNNtMPday0Utj2asscCT2QLQAjdCeQmnTPrt2KKSor/siUo J1z+jamwZB08MKw85DUAe4MmN1w9/6MjEnRIgxqchW0lyHo1COlccLE1VFn+k2NwuPhd GajL9clSVuvDjH+mjVkZgCzNTEO6rNPTnhEa62pc6aRqzmTIEdckUsRNyQqUHXW57oe9 tFGA== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@mailo.com header.s=mailo header.b=FumCYiyw; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=mailo.com Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id g8-20020a056402090800b0042fb3bafedfsi16082470edz.348.2022.06.20.03.30.49; Mon, 20 Jun 2022 03:30:49 -0700 (PDT) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@mailo.com header.s=mailo header.b=FumCYiyw; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=mailo.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 1A2B168B638; Mon, 20 Jun 2022 13:30:26 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from msg-1.mailo.com (msg-1.mailo.com [213.182.54.11]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 29A7D68B5F7 for ; Mon, 20 Jun 2022 13:30:17 +0300 (EEST) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=mailo.com; s=mailo; t=1655721012; bh=c1YRUp7vg5tAKdb+y9E5UgC1j56iaSUBEf360VCix7o=; h=X-EA-Auth:From:To:Subject:Date:Message-Id:X-Mailer:In-Reply-To: References:MIME-Version:Content-Transfer-Encoding; b=FumCYiywt4N2ETIOC39ru5i8GVpzfRriHH1CMqdcfsRUrYgtT9baOVGcKHDd5cxuo lpEuDM9Ug5DeQhIRcYHkg9HSrcHYl7aax47Im8x5zikTsvs1nGJn9O1e15kbFcUuh6 vI1Hkk0z2yETgdrTTJF38cKD0fcUpiLq2T8jCuI8= Received: by b-4.in.mailobj.net [192.168.90.14] with ESMTP via ip-206.mailobj.net [213.182.55.206] Mon, 20 Jun 2022 12:30:12 +0200 (CEST) X-EA-Auth: TForIJ1ggx49nJZJ4YOzFOdwHHDIoOxztnMpXTFgdF3YDA030jA3xEoJzpGH2DETH6IWJnEL03Yzzcple2n5TufcJm3jyM7YqzesP+rXX3I= From: Nil Admirari To: ffmpeg-devel@ffmpeg.org Date: Mon, 20 Jun 2022 13:29:58 +0300 Message-Id: <20220620103001.15035-2-nil-admirari@mailo.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220620103001.15035-1-nil-admirari@mailo.com> References: <20220620103001.15035-1-nil-admirari@mailo.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v20 2/5] compat/w32dlfcn.h: Remove MAX_PATH limit and replace LoadLibraryExA with LoadLibraryExW X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: sfn0wuvrCUu1 --- compat/w32dlfcn.h | 100 ++++++++++++++++++++++++++++++++---------- libavcodec/mf_utils.h | 1 + 2 files changed, 79 insertions(+), 22 deletions(-) diff --git a/compat/w32dlfcn.h b/compat/w32dlfcn.h index 52a94efafb..fb1aa1b72e 100644 --- a/compat/w32dlfcn.h +++ b/compat/w32dlfcn.h @@ -20,11 +20,40 @@ #define COMPAT_W32DLFCN_H #ifdef _WIN32 +#include + #include + #include "config.h" -#if (_WIN32_WINNT < 0x0602) || HAVE_WINRT +#include "libavutil/macros.h" #include "libavutil/wchar_filename.h" -#endif + +static inline wchar_t *get_module_filename(HMODULE module) +{ + wchar_t *path = NULL, *new_path; + DWORD path_size = 0, path_len; + + do { + path_size = path_size ? FFMIN(2 * path_size, INT16_MAX + 1) : MAX_PATH; + new_path = av_realloc_array(path, path_size, sizeof *path); + if (!new_path) { + av_free(path); + return NULL; + } + path = new_path; + // Returns path_size in case of insufficient buffer. + // Whether the error is set or not and whether the output + // is null-terminated or not depends on the version of Windows. + path_len = GetModuleFileNameW(module, path, path_size); + } while (path_len && path_size <= INT16_MAX && 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 +63,53 @@ */ static inline HMODULE win32_dlopen(const char *name) { + wchar_t *name_w; + HMODULE module = NULL; + if (utf8towchar(name, &name_w)) + name_w = NULL; #if _WIN32_WINNT < 0x0602 - // Need to check if KB2533623 is available + // On Win7 and earlier we 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; + 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; + new_path = wcsrchr(path, '\\'); + if (!new_path) goto exit; - path[pathlen] = '\\'; + 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 the 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 the 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,16 +126,19 @@ 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; + module = LoadPackagedLibrary(name_w, 0); #else - return LoadLibraryExA(name, NULL, LOAD_LIBRARY_SEARCH_APPLICATION_DIR | LOAD_LIBRARY_SEARCH_SYSTEM32); +#define LOAD_FLAGS (LOAD_LIBRARY_SEARCH_APPLICATION_DIR | LOAD_LIBRARY_SEARCH_SYSTEM32) + /* filename may be be in CP_ACP */ + if (!name_w) + return LoadLibraryExA(name, NULL, LOAD_FLAGS); + module = LoadLibraryExW(name_w, NULL, LOAD_FLAGS); +#undef LOAD_FLAGS #endif + av_free(name_w); + return module; } #define dlopen(name, flags) win32_dlopen(name) #define dlclose FreeLibrary diff --git a/libavcodec/mf_utils.h b/libavcodec/mf_utils.h index 3b12344f3e..aebfb9ad21 100644 --- a/libavcodec/mf_utils.h +++ b/libavcodec/mf_utils.h @@ -29,6 +29,7 @@ // mf*.h headers below indirectly include strmif.h.) #include #else +#define NO_DSHOW_STRSAFE #include // Older versions of mingw-w64 need codecapi.h explicitly included, while newer // ones include it implicitly from dshow.h (via uuids.h).