Skip to content

Commit fe28bbb

Browse files
committed
10.0.22000.0
1 parent faa22ac commit fe28bbb

File tree

4 files changed

+90
-90
lines changed

4 files changed

+90
-90
lines changed

include/tchar.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -643,7 +643,7 @@ typedef wchar_t * PTBYTE;
643643
_Check_return_ __inline size_t __CRTDECL _tclen(_In_z_ const wchar_t *_Cpc)
644644
{
645645
/* avoid compiler warning */
646-
(void *)_Cpc;
646+
(void)_Cpc;
647647
return 1;
648648
}
649649
__inline void __CRTDECL _tccpy(_Out_ wchar_t *_Pc1, _In_z_ const wchar_t *_Cpc2) { *_Pc1 = (wchar_t)*_Cpc2; }

lowio/dup.cpp

Lines changed: 52 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -6,97 +6,90 @@
66
// Defines _dup() and _dup_nolock, which duplicate lowio file handles
77
//
88
#include <corecrt_internal_lowio.h>
9+
#include <corecrt_internal_ptd_propagation.h>
910

11+
static int __cdecl duplicate_osfhnd(int const fh, int const new_fh, __crt_cached_ptd_host& ptd) throw()
12+
{
13+
// Duplicate the file handle:
14+
intptr_t new_osfhandle;
15+
16+
BOOL const result = DuplicateHandle(
17+
GetCurrentProcess(),
18+
reinterpret_cast<HANDLE>(_get_osfhandle(fh)),
19+
GetCurrentProcess(),
20+
&reinterpret_cast<HANDLE&>(new_osfhandle),
21+
0L,
22+
TRUE,
23+
DUPLICATE_SAME_ACCESS);
24+
25+
if (!result)
26+
{
27+
__acrt_errno_map_os_error_ptd(GetLastError(), ptd);
28+
return -1;
29+
}
1030

31+
// Duplicate the handle state:
32+
__acrt_lowio_set_os_handle(new_fh, new_osfhandle);
33+
_osfile(new_fh) = _osfile(fh) & ~FNOINHERIT;
34+
_textmode(new_fh) = _textmode(fh);
35+
_tm_unicode(new_fh) = _tm_unicode(fh);
36+
return new_fh;
37+
}
1138

12-
static int __cdecl dup_nolock(int const fh) throw()
39+
static int __cdecl _dup_nolock_internal(int const fh, __crt_cached_ptd_host& ptd) throw()
1340
{
14-
if (!(_osfile(fh) & FOPEN))
41+
if ((_osfile(fh) & FOPEN) == 0)
42+
{
43+
ptd.get_errno().set(EBADF);
44+
ptd.get_doserrno().set(0);
45+
_ASSERTE(("Invalid file descriptor. File possibly closed by a different thread", 0));
1546
return -1;
47+
}
1648

1749
// Allocate a duplicate handle
18-
int new_fh = _alloc_osfhnd();
50+
int const new_fh = _alloc_osfhnd();
1951
if (new_fh == -1)
2052
{
21-
errno = EMFILE;
22-
_doserrno = 0;
53+
ptd.get_errno().set(EMFILE);
54+
ptd.get_doserrno().set(0);
2355
return -1;
2456
}
2557

26-
bool success = false;
58+
int return_value = -1;
2759
__try
2860
{
29-
// Duplicate the file handle:
30-
intptr_t new_osfhandle;
31-
32-
BOOL const result = DuplicateHandle(
33-
GetCurrentProcess(),
34-
reinterpret_cast<HANDLE>(_get_osfhandle(fh)),
35-
GetCurrentProcess(),
36-
&reinterpret_cast<HANDLE&>(new_osfhandle),
37-
0L,
38-
TRUE,
39-
DUPLICATE_SAME_ACCESS);
40-
41-
if (!result)
42-
{
43-
__acrt_errno_map_os_error(GetLastError());
44-
new_fh = -1;
45-
__leave;
46-
}
47-
48-
// Duplicate the handle state:
49-
__acrt_lowio_set_os_handle(new_fh, new_osfhandle);
50-
_osfile(new_fh) = _osfile(fh) & ~FNOINHERIT;
51-
_textmode(new_fh) = _textmode(fh);
52-
_tm_unicode(new_fh) = _tm_unicode(fh);
53-
success = true;
61+
return_value = duplicate_osfhnd(fh, new_fh, ptd);
5462
}
5563
__finally
5664
{
5765
// The handle returned by _alloc_osfhnd is both open and locked. If we
5866
// failed to duplicate the handle, we need to abandon the handle by
5967
// unsetting the open flag. We always need to unlock the handle:
60-
if (!success)
68+
if (return_value == -1)
6169
{
62-
#pragma warning(disable:__WARNING_BUFFER_UNDERFLOW) // 26001 new_fh can't be -1 here
6370
_osfile(new_fh) &= ~FOPEN;
6471
}
6572

6673
__acrt_lowio_unlock_fh(new_fh);
6774
}
68-
return new_fh;
75+
return return_value;
6976
}
7077

78+
static int __cdecl _dup_internal(int const fh, __crt_cached_ptd_host& ptd)
79+
{
80+
_UCRT_CHECK_FH_CLEAR_OSSERR_RETURN(ptd, fh, EBADF, -1);
81+
_UCRT_VALIDATE_CLEAR_OSSERR_RETURN(ptd, (fh >= 0 && (unsigned)fh < (unsigned)_nhandle), EBADF, -1);
82+
_UCRT_VALIDATE_CLEAR_OSSERR_RETURN(ptd, (_osfile(fh) & FOPEN), EBADF, -1);
7183

84+
return __acrt_lowio_lock_fh_and_call(fh, [&](){
85+
return _dup_nolock_internal(fh, ptd);
86+
});
87+
}
7288

7389
// _dup() duplicates a file handle and returns the duplicate. If the function
7490
// fails, -1 is returned and errno is set.
7591
extern "C" int __cdecl _dup(int const fh)
7692
{
77-
_CHECK_FH_CLEAR_OSSERR_RETURN(fh, EBADF, -1);
78-
_VALIDATE_CLEAR_OSSERR_RETURN((fh >= 0 && (unsigned)fh < (unsigned)_nhandle), EBADF, -1);
79-
_VALIDATE_CLEAR_OSSERR_RETURN((_osfile(fh) & FOPEN), EBADF, -1);
80-
81-
__acrt_lowio_lock_fh(fh);
82-
int result = -1;
83-
__try
84-
{
85-
if (_osfile(fh) & FOPEN)
86-
{
87-
result = dup_nolock(fh);
88-
}
89-
else
90-
{
91-
errno = EBADF;
92-
_doserrno = 0;
93-
_ASSERTE(("Invalid file descriptor. File possibly closed by a different thread",0));
94-
__leave;
95-
}
96-
}
97-
__finally
98-
{
99-
__acrt_lowio_unlock_fh(fh);
100-
}
101-
return result;
93+
__crt_cached_ptd_host ptd;
94+
return _dup_internal(fh, ptd);
10295
}

lowio/dup2.cpp

Lines changed: 36 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,31 +6,22 @@
66
// Defines _dup2() and _dup2_nolock, which duplicate lowio file handles
77
//
88
#include <corecrt_internal_lowio.h>
9+
#include <corecrt_internal_ptd_propagation.h>
910

10-
11-
12-
static int __cdecl dup2_nolock(int const source_fh, int const target_fh) throw()
11+
static int __cdecl _dup2_nolock_internal(int const source_fh, int const target_fh, __crt_cached_ptd_host& ptd) throw()
1312
{
1413
if ((_osfile(source_fh) & FOPEN) == 0)
1514
{
1615
// If the source handle is not open, return an error. Noe that the
1716
// DuplicateHandle API will not detect this error, because it implies
18-
// that _osfhnd(source_fh) == INVALID_HANDLE_VALUE, and this is a
17+
// that _osfhnd(source_fh) == INVALID_HANDLE_VALUE, and this is a
1918
// legal HANDLE value to be duplicated.
20-
errno = EBADF;
21-
_doserrno = 0;
19+
ptd.get_errno().set(EBADF);
20+
ptd.get_doserrno().set(0);
2221
_ASSERTE(("Invalid file descriptor. File possibly closed by a different thread",0));
2322
return -1;
2423
}
2524

26-
// If the target is open, close it first. We ignore the possibility of an
27-
// error here: an error simply means that the OS handle value may remain
28-
// bound for the duration of the process.
29-
if (_osfile(target_fh) & FOPEN)
30-
{
31-
_close_nolock(target_fh);
32-
}
33-
3425
// Duplicate the source file onto the target file:
3526
intptr_t new_osfhandle;
3627

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

4637
if (!result)
4738
{
48-
__acrt_errno_map_os_error(GetLastError());
39+
__acrt_errno_map_os_error_ptd(GetLastError(), ptd);
4940
return -1;
5041
}
5142

43+
// If the target is open, close it once we know the OS handle was duplicated successfully.
44+
// We ignore the possibility of an error here: an error simply means that the OS handle
45+
// value may remain bound for the duration of the process.
46+
if (_osfile(target_fh) & FOPEN)
47+
{
48+
_close_nolock_internal(target_fh, ptd);
49+
}
50+
5251
__acrt_lowio_set_os_handle(target_fh, new_osfhandle);
5352

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

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

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

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

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

10298
__try
10399
{
104-
result = dup2_nolock(source_fh, target_fh);
100+
result = _dup2_nolock_internal(source_fh, target_fh, ptd);
105101
}
106102
__finally
107103
{
@@ -111,3 +107,14 @@ extern "C" int __cdecl _dup2(int const source_fh, int const target_fh)
111107
}
112108
return result;
113109
}
110+
111+
// _dup2() makes the target file handle a duplicate of the source file handle,
112+
// so that both handles refer to the same file. If the target handle is open
113+
// upon entry, it is closed so that it is not leaked.
114+
//
115+
// Returns 0 if successful; returns -1 and sets errno on failure.
116+
extern "C" int __cdecl _dup2(int const source_fh, int const target_fh)
117+
{
118+
__crt_cached_ptd_host ptd;
119+
return _dup2_internal(source_fh, target_fh, ptd);
120+
}

sdk_license.rtf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -499,7 +499,7 @@ l_f42aa342-8706-4288-bd11-ebb85995028c_Enabled}\proptype30{\staticval True}{\pro
499499
\rtlch\fcs1 \af43 \ltrch\fcs0 \b\lang1036\langfe1033\langnp1036\insrsid9780096\charrsid7753709
500500
\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
501501
MICROSOFT SOFTWARE LICENSE TERMS
502-
\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
502+
\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
503503
\hich\af43\dbch\af11\loch\f43
504504
\par }{\rtlch\fcs1 \af43 \ltrch\fcs0 \insrsid5210069\charrsid543100 \hich\af43\dbch\af11\loch\f43 _______________________________________________________________________________________________________
505505
\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

0 commit comments

Comments
 (0)