From a1157cef3741984bd3b3d9564defadb35bccd20a Mon Sep 17 00:00:00 2001 From: Bert Belder Date: Wed, 8 Aug 2012 02:24:48 +0200 Subject: [PATCH] windows: don't duplicate invalid stdio handles Ref: joyent/node#3779 --- src/win/process-stdio.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/win/process-stdio.c b/src/win/process-stdio.c index e1b3caf4..ad845a06 100644 --- a/src/win/process-stdio.c +++ b/src/win/process-stdio.c @@ -181,6 +181,20 @@ static int uv__create_stdio_pipe_pair(uv_loop_t* loop, uv_pipe_t* server_pipe, static int uv__duplicate_handle(uv_loop_t* loop, HANDLE handle, HANDLE* dup) { HANDLE current_process; + + /* _get_osfhandle will sometimes return -2 in case of an error. This seems */ + /* to happen when fd <= 2 and the process' corresponding stdio handle is */ + /* set to NULL. Unfortunately DuplicateHandle will happily duplicate /* + /* (HANDLE) -2, so this situation goes unnoticed until someone tries to */ + /* use the duplicate. Therefore we filter out known-invalid handles here. */ + if (handle == INVALID_HANDLE_VALUE || + handle == NULL || + handle == (HANDLE) -2) { + *dup = INVALID_HANDLE_VALUE; + uv__set_artificial_error(loop, UV_EBADF); + return -1; + } + current_process = GetCurrentProcess(); if (!DuplicateHandle(current_process,