fix buffer overrun on mswindows

git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@25973 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
rfm 2008-01-18 13:33:26 +00:00
parent a39a414e68
commit 4788e68beb
2 changed files with 186 additions and 165 deletions

View file

@ -2,6 +2,8 @@
* Tools/autogsdoc.m: Fix bug recording sources and outputs in * Tools/autogsdoc.m: Fix bug recording sources and outputs in
index and dependencies. index and dependencies.
* Source/NSTimeZone.m: Fix coding style errors and buffer overrun
on mswindows reported by Roland Schwingel.
2008-01-14 Nicola Pero <nicola.pero@meta-innovation.com> 2008-01-14 Nicola Pero <nicola.pero@meta-innovation.com>

View file

@ -2117,7 +2117,8 @@ GSBreakTime(NSTimeInterval when, int *year, int *month, int *day,
HKEY regDirKey; HKEY regDirKey;
BOOL isNT = NO, regFound=NO; BOOL isNT = NO, regFound=NO;
/* Open the key in the local machine hive where the time zone data is stored. */ /* Open the key in the local machine hive where
* the time zone data is stored. */
if (ERROR_SUCCESS == RegOpenKeyExW(HKEY_LOCAL_MACHINE, if (ERROR_SUCCESS == RegOpenKeyExW(HKEY_LOCAL_MACHINE,
L"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Time Zones", L"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Time Zones",
0, 0,
@ -2156,7 +2157,6 @@ GSBreakTime(NSTimeInterval when, int *year, int *month, int *day,
DWORD cbMaxValueData; // longest value data DWORD cbMaxValueData; // longest value data
DWORD cbSecurityDescriptor; // size of security descriptor DWORD cbSecurityDescriptor; // size of security descriptor
FILETIME ftLastWriteTime; // last write time FILETIME ftLastWriteTime; // last write time
DWORD i, retCode; DWORD i, retCode;
BOOL tzFound = NO; BOOL tzFound = NO;
@ -2177,16 +2177,19 @@ GSBreakTime(NSTimeInterval when, int *year, int *month, int *day,
if (cSubKeys && (retCode == ERROR_SUCCESS)) if (cSubKeys && (retCode == ERROR_SUCCESS))
{ {
wchar_t *wName = malloc(([name length]+1) * sizeof(wchar_t)); int wLen = [name length];
wchar_t *wName = malloc((wLen+1) * sizeof(wchar_t));
if (wName) if (wName)
{ {
[name getCharacters: wName]; [name getCharacters: wName];
wName[wLen] = 0;
for (i = 0; i < cSubKeys && !tzFound; i++) for (i = 0; i < cSubKeys && !tzFound; i++)
{ {
cbName = 255; cbName = 255;
retCode = RegEnumKeyExW(regDirKey, i, achKey, &cbName, NULL, NULL, NULL, &ftLastWriteTime); retCode = RegEnumKeyExW(regDirKey, i, achKey, &cbName,
NULL, NULL, NULL, &ftLastWriteTime);
if (retCode == ERROR_SUCCESS) if (retCode == ERROR_SUCCESS)
{ {
wchar_t keyBuffer[16384]; wchar_t keyBuffer[16384];
@ -2198,7 +2201,8 @@ GSBreakTime(NSTimeInterval when, int *year, int *month, int *day,
wcscpy(keyBuffer, L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Time Zones\\"); wcscpy(keyBuffer, L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Time Zones\\");
wcscat(keyBuffer, achKey); wcscat(keyBuffer, achKey);
if (ERROR_SUCCESS == RegOpenKeyExW(HKEY_LOCAL_MACHINE, keyBuffer, 0, KEY_READ, &regKey)) if (ERROR_SUCCESS == RegOpenKeyExW(HKEY_LOCAL_MACHINE,
keyBuffer, 0, KEY_READ, &regKey))
{ {
wchar_t buf[256]; wchar_t buf[256];
wchar_t standardName[256]; wchar_t standardName[256];
@ -2209,7 +2213,8 @@ GSBreakTime(NSTimeInterval when, int *year, int *month, int *day,
/* check standardname */ /* check standardname */
standardName[0] = L'\0'; standardName[0] = L'\0';
bufsize = sizeof(buf); bufsize = sizeof(buf);
if (ERROR_SUCCESS==RegQueryValueExW(regKey, L"Std", 0, &type, (BYTE *)buf, &bufsize)) if (ERROR_SUCCESS == RegQueryValueExW(regKey,
L"Std", 0, &type, (BYTE *)buf, &bufsize))
{ {
wcscpy(standardName, buf); wcscpy(standardName, buf);
if (wcscmp(standardName, wName) == 0) if (wcscmp(standardName, wName) == 0)
@ -2219,7 +2224,8 @@ GSBreakTime(NSTimeInterval when, int *year, int *month, int *day,
/* check daylightname */ /* check daylightname */
daylightName[0] = L'\0'; daylightName[0] = L'\0';
bufsize = sizeof(buf); bufsize = sizeof(buf);
if (ERROR_SUCCESS==RegQueryValueExW(regKey, L"Dlt", 0, &type, (BYTE *)buf, &bufsize)) if (ERROR_SUCCESS == RegQueryValueExW(regKey,
L"Dlt", 0, &type, (BYTE *)buf, &bufsize))
{ {
wcscpy(daylightName, buf); wcscpy(daylightName, buf);
if (wcscmp(daylightName, wName) == 0) if (wcscmp(daylightName, wName) == 0)
@ -2230,7 +2236,8 @@ GSBreakTime(NSTimeInterval when, int *year, int *month, int *day,
{ {
/* Read in the time zone data */ /* Read in the time zone data */
bufsize = sizeof(buf); bufsize = sizeof(buf);
if (ERROR_SUCCESS==RegQueryValueExW(regKey, L"TZI", 0, &type, (BYTE *)buf, &bufsize)) if (ERROR_SUCCESS == RegQueryValueExW(regKey,
L"TZI", 0, &type, (BYTE *)buf, &bufsize))
{ {
TZI *tzi = (void*)buf; TZI *tzi = (void*)buf;
Bias = tzi->Bias; Bias = tzi->Bias;
@ -2244,39 +2251,51 @@ GSBreakTime(NSTimeInterval when, int *year, int *month, int *day,
if (wcslen(standardName)) if (wcslen(standardName))
{ {
int a, b; int a, b;
ASSIGN(timeZoneName,[NSString stringWithCharacters:standardName length:wcslen(standardName)]);
/* Abbr generated here is IMHO a bit suspicous but I kept it */ ASSIGN(timeZoneName,
[NSString stringWithCharacters: standardName
length: wcslen(standardName)]);
/* Abbr generated here is IMHO
* a bit suspicous but I kept it */
for (a = 0, b = 0; standardName[a]; a++) for (a = 0, b = 0; standardName[a]; a++)
{ {
if (iswupper(standardName[a])) if (iswupper(standardName[a]))
standardName[b++] = standardName[a]; standardName[b++] = standardName[a];
} }
standardName[b] = L'\0'; standardName[b] = L'\0';
ASSIGN(timeZoneNameAbbr,[NSString stringWithCharacters:standardName length:wcslen(standardName)]); ASSIGN(timeZoneNameAbbr,
[NSString stringWithCharacters: standardName
length: wcslen(standardName)]);
} }
/* Set the daylight savings name for the time zone. */ /* Set the daylight savings name
* for the time zone. */
if (wcslen(daylightName)) if (wcslen(daylightName))
{ {
int a, b; int a, b;
ASSIGN(daylightZoneName,[NSString stringWithCharacters:daylightName length:wcslen(daylightName)]);
/* Abbr generated here is IMHO a bit suspicous but I kept it */ ASSIGN(daylightZoneName,
[NSString stringWithCharacters: daylightName
length: wcslen(daylightName)]);
/* Abbr generated here is IMHO
* a bit suspicous but I kept it */
for (a = 0, b = 0; daylightName[a]; a++) for (a = 0, b = 0; daylightName[a]; a++)
{ {
if (iswupper(daylightName[a])) if (iswupper(daylightName[a]))
daylightName[b++] = daylightName[a]; daylightName[b++] = daylightName[a];
} }
daylightName[b] = L'\0'; daylightName[b] = L'\0';
ASSIGN(daylightZoneNameAbbr,[NSString stringWithCharacters:daylightName length:wcslen(daylightName)]); ASSIGN(daylightZoneNameAbbr,
[NSString stringWithCharacters: daylightName
length: wcslen(daylightName)]);
} }
} }
RegCloseKey(regKey); RegCloseKey(regKey);
} }
} }
} }
free(wName); free(wName);
} }
} }