From b5074f126efb0667e4351798ab8909984a75262b Mon Sep 17 00:00:00 2001 From: Elinor Fung Date: Wed, 28 Jan 2026 16:09:33 -0800 Subject: [PATCH 1/2] Fix length when getting property from host --- src/coreclr/vm/hostinformation.cpp | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/src/coreclr/vm/hostinformation.cpp b/src/coreclr/vm/hostinformation.cpp index d47cfb25b75a63..5e344e637d8653 100644 --- a/src/coreclr/vm/hostinformation.cpp +++ b/src/coreclr/vm/hostinformation.cpp @@ -23,24 +23,37 @@ bool HostInformation::GetProperty(_In_z_ const char* name, SString& value) return false; size_t len = MAX_PATH + 1; - char* dest = value.OpenUTF8Buffer(static_cast(len)); + char* dest = value.OpenUTF8Buffer(static_cast(len) - 1); // OpenUTF8Buffer already includes a byte for the null terminator size_t lenActual = s_hostContract.get_runtime_property(name, dest, len, s_hostContract.context); - value.CloseBuffer(); // Doesn't exist or failed to get property if (lenActual == (size_t)-1 || lenActual == 0) + { + value.CloseBuffer(0); return false; + } if (lenActual <= len) + { + value.CloseBuffer(static_cast(lenActual) - 1); return true; + } + + value.CloseBuffer(); // Buffer was not large enough len = lenActual; - dest = value.OpenUTF8Buffer(static_cast(len)); + dest = value.OpenUTF8Buffer(static_cast(len) - 1); // OpenUTF8Buffer already includes a byte for the null terminator lenActual = s_hostContract.get_runtime_property(name, dest, len, s_hostContract.context); - value.CloseBuffer(); - return lenActual > 0 && lenActual <= len; + if (lenActual == (size_t)-1 || lenActual == 0 || lenActual > len) + { + value.CloseBuffer(0); + return false; + } + + value.CloseBuffer(static_cast(lenActual) - 1); + return true; } bool HostInformation::HasExternalProbe() From edd5c982ddcbdee6ec72016d5a817baa6c49671d Mon Sep 17 00:00:00 2001 From: Elinor Fung Date: Thu, 29 Jan 2026 14:17:25 -0800 Subject: [PATCH 2/2] Update src/coreclr/vm/hostinformation.cpp --- src/coreclr/vm/hostinformation.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/coreclr/vm/hostinformation.cpp b/src/coreclr/vm/hostinformation.cpp index 5e344e637d8653..d1f6e1b925a492 100644 --- a/src/coreclr/vm/hostinformation.cpp +++ b/src/coreclr/vm/hostinformation.cpp @@ -24,6 +24,7 @@ bool HostInformation::GetProperty(_In_z_ const char* name, SString& value) size_t len = MAX_PATH + 1; char* dest = value.OpenUTF8Buffer(static_cast(len) - 1); // OpenUTF8Buffer already includes a byte for the null terminator + // get_runtime_property returns the length including a null terminator size_t lenActual = s_hostContract.get_runtime_property(name, dest, len, s_hostContract.context); // Doesn't exist or failed to get property