Skip to content

Commit

Permalink
Cherry-pick/backport [65cc894ac5c24495|65cc894ac5]: fix off-by-one po…
Browse files Browse the repository at this point in the history
…ssible buffer overrun when looking for encodings; found by coverity

Use GetModuleHandle() in stead of LoadLibrary() for ntdll, which is already loaded by Cygwin.
  • Loading branch information
jan.nijtmans committed May 4, 2017
1 parent 7074a58 commit 144650f
Showing 1 changed file with 12 additions and 15 deletions.
27 changes: 12 additions & 15 deletions unix/tclUnixInit.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
#ifdef HAVE_LANGINFO
# include <langinfo.h>
# ifdef __APPLE__
# if defined(HAVE_WEAK_IMPORT) && MAC_OS_X_VERSION_MIN_REQUIRED < 1030
# if defined(HAVE_WEAK_IMPORT) && MAC_OS_X_VERSION_MIN_REQUIRED < 1030
/* Support for weakly importing nl_langinfo on Darwin. */
# define WEAK_IMPORT_NL_LANGINFO
# define WEAK_IMPORT_NL_LANGINFO
extern char *nl_langinfo(nl_item) WEAK_IMPORT_ATTRIBUTE;
# endif
# endif
# endif
#endif
#include <sys/resource.h>
Expand All @@ -34,7 +34,7 @@

#ifdef __CYGWIN__
DLLIMPORT extern __stdcall unsigned char GetVersionExW(void *);
DLLIMPORT extern __stdcall void *LoadLibraryW(const void *);
DLLIMPORT extern __stdcall void *GetModuleHandleW(const void *);
DLLIMPORT extern __stdcall void FreeLibrary(void *);
DLLIMPORT extern __stdcall void *GetProcAddress(void *, const char *);
DLLIMPORT extern __stdcall void GetSystemInfo(void *);
Expand All @@ -45,12 +45,12 @@ static const char *const platforms[NUMPLATFORMS] = {
};

#define NUMPROCESSORS 11
static const char *const processors[NUMPROCESSORS] = {
static const char *const processors[NUMPROCESSORS] = {
"intel", "mips", "alpha", "ppc", "shx", "arm", "ia64", "alpha64", "msil",
"amd64", "ia32_on_win64"
};

typedef struct _SYSTEM_INFO {
typedef struct {
union {
DWORD dwOemId;
struct {
Expand All @@ -69,7 +69,7 @@ typedef struct _SYSTEM_INFO {
int wProcessorRevision;
} SYSTEM_INFO;

typedef struct _OSVERSIONINFOW {
typedef struct {
DWORD dwOSVersionInfoSize;
DWORD dwMajorVersion;
DWORD dwMinorVersion;
Expand Down Expand Up @@ -666,7 +666,7 @@ SearchKnownEncodings(
int left = 0;
int right = sizeof(localeTable)/sizeof(LocaleTable);

while (left <= right) {
while (left < right) {
int test = (left + right)/2;
int code = strcmp(localeTable[test].lang, encoding);

Expand Down Expand Up @@ -832,7 +832,7 @@ TclpSetVariables(
*/

CFLocaleRef localeRef;

if (&CFLocaleCopyCurrent != NULL && &CFLocaleGetIdentifier != NULL &&
(localeRef = CFLocaleCopyCurrent())) {
CFStringRef locale = CFLocaleGetIdentifier(localeRef);
Expand Down Expand Up @@ -929,16 +929,13 @@ TclpSetVariables(
#ifdef __CYGWIN__
unameOK = 1;
if (!osInfoInitialized) {
HANDLE handle = LoadLibraryW(L"NTDLL");
HANDLE handle = GetModuleHandleW(L"NTDLL");
int(__stdcall *getversion)(void *) =
(int(__stdcall *)(void *))GetProcAddress(handle, "RtlGetVersion");
osInfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFOW);
if (!getversion || getversion(&osInfo)) {
GetVersionExW(&osInfo);
}
if (handle) {
FreeLibrary(handle);
}
osInfoInitialized = 1;
}

Expand Down Expand Up @@ -1130,7 +1127,7 @@ TclpGetCStackParams(
stackGrowsDown = StackGrowsDown(NULL);
}
#endif

/*
* The first time through in a thread: record the "outermost" stack
* frame and inquire with the OS about the stack size.
Expand Down Expand Up @@ -1159,7 +1156,7 @@ TclpGetCStackParams(
if (!stackSize) {
/*
* Stack failure: if we didn't already blow up, we are within the
* safety area. Recheck with the OS in case the stack was grown.
* safety area. Recheck with the OS in case the stack was grown.
*/
result = GetStackSize(&stackSize);
if (result != TCL_OK) {
Expand Down

0 comments on commit 144650f

Please sign in to comment.