Skip to content

Add set_value_multistring_nothrow for REG_MULTI_SZ writes#631

Draft
benhillis wants to merge 3 commits intomicrosoft:masterfrom
benhillis:add-set-value-multistring-nothrow
Draft

Add set_value_multistring_nothrow for REG_MULTI_SZ writes#631
benhillis wants to merge 3 commits intomicrosoft:masterfrom
benhillis:add-set-value-multistring-nothrow

Conversation

@benhillis
Copy link
Copy Markdown
Member

Adds set_value_multistring_nothrow() — the missing nothrow counterpart to the existing set_value_multistring().

Two overloads, matching the existing pattern:

HRESULT set_value_multistring_nothrow(HKEY key, PCWSTR subkey, PCWSTR value_name, const std::vector<std::wstring>& data);
HRESULT set_value_multistring_nothrow(HKEY key, PCWSTR value_name, const std::vector<std::wstring>& data);

Uses try/CATCH_RETURN() since get_multistring_from_wstrings allocates internally. Gated behind WIL_USE_STL.

Tests cover round-trips via both overloads, empty arrays, default value names, E_ACCESSDENIED on read-only keys, and cross-verification with the cotaskmem nothrow reader.

Fixes #479

Adds set_value_multistring_nothrow() - the missing nothrow counterpart
to the existing set_value_multistring().

Two overloads matching the existing pattern. Uses try/CATCH_RETURN()
since get_multistring_from_wstrings allocates internally. Gated behind
WIL_USE_STL.

Fixes microsoft#479
@benhillis benhillis force-pushed the add-set-value-multistring-nothrow branch from 8390244 to 98ac5fc Compare April 20, 2026 23:03
@dunhor
Copy link
Copy Markdown
Member

dunhor commented Apr 21, 2026

It feels very weird and unnatural to have a "nothrow" function use exceptions internally -- I'm not aware of any existing place where this is done either. Coupled with the fact that the argument is std::vector<std::wstring>, so the caller has to be at least somewhat aware that exceptions exist.

I get why you are doing it, though. OOM and system errors are two very different classes of errors, and it's nice to have control flow that deals with error codes as opposed to exceptions, so I don't necessarily object to the idea. Curious what other folks think.

@benhillis
Copy link
Copy Markdown
Member Author

not aware of any existing place where this is done either. Coupled with the fact that the argument is std::vector<std::wstring>, so the caller has to be at least somewhat aw

I agree, and that's not the way things are typically done in wil, let me think about this a bit more.

Replace try/CATCH_RETURN() with a truly nothrow implementation that
builds the multi-sz buffer using HeapAlloc + memcpy, matching the
idiomatic wil pattern for nothrow functions.

- Pre-calculate total buffer size with overflow checks
- Guard against size_t-to-DWORD truncation before calling RegSetKeyValueW
- Use HeapAlloc for the temporary buffer (no COM dependency needed)
- Relax preprocessor guard from WIL_USE_STL+WIL_ENABLE_EXCEPTIONS to
  just WIL_USE_STL since exceptions are no longer used internally

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Comment thread include/wil/registry.h Outdated
Comment thread include/wil/registry_helpers.h Outdated
Comment thread include/wil/registry_helpers.h
…, add WI_ASSERT

- Use unique_process_heap_ptr instead of raw HeapAlloc/HeapFree
- Simplify memcpy to copy null terminator with c_str() (size + 1)
- Add WI_ASSERT to verify offset matches expected buffer size

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Function missing wil::reg::set_value_multistring_nothrow

2 participants