Skip to content

Commit

Permalink
10.0.22000.0
Browse files Browse the repository at this point in the history
  • Loading branch information
huangqinjin committed Oct 17, 2021
1 parent faa22ac commit fe28bbb
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 90 deletions.
2 changes: 1 addition & 1 deletion include/tchar.h
Original file line number Diff line number Diff line change
Expand Up @@ -643,7 +643,7 @@ typedef wchar_t * PTBYTE;
_Check_return_ __inline size_t __CRTDECL _tclen(_In_z_ const wchar_t *_Cpc)
{
/* avoid compiler warning */
(void *)_Cpc;
(void)_Cpc;
return 1;
}
__inline void __CRTDECL _tccpy(_Out_ wchar_t *_Pc1, _In_z_ const wchar_t *_Cpc2) { *_Pc1 = (wchar_t)*_Cpc2; }
Expand Down
111 changes: 52 additions & 59 deletions lowio/dup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,97 +6,90 @@
// Defines _dup() and _dup_nolock, which duplicate lowio file handles
//
#include <corecrt_internal_lowio.h>
#include <corecrt_internal_ptd_propagation.h>

static int __cdecl duplicate_osfhnd(int const fh, int const new_fh, __crt_cached_ptd_host& ptd) throw()
{
// Duplicate the file handle:
intptr_t new_osfhandle;

BOOL const result = DuplicateHandle(
GetCurrentProcess(),
reinterpret_cast<HANDLE>(_get_osfhandle(fh)),
GetCurrentProcess(),
&reinterpret_cast<HANDLE&>(new_osfhandle),
0L,
TRUE,
DUPLICATE_SAME_ACCESS);

if (!result)
{
__acrt_errno_map_os_error_ptd(GetLastError(), ptd);
return -1;
}

// Duplicate the handle state:
__acrt_lowio_set_os_handle(new_fh, new_osfhandle);
_osfile(new_fh) = _osfile(fh) & ~FNOINHERIT;
_textmode(new_fh) = _textmode(fh);
_tm_unicode(new_fh) = _tm_unicode(fh);
return new_fh;
}

static int __cdecl dup_nolock(int const fh) throw()
static int __cdecl _dup_nolock_internal(int const fh, __crt_cached_ptd_host& ptd) throw()
{
if (!(_osfile(fh) & FOPEN))
if ((_osfile(fh) & FOPEN) == 0)
{
ptd.get_errno().set(EBADF);
ptd.get_doserrno().set(0);
_ASSERTE(("Invalid file descriptor. File possibly closed by a different thread", 0));
return -1;
}

// Allocate a duplicate handle
int new_fh = _alloc_osfhnd();
int const new_fh = _alloc_osfhnd();
if (new_fh == -1)
{
errno = EMFILE;
_doserrno = 0;
ptd.get_errno().set(EMFILE);
ptd.get_doserrno().set(0);
return -1;
}

bool success = false;
int return_value = -1;
__try
{
// Duplicate the file handle:
intptr_t new_osfhandle;

BOOL const result = DuplicateHandle(
GetCurrentProcess(),
reinterpret_cast<HANDLE>(_get_osfhandle(fh)),
GetCurrentProcess(),
&reinterpret_cast<HANDLE&>(new_osfhandle),
0L,
TRUE,
DUPLICATE_SAME_ACCESS);

if (!result)
{
__acrt_errno_map_os_error(GetLastError());
new_fh = -1;
__leave;
}

// Duplicate the handle state:
__acrt_lowio_set_os_handle(new_fh, new_osfhandle);
_osfile(new_fh) = _osfile(fh) & ~FNOINHERIT;
_textmode(new_fh) = _textmode(fh);
_tm_unicode(new_fh) = _tm_unicode(fh);
success = true;
return_value = duplicate_osfhnd(fh, new_fh, ptd);
}
__finally
{
// The handle returned by _alloc_osfhnd is both open and locked. If we
// failed to duplicate the handle, we need to abandon the handle by
// unsetting the open flag. We always need to unlock the handle:
if (!success)
if (return_value == -1)
{
#pragma warning(disable:__WARNING_BUFFER_UNDERFLOW) // 26001 new_fh can't be -1 here
_osfile(new_fh) &= ~FOPEN;
}

__acrt_lowio_unlock_fh(new_fh);
}
return new_fh;
return return_value;
}

static int __cdecl _dup_internal(int const fh, __crt_cached_ptd_host& ptd)
{
_UCRT_CHECK_FH_CLEAR_OSSERR_RETURN(ptd, fh, EBADF, -1);
_UCRT_VALIDATE_CLEAR_OSSERR_RETURN(ptd, (fh >= 0 && (unsigned)fh < (unsigned)_nhandle), EBADF, -1);
_UCRT_VALIDATE_CLEAR_OSSERR_RETURN(ptd, (_osfile(fh) & FOPEN), EBADF, -1);

return __acrt_lowio_lock_fh_and_call(fh, [&](){
return _dup_nolock_internal(fh, ptd);
});
}

// _dup() duplicates a file handle and returns the duplicate. If the function
// fails, -1 is returned and errno is set.
extern "C" int __cdecl _dup(int const fh)
{
_CHECK_FH_CLEAR_OSSERR_RETURN(fh, EBADF, -1);
_VALIDATE_CLEAR_OSSERR_RETURN((fh >= 0 && (unsigned)fh < (unsigned)_nhandle), EBADF, -1);
_VALIDATE_CLEAR_OSSERR_RETURN((_osfile(fh) & FOPEN), EBADF, -1);

__acrt_lowio_lock_fh(fh);
int result = -1;
__try
{
if (_osfile(fh) & FOPEN)
{
result = dup_nolock(fh);
}
else
{
errno = EBADF;
_doserrno = 0;
_ASSERTE(("Invalid file descriptor. File possibly closed by a different thread",0));
__leave;
}
}
__finally
{
__acrt_lowio_unlock_fh(fh);
}
return result;
__crt_cached_ptd_host ptd;
return _dup_internal(fh, ptd);
}
65 changes: 36 additions & 29 deletions lowio/dup2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,22 @@
// Defines _dup2() and _dup2_nolock, which duplicate lowio file handles
//
#include <corecrt_internal_lowio.h>
#include <corecrt_internal_ptd_propagation.h>



static int __cdecl dup2_nolock(int const source_fh, int const target_fh) throw()
static int __cdecl _dup2_nolock_internal(int const source_fh, int const target_fh, __crt_cached_ptd_host& ptd) throw()
{
if ((_osfile(source_fh) & FOPEN) == 0)
{
// If the source handle is not open, return an error. Noe that the
// DuplicateHandle API will not detect this error, because it implies
// that _osfhnd(source_fh) == INVALID_HANDLE_VALUE, and this is a
// that _osfhnd(source_fh) == INVALID_HANDLE_VALUE, and this is a
// legal HANDLE value to be duplicated.
errno = EBADF;
_doserrno = 0;
ptd.get_errno().set(EBADF);
ptd.get_doserrno().set(0);
_ASSERTE(("Invalid file descriptor. File possibly closed by a different thread",0));
return -1;
}

// If the target is open, close it first. We ignore the possibility of an
// error here: an error simply means that the OS handle value may remain
// bound for the duration of the process.
if (_osfile(target_fh) & FOPEN)
{
_close_nolock(target_fh);
}

// Duplicate the source file onto the target file:
intptr_t new_osfhandle;

Expand All @@ -45,10 +36,18 @@ static int __cdecl dup2_nolock(int const source_fh, int const target_fh) throw()

if (!result)
{
__acrt_errno_map_os_error(GetLastError());
__acrt_errno_map_os_error_ptd(GetLastError(), ptd);
return -1;
}

// If the target is open, close it once we know the OS handle was duplicated successfully.
// We ignore the possibility of an error here: an error simply means that the OS handle
// value may remain bound for the duration of the process.
if (_osfile(target_fh) & FOPEN)
{
_close_nolock_internal(target_fh, ptd);
}

__acrt_lowio_set_os_handle(target_fh, new_osfhandle);

// Copy the _osfile information, with the FNOINHERIT bit cleared:
Expand All @@ -59,30 +58,27 @@ static int __cdecl dup2_nolock(int const source_fh, int const target_fh) throw()
return 0;
}



// _dup2() makes the target file handle a duplicate of the source file handle,
// so that both handles refer to the same file. If the target handle is open
// upon entry, it is closed so that it is not leaked.
//
// Returns 0 if successful; returns -1 and sets errno on failure.
extern "C" int __cdecl _dup2(int const source_fh, int const target_fh)
static int __cdecl _dup2_internal(int const source_fh, int const target_fh, __crt_cached_ptd_host& ptd) throw()
{
_CHECK_FH_CLEAR_OSSERR_RETURN( source_fh, EBADF, -1 );
_VALIDATE_CLEAR_OSSERR_RETURN((source_fh >= 0 && (unsigned)source_fh < (unsigned)_nhandle), EBADF, -1);
_VALIDATE_CLEAR_OSSERR_RETURN((_osfile(source_fh) & FOPEN), EBADF, -1);
_CHECK_FH_CLEAR_OSSERR_RETURN( target_fh, EBADF, -1 );
_VALIDATE_CLEAR_OSSERR_RETURN(((unsigned)target_fh < _NHANDLE_), EBADF, -1);
_UCRT_CHECK_FH_CLEAR_OSSERR_RETURN(ptd, source_fh, EBADF, -1);
_UCRT_VALIDATE_CLEAR_OSSERR_RETURN(ptd, (source_fh >= 0 && (unsigned)source_fh < (unsigned)_nhandle), EBADF, -1);
_UCRT_VALIDATE_CLEAR_OSSERR_RETURN(ptd, (_osfile(source_fh) & FOPEN), EBADF, -1);
_UCRT_CHECK_FH_CLEAR_OSSERR_RETURN(ptd, target_fh, EBADF, -1);
_UCRT_VALIDATE_CLEAR_OSSERR_RETURN(ptd, ((unsigned)target_fh < _NHANDLE_), EBADF, -1);

// Make sure there is an __crt_lowio_handle_data struct corresponding to the target_fh:
if (target_fh >= _nhandle && __acrt_lowio_ensure_fh_exists(target_fh) != 0)
{
return -1;
}

// If the source and target are the same, return success (we've already
// verified that the file handle is open, above). This is for conformance
// with the POSIX specification for dup2.
if (source_fh == target_fh)
{
return 0;
}

// Obtain the two file handle locks. In order to prevent deadlock, we
// always obtain the lock for the lower-numbered file handle first:
Expand All @@ -101,7 +97,7 @@ extern "C" int __cdecl _dup2(int const source_fh, int const target_fh)

__try
{
result = dup2_nolock(source_fh, target_fh);
result = _dup2_nolock_internal(source_fh, target_fh, ptd);
}
__finally
{
Expand All @@ -111,3 +107,14 @@ extern "C" int __cdecl _dup2(int const source_fh, int const target_fh)
}
return result;
}

// _dup2() makes the target file handle a duplicate of the source file handle,
// so that both handles refer to the same file. If the target handle is open
// upon entry, it is closed so that it is not leaked.
//
// Returns 0 if successful; returns -1 and sets errno on failure.
extern "C" int __cdecl _dup2(int const source_fh, int const target_fh)
{
__crt_cached_ptd_host ptd;
return _dup2_internal(source_fh, target_fh, ptd);
}
2 changes: 1 addition & 1 deletion sdk_license.rtf
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,7 @@ l_f42aa342-8706-4288-bd11-ebb85995028c_Enabled}\proptype30{\staticval True}{\pro
\rtlch\fcs1 \af43 \ltrch\fcs0 \b\lang1036\langfe1033\langnp1036\insrsid9780096\charrsid7753709
\par }\pard \ltrpar\ql \li90\ri0\sb120\sa120\widctlpar\wrapdefault\aspalpha\aspnum\faauto\adjustright\rin0\lin90\itap0\pararsid5210069 {\rtlch\fcs1 \af43 \ltrch\fcs0 \b\insrsid9780096\charrsid7753709 \hich\af43\dbch\af11\loch\f43
MICROSOFT SOFTWARE LICENSE TERMS
\par \hich\af43\dbch\af11\loch\f43 MICROSOFT WINDOWS SOFTWARE DEVELOPMENT KIT}{\rtlch\fcs1 \af43 \ltrch\fcs0 \b\insrsid1582711\charrsid7753709 \hich\af43\dbch\af11\loch\f43 (SDK) FOR WINDOWS 10}{\rtlch\fcs1 \af43 \ltrch\fcs0 \b\insrsid9780096\charrsid7753709
\par \hich\af43\dbch\af11\loch\f43 MICROSOFT WINDOWS SOFTWARE DEVELOPMENT KIT}{\rtlch\fcs1 \af43 \ltrch\fcs0 \b\insrsid1582711\charrsid7753709 \hich\af43\dbch\af11\loch\f43 (SDK) FOR WINDOWS}{\rtlch\fcs1 \af43 \ltrch\fcs0 \b\insrsid9780096\charrsid7753709
\hich\af43\dbch\af11\loch\f43
\par }{\rtlch\fcs1 \af43 \ltrch\fcs0 \insrsid5210069\charrsid543100 \hich\af43\dbch\af11\loch\f43 _______________________________________________________________________________________________________
\par }\pard \ltrpar\ql \li90\ri0\sb120\sa120\widctlpar\wrapdefault\aspalpha\aspnum\faauto\adjustright\rin0\lin90\itap0\pararsid5722447 {\rtlch\fcs1 \af43 \ltrch\fcs0 \insrsid9780096\charrsid543100 \hich\af43\dbch\af11\loch\f43
Expand Down

0 comments on commit fe28bbb

Please sign in to comment.