From 0fa1475dfdfa738404cb64731c9be4ad43c29041 Mon Sep 17 00:00:00 2001 From: Timo Kreuzer Date: Sat, 8 Oct 2011 13:50:14 +0000 Subject: [PATCH] [RTL] - Fix possible integer overflow in RtlpInitializeHeapSegment - Simplify loop in RtlIsDosDeviceName_Ustr - Make MonthLengths an array of UCHARs instead of ints - Remove pointless loops in RtlTimeToTimeFields - Fix MSVC warnings svn path=/trunk/; revision=54054 --- reactos/lib/rtl/actctx.c | 2 +- reactos/lib/rtl/heap.c | 4 +-- reactos/lib/rtl/path.c | 9 +++---- reactos/lib/rtl/registry.c | 4 +-- reactos/lib/rtl/time.c | 50 +++++++++++++++----------------------- reactos/lib/rtl/unicode.c | 7 +++--- 6 files changed, 31 insertions(+), 45 deletions(-) diff --git a/reactos/lib/rtl/actctx.c b/reactos/lib/rtl/actctx.c index f883e240dc4..8adf4bec2ff 100644 --- a/reactos/lib/rtl/actctx.c +++ b/reactos/lib/rtl/actctx.c @@ -1794,7 +1794,7 @@ static NTSTATUS get_manifest_in_manifest_file( struct actctx_loader* acl, struct status = NtQueryInformationFile( file, &io, &info, sizeof(info), FileStandardInformation); if (status == STATUS_SUCCESS) - status = parse_manifest(acl, ai, filename, directory, shared, base, info.EndOfFile.QuadPart); + status = parse_manifest(acl, ai, filename, directory, shared, base, (SIZE_T)info.EndOfFile.QuadPart); NtUnmapViewOfSection( NtCurrentProcess(), base ); NtClose( mapping ); diff --git a/reactos/lib/rtl/heap.c b/reactos/lib/rtl/heap.c index bb7953490f0..065efa78b19 100644 --- a/reactos/lib/rtl/heap.c +++ b/reactos/lib/rtl/heap.c @@ -913,9 +913,9 @@ RtlpInitializeHeapSegment(IN OUT PHEAP Heap, /* Initialise the Heap Entries contained within the Heap Segment */ Segment->FirstEntry = &Segment->Entry + Segment->Entry.Size; - Segment->LastValidEntry = (PHEAP_ENTRY) ((ULONG_PTR) (Segment) + SegmentReserve); + Segment->LastValidEntry = (PHEAP_ENTRY)((ULONG_PTR)Segment + SegmentReserve); - if ((Segment->Entry.Size << HEAP_ENTRY_SHIFT) < SegmentCommit) + if (((SIZE_T)Segment->Entry.Size << HEAP_ENTRY_SHIFT) < SegmentCommit) { HeapEntry = Segment->FirstEntry; diff --git a/reactos/lib/rtl/path.c b/reactos/lib/rtl/path.c index 188a4fd154f..778cef4430b 100644 --- a/reactos/lib/rtl/path.c +++ b/reactos/lib/rtl/path.c @@ -47,7 +47,7 @@ RtlIsDosDeviceName_Ustr(IN PUNICODE_STRING PathString) { UNICODE_STRING PathCopy; PWCHAR Start, End; - ULONG PathChars, ColonCount = 0; + USHORT PathChars, ColonCount = 0; USHORT ReturnOffset = 0, ReturnLength; WCHAR c; @@ -94,19 +94,18 @@ RtlIsDosDeviceName_Ustr(IN PUNICODE_STRING PathString) } /* Check for extension or space, and truncate */ - c = PathCopy.Buffer[PathChars - 1]; do { /* Stop if we hit something else than a space or period */ + c = PathCopy.Buffer[PathChars - 1]; if ((c != '.') && (c != ' ')) break; - /* Fixup the lengths and get the next character */ + /* Fixup the lengths */ PathCopy.Length -= sizeof(WCHAR); - if (--PathChars) c = PathCopy.Buffer[PathChars - 1]; /* Remember this for later */ ColonCount++; - } while (PathChars); + } while (--PathChars); /* Anything still left? */ if (PathChars) diff --git a/reactos/lib/rtl/registry.c b/reactos/lib/rtl/registry.c index 56e274abddf..434946f4ed4 100644 --- a/reactos/lib/rtl/registry.c +++ b/reactos/lib/rtl/registry.c @@ -118,7 +118,7 @@ RtlpCallQueryRegistryRoutine(IN PRTL_QUERY_REGISTRY_TABLE QueryTable, { ULONG InfoLength; SIZE_T Length, SpareLength, c; - LONG RequiredLength; + ULONG RequiredLength; PCHAR SpareData, DataEnd; ULONG Type; PWCHAR Name, p, ValueEnd; @@ -341,7 +341,7 @@ RtlpCallQueryRegistryRoutine(IN PRTL_QUERY_REGISTRY_TABLE QueryTable, Status = RtlExpandEnvironmentStrings_U(Environment, &Source, &Destination, - (PULONG)&RequiredLength); + &RequiredLength); Type = REG_SZ; /* Check for success */ diff --git a/reactos/lib/rtl/time.c b/reactos/lib/rtl/time.c index 1b0b6c5e3f7..0a6f68505a7 100644 --- a/reactos/lib/rtl/time.c +++ b/reactos/lib/rtl/time.c @@ -38,14 +38,14 @@ static const int YearLengths[2] = - { - DAYSPERNORMALYEAR, DAYSPERLEAPYEAR - }; -static const int MonthLengths[2][MONSPERYEAR] = - { - { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }, - { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 } - }; +{ + DAYSPERNORMALYEAR, DAYSPERLEAPYEAR +}; +static const UCHAR MonthLengths[2][MONSPERYEAR] = +{ + { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }, + { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 } +}; static __inline int IsLeapYear(int Year) { @@ -76,7 +76,7 @@ RtlCutoverTimeToSystemTime(IN PTIME_FIELDS CutoverTimeFields, TIME_FIELDS CurrentTimeFields; TIME_FIELDS CutoverSystemTimeFields; LARGE_INTEGER CutoverSystemTime; - CSHORT MonthLength; + UCHAR MonthLength; CSHORT Days; BOOLEAN NextYearsCutover = FALSE; @@ -255,32 +255,20 @@ RtlTimeToTimeFields( IN PLARGE_INTEGER Time, OUT PTIME_FIELDS TimeFields) { - const int *Months; - int SecondsInDay, CurYear; - int LeapYear, CurMonth; - long int Days; - LONGLONG IntTime = (LONGLONG)Time->QuadPart; + const UCHAR *Months; + ULONG SecondsInDay, CurYear; + ULONG LeapYear, CurMonth; + ULONG Days; + ULONGLONG IntTime = Time->QuadPart; /* Extract millisecond from time and convert time into seconds */ TimeFields->Milliseconds = (CSHORT) ((IntTime % TICKSPERSEC) / TICKSPERMSEC); IntTime = IntTime / TICKSPERSEC; /* Split the time into days and seconds within the day */ - Days = IntTime / SECSPERDAY; + Days = (ULONG)(IntTime / SECSPERDAY); SecondsInDay = IntTime % SECSPERDAY; - /* Adjust the values for days and seconds in day */ - while (SecondsInDay < 0) - { - SecondsInDay += SECSPERDAY; - Days--; - } - while (SecondsInDay >= SECSPERDAY) - { - SecondsInDay -= SECSPERDAY; - Days++; - } - /* compute time of day */ TimeFields->Hour = (CSHORT) (SecondsInDay / SECSPERHOUR); SecondsInDay = SecondsInDay % SECSPERHOUR; @@ -297,20 +285,20 @@ RtlTimeToTimeFields( while (1) { LeapYear = IsLeapYear(CurYear); - if (Days < (long) YearLengths[LeapYear]) + if (Days < YearLengths[LeapYear]) { break; } CurYear++; - Days = Days - (long) YearLengths[LeapYear]; + Days = Days - YearLengths[LeapYear]; } TimeFields->Year = (CSHORT) CurYear; /* Compute month of year */ LeapYear = IsLeapYear(CurYear); Months = MonthLengths[LeapYear]; - for (CurMonth = 0; Days >= (long) Months[CurMonth]; CurMonth++) - Days = Days - (long) Months[CurMonth]; + for (CurMonth = 0; Days >= Months[CurMonth]; CurMonth++) + Days = Days - Months[CurMonth]; TimeFields->Month = (CSHORT) (CurMonth + 1); TimeFields->Day = (CSHORT) (Days + 1); } diff --git a/reactos/lib/rtl/unicode.c b/reactos/lib/rtl/unicode.c index 9d21b539e78..32de447380f 100644 --- a/reactos/lib/rtl/unicode.c +++ b/reactos/lib/rtl/unicode.c @@ -2495,8 +2495,7 @@ RtlFindCharInUnicodeString( IN PCUNICODE_STRING MatchString, OUT PUSHORT Position) { - SHORT i; - USHORT j; + USHORT i, j; switch (Flags) { @@ -2520,7 +2519,7 @@ RtlFindCharInUnicodeString( case 1: { - for (i = SearchString->Length / sizeof(WCHAR) - 1; i >= 0; i--) + for (i = SearchString->Length / sizeof(WCHAR) - 1; (i + 1) > 0; i--) { for (j = 0; j < MatchString->Length / sizeof(WCHAR); j++) { @@ -2561,7 +2560,7 @@ RtlFindCharInUnicodeString( case 3: { - for (i = SearchString->Length / sizeof(WCHAR) - 1; i >= 0; i--) + for (i = SearchString->Length / sizeof(WCHAR) - 1; (i + 1) > 0; i--) { j = 0;