From patchwork Sun Jun 19 11:28:24 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nil Admirari X-Patchwork-Id: 36332 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:1a22:b0:84:42e0:ad30 with SMTP id cj34csp1095464pzb; Sun, 19 Jun 2022 04:29:10 -0700 (PDT) X-Google-Smtp-Source: AGRyM1ut6L4+p+vLgjtYXOtiW1tUf7Z8/jTbtqhUBbKobp5YX32HVtWs1dEXXiKOdLFz7gieKQ/n X-Received: by 2002:a05:6402:5202:b0:435:8034:fb87 with SMTP id s2-20020a056402520200b004358034fb87mr842314edd.409.1655638150519; Sun, 19 Jun 2022 04:29:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1655638150; cv=none; d=google.com; s=arc-20160816; b=a9IzBgExz+RfmWJy3IoOp0Vy6yyLgVmlLK8mkixEgGGBqTGRdZzt6OQcCDB3Z8rteQ OzJ/AYVYfKqDFdjA98utifG4z9+ijSw0Q9iaIzo+gX9Fa2UP1KgQmvCUyEzwgSQV7psa jMQs2/E0vf1CGmdHwObyWfJHAwpEYdTLZL4DEhv62LlADc2fKsB/74ZFJjGE+ePCTKvV BQJC6knqQym77djm5uNJc4fv9FPr7IiI8MTGDWJsqPFzljrM9fXLAvc+EmqFbrVWuedE 3aQ2ZE8uX0AI7bbGoiGB3wFsX93z731G0EGPwyn3OgMJnVCxlcvmQ8zPon6tEbgNxxsW ithA== 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=vNf9J4FFP4okh4+d5VqlxqT3Ti4YRMH1Xza3sdgkR5gvpjjCJ4vuMUgXobyNr8YTQ4 ZrXAMogIrBKS+5VwQeP1gWYaXZ8hFlcwjTMJ3B2c5kW3aQjGhdG4ansrno171HPALWfM Je2y9dk0ITSXKcV+/cISVbAYTQaYJot4EiReca+vtlZKh+IlWNG7hoEqS9bEPqPmeEee VRKnpJBcigGY+lQpXDz46x190vcjogQ6S1U1zlb7PlxvDTqJtH9SWXEPQgcbReAW3DO9 oISoXp5/CLwOZz4+wMc/DYOljXYikXhOtH6WkdYvzlgcyByvfxaohX1iUDPhlBRYTWzg 8r+Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@mailo.com header.s=mailo header.b="n/W+rWch"; 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 t16-20020a056402241000b0043574de150asi3093832eda.34.2022.06.19.04.29.09; Sun, 19 Jun 2022 04:29:10 -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="n/W+rWch"; 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 79E4768B366; Sun, 19 Jun 2022 14:28:45 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from msg-6.mailo.com (ip-16.mailobj.net [213.182.54.16]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id E36E268B3C6 for ; Sun, 19 Jun 2022 14:28:36 +0300 (EEST) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=mailo.com; s=mailo; t=1655638113; 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=n/W+rWch4gCY6xU1GMU0/UDF9cz3GXDX4sO6v4a853T4AfWxFQ9eaOCB4qvH1xbR6 J3gDY+AlDek61zX+Lk6qf76p9wnmGSSfMhfFF7BCmhn+wENHUEoPE+gzgolVBS6d5O B9L0S3caC2KejdQqnrNzwcquI0gxP7Pf/r6zKrac= Received: by b-1.in.mailobj.net [192.168.90.11] with ESMTP via ip-206.mailobj.net [213.182.55.206] Sun, 19 Jun 2022 13:28:33 +0200 (CEST) X-EA-Auth: CJQq2BshyqCFIduOY8+7cyAhFAhqsjO5Eh+yw4EcATaXA7LHw3vT2NWSdXG82fCMc17BiJnUUIlOgqc0qN5z9M09Ap6GaCpXUJx+p4QUiYw= From: Nil Admirari To: ffmpeg-devel@ffmpeg.org Date: Sun, 19 Jun 2022 14:28:24 +0300 Message-Id: <20220619112827.4183-2-nil-admirari@mailo.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220619112827.4183-1-nil-admirari@mailo.com> References: <20220619112827.4183-1-nil-admirari@mailo.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v18 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: XyK1FBB4+Cqf --- 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).