[PR #1105] Clean up some misuses of INVALID_HANDLE_VALUE (fixes #427) #24484

Open
opened 2026-01-31 09:03:34 +00:00 by claunia · 0 comments
Owner

Original Pull Request: https://github.com/microsoft/terminal/pull/1105

State: closed
Merged: Yes


Almost all functions in the Windows API that open or create objects and return HANDLEs to them return null on failure; only a few (mostly to do with the file system) return INVALID_HANDLE_VALUE on failure. This PR scrubs the repo of a few, but not necessarily all, cases where INVALID_HANDLE_VALUE was mistakenly used or tested against instead of null. In particular, it fixes 2 cases reported in issue #427 where the return value of CreateThread() was compared against INVALID_HANDLE_VALUE against null, causing the error handling code to run at the wrong time.

There are a lot of other uses of INVALID_HANDLE_VALUE I found that looked questionable, but which I left alone. Most of these were used to initialize HANDLE-typed variables and as a sentinel to see if those variables remained unset to a "real" value.

PR Checklist

  • Fixes Writing into inactive screen buffer is broken (#427)
  • CLA signed - N/A; I work for Microsoft
  • Tests added/passed - no tests added, existing unit tests pass
  • Requires documentation to be updated - N/A; no doc changes needed
  • I've discussed this with core contributors already. If not checked, I'm ready to accept this work might be rejected in favor of a different grand plan.

References

This PR was inspired by my previous PR #1015 which also resolved some misuses of INVALID_HANDLE_VALUE.

**Original Pull Request:** https://github.com/microsoft/terminal/pull/1105 **State:** closed **Merged:** Yes --- Almost all functions in the Windows API that open or create objects and return HANDLEs to them return null on failure; only a few (mostly to do with the file system) return INVALID_HANDLE_VALUE on failure. This PR scrubs the repo of a few, but not necessarily all, cases where INVALID_HANDLE_VALUE was mistakenly used or tested against instead of null. In particular, it fixes 2 cases reported in issue #427 where the return value of [CreateThread()](https://docs.microsoft.com/windows/desktop/api/processthreadsapi/nf-processthreadsapi-createthread) was compared against INVALID_HANDLE_VALUE against null, causing the error handling code to run at the wrong time. There are a lot of other uses of INVALID_HANDLE_VALUE I found that looked questionable, but which I left alone. Most of these were used to initialize HANDLE-typed variables and as a sentinel to see if those variables remained unset to a "real" value. ### PR Checklist * [x] Fixes #427 * [x] CLA signed - N/A; I work for Microsoft * [x] Tests added/passed - no tests added, existing unit tests pass * [x] Requires documentation to be updated - N/A; no doc changes needed * [ ] I've discussed this with core contributors already. If not checked, I'm ready to accept this work might be rejected in favor of a different grand plan. ### References This PR was inspired by my previous PR #1015 which also resolved some misuses of INVALID_HANDLE_VALUE.
claunia added the pull-request label 2026-01-31 09:03:34 +00:00
Sign in to join this conversation.
No Label pull-request
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/terminal#24484