Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove strtok, strtok_s, wcstok_s PAL APIs #98008

Merged
merged 11 commits into from
Feb 7, 2024
13 changes: 11 additions & 2 deletions src/coreclr/md/compiler/emit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1951,11 +1951,20 @@ STDMETHODIMP RegMeta::DefineDocument( // S_OK or error.
*partsIndexesPtr++ = 0;
partsIndexesCount++;
}
stringToken = strtok(docName, (const char*)delim);
char* context;
#ifdef HOST_WINDOWS
stringToken = strtok_s(docName, (const char*)delim, &context);
#else
stringToken = strtok_r(docName, (const char*)delim, &context);
#endif
while (stringToken != NULL)
{
IfFailGo(m_pStgdb->m_MiniMd.m_BlobHeap.AddBlob(MetaData::DataBlob((BYTE*)stringToken, (ULONG)strlen(stringToken)), partsIndexesPtr++));
stringToken = strtok(NULL, (const char*)delim);
#ifdef HOST_WINDOWS
stringToken = strtok_s(NULL, (const char*)delim, &context);
#else
stringToken = strtok_r(NULL, (const char*)delim, &context);
#endif
partsIndexesCount++;
}

Expand Down
4 changes: 1 addition & 3 deletions src/coreclr/pal/inc/mbusafecrt.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,7 @@ extern errno_t wcscpy_s( WCHAR* outDest, size_t inDestBufferSize, const WCHAR* i

extern errno_t strncpy_s( char* outDest, size_t inDestBufferSize, const char* inSrc, size_t inCount );
extern errno_t wcsncpy_s( WCHAR* outDest, size_t inDestBufferSize, const WCHAR* inSrc, size_t inCount );

extern char* strtok_s( char* inString, const char* inControl, char** ioContext );
extern WCHAR* wcstok_s( WCHAR* inString, const WCHAR* inControl, WCHAR** ioContext );
extern errno_t wcsncpy_s( WCHAR* outDest, size_t inDestBufferSize, const WCHAR* inSrc, size_t inCount );

// strnlen is not required unless the source string is completely untrusted (e.g. anonymous input on a website)
#ifndef SUPPRESS_STRNLEN
Expand Down
3 changes: 1 addition & 2 deletions src/coreclr/pal/inc/pal.h
Original file line number Diff line number Diff line change
Expand Up @@ -3982,7 +3982,6 @@ PAL_GetCurrentThreadAffinitySet(SIZE_T size, UINT_PTR* data);
#define exit PAL_exit
#define realloc PAL_realloc
#define fopen PAL_fopen
#define strtok PAL_strtok
#define strtoul PAL_strtoul
#define strtoull PAL_strtoull
#define fprintf PAL_fprintf
Expand Down Expand Up @@ -4083,7 +4082,7 @@ PALIMPORT char * __cdecl strchr(const char *, int);
PALIMPORT char * __cdecl strrchr(const char *, int);
PALIMPORT char * __cdecl strpbrk(const char *, const char *);
PALIMPORT char * __cdecl strstr(const char *, const char *);
PALIMPORT char * __cdecl strtok(char *, const char *);
PALIMPORT char * __cdecl strtok_r(char *, const char *, char **);
PALIMPORT int __cdecl atoi(const char *);
PALIMPORT ULONG __cdecl strtoul(const char *, char **, int);
PALIMPORT ULONGLONG __cdecl strtoull(const char *, char **, int);
Expand Down
149 changes: 0 additions & 149 deletions src/coreclr/pal/inc/rt/safecrt.h
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,6 @@ void __cdecl _invalid_parameter(const WCHAR *_Message, const WCHAR *_FunctionNam
#define _tcsncat_s strncat_s
#define _tcsset_s _strset_s
#define _tcsnset_s _strnset_s
#define _tcstok_s strtok_s
#define _vsntprintf_s _vsnprintf_s

#elif defined(_UNICODE) || defined(UNICODE)
Expand All @@ -381,7 +380,6 @@ void __cdecl _invalid_parameter(const WCHAR *_Message, const WCHAR *_FunctionNam
#define _tcsncat_s wcsncat_s
#define _tcsset_s _wcsset_s
#define _tcsnset_s _wcsnset_s
#define _tcstok_s wcstok_s
#define _tmakepath_s _wmakepath_s
#define _stprintf_s swprintf_s
#define _tscanf_s wscanf_s
Expand Down Expand Up @@ -1118,153 +1116,6 @@ errno_t __cdecl _wcsnset_s(WCHAR *_Dst, size_t _SizeInWords, WCHAR _Value, size_

#endif

/* strtok_s */
/*
* strtok_s, wcstok_s ;
* uses _Context to keep track of the position in the string.
*/
_SAFECRT__EXTERN_C
char * __cdecl strtok_s(char *_String, const char *_Control, char **_Context);

#if _SAFECRT_USE_INLINES || _SAFECRT_IMPL

_SAFECRT__INLINE
char * __cdecl strtok_s(char *_String, const char *_Control, char **_Context)
{
unsigned char *str;
const unsigned char *ctl = (const unsigned char *)_Control;
unsigned char map[32];
int count;

/* validation section */
_SAFECRT__VALIDATE_POINTER_ERROR_RETURN(_Context, EINVAL, nullptr);
_SAFECRT__VALIDATE_POINTER_ERROR_RETURN(_Control, EINVAL, nullptr);
_SAFECRT__VALIDATE_CONDITION_ERROR_RETURN(_String != nullptr || *_Context != nullptr, EINVAL, nullptr);

/* Clear control map */
for (count = 0; count < 32; count++)
{
map[count] = 0;
}

/* Set bits in delimiter table */
do {
map[*ctl >> 3] |= (1 << (*ctl & 7));
} while (*ctl++);

/* If string is nullptr, set str to the saved
* pointer (i.e., continue breaking tokens out of the string
* from the last strtok call) */
if (_String != nullptr)
{
str = (unsigned char *)_String;
}
else
{
str = (unsigned char *)*_Context;
}

/* Find beginning of token (skip over leading delimiters). Note that
* there is no token iff this loop sets str to point to the terminal
* null (*str == 0) */
while ((map[*str >> 3] & (1 << (*str & 7))) && *str != 0)
{
str++;
}

_String = (char *)str;

/* Find the end of the token. If it is not the end of the string,
* put a null there. */
for ( ; *str != 0 ; str++ )
{
if (map[*str >> 3] & (1 << (*str & 7)))
{
*str++ = 0;
break;
}
}

/* Update context */
*_Context = (char *)str;

/* Determine if a token has been found. */
if (_String == (char *)str)
{
return nullptr;
}
else
{
return _String;
}
}
#endif

/* wcstok_s */
_SAFECRT__EXTERN_C
WCHAR * __cdecl wcstok_s(WCHAR *_String, const WCHAR *_Control, WCHAR **_Context);

#if _SAFECRT_USE_INLINES || _SAFECRT_IMPL

_SAFECRT__INLINE
WCHAR * __cdecl wcstok_s(WCHAR *_String, const WCHAR *_Control, WCHAR **_Context)
{
WCHAR *token;
const WCHAR *ctl;

/* validation section */
_SAFECRT__VALIDATE_POINTER_ERROR_RETURN(_Context, EINVAL, nullptr);
_SAFECRT__VALIDATE_POINTER_ERROR_RETURN(_Control, EINVAL, nullptr);
_SAFECRT__VALIDATE_CONDITION_ERROR_RETURN(_String != nullptr || *_Context != nullptr, EINVAL, nullptr);

/* If string==nullptr, continue with previous string */
if (!_String)
{
_String = *_Context;
}

/* Find beginning of token (skip over leading delimiters). Note that
* there is no token iff this loop sets string to point to the terminal null. */
for ( ; *_String != 0 ; _String++)
{
for (ctl = _Control; *ctl != 0 && *ctl != *_String; ctl++)
;
if (*ctl == 0)
{
break;
}
}

token = _String;

/* Find the end of the token. If it is not the end of the string,
* put a null there. */
for ( ; *_String != 0 ; _String++)
{
for (ctl = _Control; *ctl != 0 && *ctl != *_String; ctl++)
;
if (*ctl != 0)
{
*_String++ = 0;
break;
}
}

/* Update the context */
*_Context = _String;

/* Determine if a token has been found. */
if (token == _String)
{
return nullptr;
}
else
{
return token;
}
}
#endif

#ifndef PAL_STDCPP_COMPAT
/* strnlen */
/*
Expand Down
3 changes: 0 additions & 3 deletions src/coreclr/pal/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,6 @@ set(SOURCES
cruntime/misc.cpp
cruntime/printfcpp.cpp
cruntime/string.cpp
cruntime/stringtls.cpp
cruntime/thread.cpp
cruntime/wchar.cpp
debug/debug.cpp
Expand Down Expand Up @@ -187,15 +186,13 @@ set(SOURCES
safecrt/strlen_s.cpp
safecrt/strncat_s.cpp
safecrt/strncpy_s.cpp
safecrt/strtok_s.cpp
safecrt/vsprintf.cpp
safecrt/wcscat_s.cpp
safecrt/wcscpy_s.cpp
safecrt/wcslen_s.cpp
safecrt/wcslwr_s.cpp
safecrt/wcsncat_s.cpp
safecrt/wcsncpy_s.cpp
safecrt/wcstok_s.cpp
safecrt/wmakepath_s.cpp
safecrt/xtoa_s.cpp
safecrt/xtow_s.cpp
Expand Down
77 changes: 0 additions & 77 deletions src/coreclr/pal/src/cruntime/stringtls.cpp

This file was deleted.

3 changes: 2 additions & 1 deletion src/coreclr/pal/src/include/pal/palinternal.h
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ function_name() to call the system's implementation
#define strrchr DUMMY_strrchr
#define strpbrk DUMMY_strpbrk
#define strtod DUMMY_strtod
#define strtok_r DUMMY_strtok_r
#define tolower DUMMY_tolower
#define toupper DUMMY_toupper
#define isprint DUMMY_isprint
Expand Down Expand Up @@ -375,7 +376,7 @@ function_name() to call the system's implementation
#undef strtoul
#undef strtoull
#undef strtod
#undef strtok
#undef strtok_r
#undef strdup
#undef tolower
#undef toupper
Expand Down
14 changes: 0 additions & 14 deletions src/coreclr/pal/src/include/pal/thread.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,19 +159,6 @@ namespace CorUnix
};
#endif // HAVE_MACH_EXCEPTIONS

class CThreadCRTInfo : public CThreadInfoInitializer
{
public:
CHAR * strtokContext; // Context for strtok function
WCHAR * wcstokContext; // Context for wcstok function

CThreadCRTInfo() :
strtokContext(NULL),
wcstokContext(NULL)
{
};
};

class CPalThread
{
friend
Expand Down Expand Up @@ -330,7 +317,6 @@ namespace CorUnix
CThreadSynchronizationInfo synchronizationInfo;
CThreadSuspensionInfo suspensionInfo;
CThreadApcInfo apcInfo;
CThreadCRTInfo crtInfo;

CPalThread()
:
Expand Down
Loading
Loading