From patchwork Fri Jun 17 09:31:38 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nil Admirari X-Patchwork-Id: 36295 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:1a22:b0:84:42e0:ad30 with SMTP id cj34csp23644pzb; Fri, 17 Jun 2022 02:32:31 -0700 (PDT) X-Google-Smtp-Source: AGRyM1s1Kj2y9Kq6ENGulaDast4+5sRpNvPFhdlblQ5ktH5bJQtnZ+z1WGMNid7xbIhvuHoQ7NiM X-Received: by 2002:a17:907:7d8e:b0:711:cf4b:9c5 with SMTP id oz14-20020a1709077d8e00b00711cf4b09c5mr8291750ejc.637.1655458351740; Fri, 17 Jun 2022 02:32:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1655458351; cv=none; d=google.com; s=arc-20160816; b=r+eYr0cjHbQQzdk+tf0nZXeTtsbX30gRH3j+PPQV+KC5O6RC5wMzKhnfLLwwJLtQnv oGh5zQJPS8DUTC0p+fkpD6JMQ+E5EuqO2nBP7nko5/taV0mSMV0QxqaO7U9LtkkWZb+p BAySXpIIB0pqdCvk+MMCvms5BYJZVGPomGv9c/BPojMeR86/ks7EE2ycYIoOZEoO+IJz AXMDURkVVdqpRE8N0OCuv3vxnBMjUTftGWCwivJ3uWl7uXZ739lVmR6ta1kScV5nJ/iW S4R+U+r1WnQfnq+u/qP4Z55k81e1Y9EIM/56yamtsueolmZE6pS/w0B5aAZ96NCXvjgO oFQw== 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=ncPz/Mnc+GEXoEw1CdkrtAKkJz2h4ino4B5Y7iIx7KyMrD4L3VJVzHnSurnhZf6Bfw FtcRsgLgdPLjvIHCXaNV6HP7K9MX61LalBehekKOxXKXjg6SOB8lBBVb9+Bus9pCQUie xHSLEbvrslCUbdfK7zQE6XOKMCDmog8EY7BJnKEfDt2XnfqpJUHUzU4E73GbiclpQaku Tg4K6uNU/bhGIk7eGC9CNZvSIr3C83o9xfZRjoDWP5NULc8j3Qnu1G6L0cHlQsaVk5Zx JVGr/z+wlAwtWhaH4Wp4yWxE08TohLEDwAUw0C/2FeNmjw2EBGxV5jbfm9J5CevJn7TG xPUQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@mailo.com header.s=mailo header.b=ogViOH3N; 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 nb41-20020a1709071ca900b00711da98d1besi4572860ejc.251.2022.06.17.02.32.31; Fri, 17 Jun 2022 02:32:31 -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=ogViOH3N; 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 A415768B8A0; Fri, 17 Jun 2022 12:32:03 +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 9955868B80F for ; Fri, 17 Jun 2022 12:31:54 +0300 (EEST) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=mailo.com; s=mailo; t=1655458310; 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=ogViOH3NwyxhiufI8zIvMfrfsElNUBV0DgmFruH8x8MfCojOA0YIGZCA1BeZtpW9i 0zMtHNI3oGNJojV9+IOEC47iLiOGmQJ6Fm25MhnHQH6kYJjhd/z5mCd6UPUxXNa7bJ CRLhomUK4EuMNAy20GoaVD2QlerF79hbjbDmj0u0= Received: by b-3.in.mailobj.net [192.168.90.13] with ESMTP via ip-206.mailobj.net [213.182.55.206] Fri, 17 Jun 2022 11:31:50 +0200 (CEST) X-EA-Auth: i1nUvlS819I1tscddhfyLTcSC88CytiETwRjm7DFPCoZ3YLZo7VU/JFHj9mMGEBAXyPhpv72sPoMGSGZAxFqbOUV06zdjQMm+OjdHd23Ka4= From: Nil Admirari To: ffmpeg-devel@ffmpeg.org Date: Fri, 17 Jun 2022 12:31:38 +0300 Message-Id: <20220617093141.9826-2-nil-admirari@mailo.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220617093141.9826-1-nil-admirari@mailo.com> References: <20220617093141.9826-1-nil-admirari@mailo.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v17 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: JvECojdLWW9E --- 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).