sal/osl/w32/process.cxx | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-)
New commits: commit 11f10c48688436129337ffc7a082a56023c58071 Author: Mike Kaganski <mike.kagan...@collabora.com> AuthorDate: Sat Mar 29 21:50:47 2025 +0100 Commit: Mike Kaganski <mike.kagan...@collabora.com> CommitDate: Sun Mar 30 08:55:37 2025 +0200 Use GetEnvironmentVariableW a bit more correctly 1. Make the buffer size equal to the documented max environment variable length plus one. 2. Check that the function result is not larger than the bufer size, which would indicate "buffer too small" 3. Use the returned length to avoid finding the length again. While at this: drop the obsolete comment about large buffer again. Before commit f78a2bcce88dd5c12052ae3e55c561cdd48b05fe, the comment told about a problem in unicows library (the Win9x implementation of W-functions). The restored comment only told about unspecified problem in GetEnvironmentVariableW. Since we don't support Win9x (and unicows), the comment can be removed. The size of the buffer is not a problem here, so just let it be. Change-Id: If57a37a9c35aca2c08543619aa4aba6faac79045 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/183495 Tested-by: Jenkins Reviewed-by: Mike Kaganski <mike.kagan...@collabora.com> diff --git a/sal/osl/w32/process.cxx b/sal/osl/w32/process.cxx index 40df459105d9..756ae5f4dbac 100644 --- a/sal/osl/w32/process.cxx +++ b/sal/osl/w32/process.cxx @@ -426,20 +426,13 @@ void SAL_CALL osl_setCommandArgs (int argc, char ** argv) } } -/* TODO because of an issue with GetEnvironmentVariableW we have to - allocate a buffer large enough to hold the requested environment - variable instead of testing for the required size. This wastes - some stack space, maybe we should revoke this work around if - this is no longer a problem */ -#define ENV_BUFFER_SIZE (32*1024-1) - oslProcessError SAL_CALL osl_getEnvironment(rtl_uString *ustrVar, rtl_uString **ustrValue) { - WCHAR buff[ENV_BUFFER_SIZE]; - - if (GetEnvironmentVariableW(o3tl::toW(ustrVar->buffer), buff, ENV_BUFFER_SIZE) > 0) + WCHAR buff[32 * 1024]; + DWORD len = GetEnvironmentVariableW(o3tl::toW(ustrVar->buffer), buff, std::size(buff)); + if (len > 0 && len < std::size(buff)) { - rtl_uString_newFromStr(ustrValue, o3tl::toU(buff)); + rtl_uString_newFromStr_WithLength(ustrValue, o3tl::toU(buff), len); return osl_Process_E_None; } return osl_Process_E_Unknown;