* introduced PH_FULL_STRING with SIZE_T Length instead of USHORT

* fixed overflow when copying listviews
* PhReAlloc -> PhReAllocate

git-svn-id: svn://svn.code.sf.net/p/processhacker/code@3337 21ef857c-d57f-4fe0-8362-d861dc6d29cd
This commit is contained in:
wj32
2010-07-11 09:46:17 +00:00
parent 3851cf3da9
commit eba8d3f077
16 changed files with 414 additions and 50 deletions
+1
View File
@@ -95,6 +95,7 @@ PPH_STRING PhGetDiagnosticsString()
// basesup
OBJECT_TYPE_COUNT(PhStringType);
OBJECT_TYPE_COUNT(PhAnsiStringType);
OBJECT_TYPE_COUNT(PhFullStringType);
OBJECT_TYPE_COUNT(PhListType);
OBJECT_TYPE_COUNT(PhPointerListType);
OBJECT_TYPE_COUNT(PhQueueType);
+16 -24
View File
@@ -58,12 +58,11 @@ VOID PhpMapDisplayIndexTreeList(
*NumberOfColumns = count;
}
PPH_STRING PhGetProcessTreeListText(
PPH_FULL_STRING PhGetProcessTreeListText(
__in HWND TreeListHandle
)
{
PPH_STRING string;
PH_STRING_BUILDER stringBuilder;
PPH_FULL_STRING string;
ULONG displayToId[PHTLC_MAXIMUM];
ULONG rows;
ULONG columns;
@@ -73,7 +72,7 @@ PPH_STRING PhGetProcessTreeListText(
PhpMapDisplayIndexTreeList(TreeListHandle, displayToId, NULL, &columns);
rows = TreeList_GetVisibleNodeCount(TreeListHandle);
PhInitializeStringBuilder(&stringBuilder, 200);
string = PhCreateFullString2(0x100);
for (i = 0; i < rows; i++)
{
@@ -91,20 +90,17 @@ PPH_STRING PhGetProcessTreeListText(
PhInitializeEmptyStringRef(&getNodeText.Text);
TreeList_GetNodeText(TreeListHandle, &getNodeText);
PhStringBuilderAppendEx(&stringBuilder, getNodeText.Text.Buffer, getNodeText.Text.Length);
PhStringBuilderAppend2(&stringBuilder, L", ");
PhFullStringAppendEx(string, getNodeText.Text.Buffer, getNodeText.Text.Length);
PhFullStringAppend2(string, L", ");
}
// Remove the trailing comma and space.
if (stringBuilder.String->Length != 0)
PhStringBuilderRemove(&stringBuilder, stringBuilder.String->Length / 2 - 2, 2);
if (string->Length != 0)
PhFullStringRemove(string, string->Length / 2 - 2, 2);
PhStringBuilderAppend2(&stringBuilder, L"\r\n");
PhFullStringAppend2(string, L"\r\n");
}
string = PhReferenceStringBuilderString(&stringBuilder);
PhDeleteStringBuilder(&stringBuilder);
return string;
}
@@ -334,12 +330,11 @@ VOID PhpMapDisplayIndexListView(
*NumberOfColumns = count;
}
PPH_STRING PhGetListViewText(
PPH_FULL_STRING PhGetListViewText(
__in HWND ListViewHandle
)
{
PPH_STRING string;
PH_STRING_BUILDER stringBuilder;
PPH_FULL_STRING string;
ULONG displayToId[100];
ULONG rows;
ULONG columns;
@@ -349,7 +344,7 @@ PPH_STRING PhGetListViewText(
PhpMapDisplayIndexListView(ListViewHandle, displayToId, 100, &columns);
rows = ListView_GetItemCount(ListViewHandle);
PhInitializeStringBuilder(&stringBuilder, 200);
string = PhCreateFullString2(0x100);
for (i = 0; i < rows; i++)
{
@@ -368,20 +363,17 @@ PPH_STRING PhGetListViewText(
lvItem.pszText = buffer;
if (ListView_GetItem(ListViewHandle, &lvItem))
PhStringBuilderAppend2(&stringBuilder, buffer);
PhFullStringAppend2(string, buffer);
PhStringBuilderAppend2(&stringBuilder, L", ");
PhFullStringAppend2(string, L", ");
}
// Remove the trailing comma and space.
if (stringBuilder.String->Length != 0)
PhStringBuilderRemove(&stringBuilder, stringBuilder.String->Length / 2 - 2, 2);
if (string->Length != 0)
PhFullStringRemove(string, string->Length / 2 - 2, 2);
PhStringBuilderAppend2(&stringBuilder, L"\r\n");
PhFullStringAppend2(string, L"\r\n");
}
string = PhReferenceStringBuilderString(&stringBuilder);
PhDeleteStringBuilder(&stringBuilder);
return string;
}
+8
View File
@@ -105,6 +105,10 @@ static VOID PhpPrintObjectInfo(
{
wprintf(L"\t%.32S", ((PPH_ANSI_STRING)PhObjectHeaderToObject(ObjectHeader))->Buffer);
}
else if (ObjectHeader->Type == PhFullStringType)
{
wprintf(L"\t%.32s", ((PPH_FULL_STRING)PhObjectHeaderToObject(ObjectHeader))->Buffer);
}
else if (ObjectHeader->Type == PhListType)
{
wprintf(L"\tCount: %u", ((PPH_LIST)PhObjectHeaderToObject(ObjectHeader))->Count);
@@ -163,6 +167,10 @@ static VOID PhpDumpObjectInfo(
{
wprintf(L"%S\n", ((PPH_ANSI_STRING)PhObjectHeaderToObject(ObjectHeader))->Buffer);
}
else if (ObjectHeader->Type == PhFullStringType)
{
wprintf(L"%s\n", ((PPH_FULL_STRING)PhObjectHeaderToObject(ObjectHeader))->Buffer);
}
else if (ObjectHeader->Type == PhHashtableType)
{
PhpPrintHashtableStatistics((PPH_HASHTABLE)PhObjectHeaderToObject(ObjectHeader));
+1 -1
View File
@@ -354,7 +354,7 @@ NTSTATUS PhpEnumHandlesGeneric(
if (count == allocatedCount)
{
allocatedCount *= 2;
convertedHandles = PhReAlloc(
convertedHandles = PhReAllocate(
convertedHandles,
FIELD_OFFSET(SYSTEM_HANDLE_INFORMATION_EX, Handles) +
sizeof(SYSTEM_HANDLE_TABLE_ENTRY_INFO_EX) * allocatedCount
+2 -2
View File
@@ -985,7 +985,7 @@ BOOLEAN PhaChoiceDialog(
// cpysave
PPH_STRING PhGetProcessTreeListText(
PPH_FULL_STRING PhGetProcessTreeListText(
__in HWND TreeListHandle
);
@@ -996,7 +996,7 @@ PPH_LIST PhGetProcessTreeListLines(
__in BOOLEAN UseTabs
);
PPH_STRING PhGetListViewText(
PPH_FULL_STRING PhGetListViewText(
__in HWND ListViewHandle
);
+1 -1
View File
@@ -289,7 +289,7 @@ mxml_set_attr(mxml_node_t *node, /* I - Element node */
if (node->value.element.num_attrs == 0)
attr = PhAllocateSafe(sizeof(mxml_attr_t));
else
attr = PhReAllocSafe(node->value.element.attrs,
attr = PhReAllocateSafe(node->value.element.attrs,
(node->value.element.num_attrs + 1) * sizeof(mxml_attr_t));
if (!attr)
+1 -1
View File
@@ -642,7 +642,7 @@ mxml_add_char(int ch, /* I - Character to add */
else
(*bufsize) += 1024;
if ((newbuffer = PhReAllocSafe(*buffer, *bufsize)) == NULL)
if ((newbuffer = PhReAllocateSafe(*buffer, *bufsize)) == NULL)
{
PhFree(*buffer);
+1 -1
View File
@@ -343,7 +343,7 @@ mxmlIndexNew(mxml_node_t *node, /* I - XML node tree */
if (!ind->alloc_nodes)
temp = PhAllocateSafe(64 * sizeof(mxml_node_t *));
else
temp = PhReAllocSafe(ind->nodes, (ind->alloc_nodes + 64) * sizeof(mxml_node_t *));
temp = PhReAllocateSafe(ind->nodes, (ind->alloc_nodes + 64) * sizeof(mxml_node_t *));
if (!temp)
{
+2 -2
View File
@@ -1493,10 +1493,10 @@ VOID PhSelectAndEnsureVisibleProcessNode(
VOID PhCopyProcessTree()
{
PPH_STRING text;
PPH_FULL_STRING text;
text = PhGetProcessTreeListText(ProcessTreeListHandle);
PhSetClipboardString(ProcessTreeListHandle, &text->sr);
PhSetClipboardStringEx(ProcessTreeListHandle, text->Buffer, text->Length);
PhDereferenceObject(text);
}
+2 -2
View File
@@ -539,10 +539,10 @@ VOID PhCopyListView(
__in HWND ListViewHandle
)
{
PPH_STRING text;
PPH_FULL_STRING text;
text = PhGetListViewText(ListViewHandle);
PhSetClipboardString(ListViewHandle, &text->sr);
PhSetClipboardStringEx(ListViewHandle, text->Buffer, text->Length);
PhDereferenceObject(text);
}
+280 -8
View File
@@ -30,6 +30,11 @@ typedef struct _PHP_BASE_THREAD_CONTEXT
PVOID Parameter;
} PHP_BASE_THREAD_CONTEXT, *PPHP_BASE_THREAD_CONTEXT;
VOID NTAPI PhpFullStringDeleteProcedure(
__in PVOID Object,
__in ULONG Flags
);
VOID NTAPI PhpListDeleteProcedure(
__in PVOID Object,
__in ULONG Flags
@@ -54,6 +59,7 @@ VOID NTAPI PhpHashtableDeleteProcedure(
PPH_OBJECT_TYPE PhStringType;
PPH_OBJECT_TYPE PhAnsiStringType;
PPH_OBJECT_TYPE PhFullStringType;
PPH_OBJECT_TYPE PhListType;
PPH_OBJECT_TYPE PhPointerListType;
PPH_OBJECT_TYPE PhQueueType;
@@ -135,6 +141,18 @@ BOOLEAN PhInitializeBase()
)))
return FALSE;
parameters.FreeListSize = sizeof(PH_FULL_STRING);
parameters.FreeListCount = 32;
if (!NT_SUCCESS(PhCreateObjectTypeEx(
&PhFullStringType,
L"FullString",
PHOBJTYPE_USE_FREE_LIST,
PhpFullStringDeleteProcedure,
&parameters
)))
return FALSE;
parameters.FreeListSize = sizeof(PH_LIST);
parameters.FreeListCount = 128;
@@ -433,7 +451,7 @@ VOID PhFree(
* the block of memory, it raises a
* STATUS_INSUFFICIENT_RESOURCES exception.
*/
__mayRaise PVOID PhReAlloc(
__mayRaise PVOID PhReAllocate(
__in PVOID Memory,
__in SIZE_T Size
)
@@ -461,7 +479,7 @@ __mayRaise PVOID PhReAlloc(
* The existing contents of the memory block are
* copied to the new block.
*/
PVOID PhReAllocSafe(
PVOID PhReAllocateSafe(
__in PVOID Memory,
__in SIZE_T Size
)
@@ -1158,6 +1176,260 @@ PPH_ANSI_STRING PhCreateAnsiStringFromUnicodeEx(
return string;
}
/**
* Creates a string object from an existing
* null-terminated string.
*
* \param Buffer A null-terminated Unicode string.
*/
PPH_FULL_STRING PhCreateFullString(
__in PWSTR Buffer
)
{
SIZE_T length;
length = wcslen(Buffer) * sizeof(WCHAR);
return PhCreateFullStringEx(Buffer, length, length);
}
/**
* Creates a string object.
*
* \param InitialCapacity The number of bytes to allocate
* for the string. This should not include space for a null
* terminator.
*/
PPH_FULL_STRING PhCreateFullString2(
__in SIZE_T InitialCapacity
)
{
return PhCreateFullStringEx(NULL, 0, InitialCapacity);
}
/**
* Creates a string object using a specified length.
*
* \param Buffer A null-terminated Unicode string.
* \param Length The length, in bytes, of the string.
* \param InitialCapacity The number of bytes to allocate
* for the string. This should not include space for a null
* terminator. If the specified value is less than \a Length,
* \a Length bytes are still allocated.
*/
PPH_FULL_STRING PhCreateFullStringEx(
__in_opt PWSTR Buffer,
__in SIZE_T Length,
__in_opt SIZE_T InitialCapacity
)
{
PPH_FULL_STRING string;
if (!NT_SUCCESS(PhCreateObject(
&string,
sizeof(PH_FULL_STRING), // null terminator
0,
PhFullStringType,
0
)))
return NULL;
if (InitialCapacity < Length)
InitialCapacity = Length;
string->Length = Length;
string->AllocatedLength = InitialCapacity;
string->Buffer = PhAllocate(string->AllocatedLength + sizeof(WCHAR));
string->Buffer[Length / sizeof(WCHAR)] = 0;
if (Buffer)
{
memcpy(string->Buffer, Buffer, Length);
}
return string;
}
VOID NTAPI PhpFullStringDeleteProcedure(
__in PVOID Object,
__in ULONG Flags
)
{
PPH_FULL_STRING string = (PPH_FULL_STRING)Object;
PhFree(string->Buffer);
}
FORCEINLINE VOID PhpWriteFullStringNullTerminator(
__in PPH_FULL_STRING String
)
{
String->Buffer[String->Length / sizeof(WCHAR)] = 0;
}
/**
* Resizes a string object.
*
* \param String A string object.
* \param NewLength The new required length of the string object.
* This should not include space for a null terminator.
* \param Growing TRUE to use sizing logic for growing strings,
* otherwise FALSE to resize to the exact specified length.
*/
VOID PhResizeFullString(
__inout PPH_FULL_STRING String,
__in SIZE_T NewLength,
__in BOOLEAN Growing
)
{
if (Growing)
{
assert(NewLength >= String->AllocatedLength);
// Double the string size. If that still isn't
// enough room, just use the new length.
String->AllocatedLength *= 2;
if (String->AllocatedLength < NewLength)
String->AllocatedLength = NewLength;
}
else
{
String->AllocatedLength = NewLength;
// This check only applies when we're shortening the string.
if (String->Length > String->AllocatedLength)
String->Length = String->AllocatedLength;
}
// Resize the buffer.
String->Buffer = PhReAllocate(String->Buffer, String->AllocatedLength + sizeof(WCHAR));
// Make sure we have a null terminator.
PhpWriteFullStringNullTerminator(String);
}
/**
* Appends a string to the end of a string.
*
* \param String A string object.
* \param ShortString The string to append.
*/
VOID PhFullStringAppend(
__inout PPH_FULL_STRING String,
__in PPH_STRING ShortString
)
{
PhFullStringAppendEx(
String,
ShortString->Buffer,
ShortString->Length
);
}
/**
* Appends a string to the end of a string.
*
* \param String A string object.
* \param StringZ The string to append.
*/
VOID PhFullStringAppend2(
__inout PPH_FULL_STRING String,
__in PWSTR StringZ
)
{
PhFullStringAppendEx(
String,
StringZ,
wcslen(StringZ) * sizeof(WCHAR)
);
}
/**
* Appends a string to the end of a string.
*
* \param String A string object.
* \param Buffer The string to append.
* \param Length The number of bytes to append.
*/
VOID PhFullStringAppendEx(
__inout PPH_FULL_STRING String,
__in PWSTR Buffer,
__in SIZE_T Length
)
{
if (Length == 0)
return;
// Resize the string is necessary.
if (String->AllocatedLength < String->Length + Length)
PhResizeFullString(String, String->Length + Length, TRUE);
memcpy(
&String->Buffer[String->Length / sizeof(WCHAR)],
Buffer,
Length
);
String->Length += Length;
PhpWriteFullStringNullTerminator(String);
}
/**
* Appends a character to the end of a string.
*
* \param String A string object.
* \param Character The character to append.
*/
VOID PhFullStringAppendChar(
__inout PPH_FULL_STRING String,
__in WCHAR Character
)
{
if (String->AllocatedLength < String->Length + sizeof(WCHAR))
PhResizeFullString(String, String->Length + sizeof(WCHAR), TRUE);
String->Buffer[String->Length / sizeof(WCHAR)] = Character;
String->Length += sizeof(WCHAR);
PhpWriteFullStringNullTerminator(String);
}
/**
* Appends a formatted string to the end of a string.
*
* \param String A string object.
* \param Format The format-control string.
*/
VOID PhFullStringAppendFormat(
__inout PPH_FULL_STRING String,
__in __format_string PWSTR Format,
...
)
{
va_list argptr;
PPH_STRING string;
va_start(argptr, Format);
string = PhFormatString_V(Format, argptr);
PhFullStringAppend(String, string);
PhDereferenceObject(string);
}
VOID PhFullStringRemove(
__inout PPH_FULL_STRING String,
__in SIZE_T StartIndex,
__in SIZE_T Count
)
{
// Overwrite the removed part with the part behind it.
memmove(
&String->Buffer[StartIndex],
&String->Buffer[StartIndex + Count],
String->Length - (Count + StartIndex) * sizeof(WCHAR)
);
String->Length -= Count * sizeof(WCHAR);
PhpWriteFullStringNullTerminator(String);
}
/**
* Initializes a string builder object.
*
@@ -1495,7 +1767,7 @@ VOID PhStringBuilderRemove(
// Overwrite the removed part with the part
// behind it.
memcpy(
memmove(
&StringBuilder->String->Buffer[StartIndex],
&StringBuilder->String->Buffer[StartIndex + Count],
StringBuilder->String->Length - (Count + StartIndex) * sizeof(WCHAR)
@@ -1561,7 +1833,7 @@ VOID PhAddListItem(
if (List->Count == List->AllocatedCount)
{
List->AllocatedCount *= 2;
List->Items = PhReAlloc(List->Items, List->AllocatedCount * sizeof(PVOID));
List->Items = PhReAllocate(List->Items, List->AllocatedCount * sizeof(PVOID));
}
List->Items[List->Count++] = Item;
@@ -1588,7 +1860,7 @@ VOID PhAddListItems(
if (List->AllocatedCount < List->Count + Count)
List->AllocatedCount = List->Count + Count;
List->Items = PhReAlloc(List->Items, List->AllocatedCount * sizeof(PVOID));
List->Items = PhReAllocate(List->Items, List->AllocatedCount * sizeof(PVOID));
}
memcpy(
@@ -1677,7 +1949,7 @@ VOID PhInsertListItems(
if (List->AllocatedCount < List->Count + Count)
List->AllocatedCount = List->Count + Count;
List->Items = PhReAlloc(List->Items, List->AllocatedCount * sizeof(PVOID));
List->Items = PhReAllocate(List->Items, List->AllocatedCount * sizeof(PVOID));
}
if (Index < List->Count)
@@ -1907,7 +2179,7 @@ HANDLE PhAddPointerListItem(
if (PointerList->NextEntry == PointerList->AllocatedCount)
{
PointerList->AllocatedCount *= 2;
PointerList->Items = PhReAlloc(PointerList->Items, PointerList->AllocatedCount * sizeof(PVOID));
PointerList->Items = PhReAllocate(PointerList->Items, PointerList->AllocatedCount * sizeof(PVOID));
}
index = PointerList->NextEntry++;
@@ -2262,7 +2534,7 @@ VOID PhpResizeHashtable(
// Re-allocate the entries.
Hashtable->AllocatedEntries = Hashtable->AllocatedBuckets;
Hashtable->Entries = PhReAlloc(
Hashtable->Entries = PhReAllocate(
Hashtable->Entries,
PH_HASHTABLE_ENTRY_SIZE(Hashtable->EntrySize) * Hashtable->AllocatedEntries
);
+12 -3
View File
@@ -807,15 +807,24 @@ VOID PhSetClipboardString(
__in HWND hWnd,
__in PPH_STRINGREF String
)
{
PhSetClipboardStringEx(hWnd, String->Buffer, String->Length);
}
VOID PhSetClipboardStringEx(
__in HWND hWnd,
__in PWSTR Buffer,
__in SIZE_T Length
)
{
HANDLE data;
PVOID memory;
data = GlobalAlloc(GMEM_MOVEABLE, String->Length + 2);
data = GlobalAlloc(GMEM_MOVEABLE, Length + 2);
memory = GlobalLock(data);
memcpy(memory, String->Buffer, String->Length);
*(PWCHAR)((PCHAR)memory + String->Length) = 0;
memcpy(memory, Buffer, Length);
*(PWCHAR)((PCHAR)memory + Length) = 0;
GlobalUnlock(memory);
+79 -3
View File
@@ -158,12 +158,12 @@ VOID PhFree(
__in __post_invalid PVOID Memory
);
__mayRaise PVOID PhReAlloc(
__mayRaise PVOID PhReAllocate(
__in PVOID Memory,
__in SIZE_T Size
);
PVOID PhReAllocSafe(
PVOID PhReAllocateSafe(
__in PVOID Memory,
__in SIZE_T Size
);
@@ -1251,6 +1251,82 @@ PPH_ANSI_STRING PhCreateAnsiStringFromUnicodeEx(
__in SIZE_T Length
);
// full string
#ifndef BASESUP_PRIVATE
extern PPH_OBJECT_TYPE PhFullStringType;
#endif
/**
* A full Unicode string object.
*
* \remarks This string object is similar to PH_STRING except
* that the length is not restricted to 16 bits. Unlike
* PH_STRING and PH_ANSI_STRING, this object is mutable.
*/
typedef struct _PH_FULL_STRING
{
/** The length, in bytes, of the string. */
SIZE_T Length;
/** The allocated length of the string, in bytes, not including the null terminator. */
SIZE_T AllocatedLength;
/** The buffer containing the contents of the string. */
PWSTR Buffer;
} PH_FULL_STRING, *PPH_FULL_STRING;
PPH_FULL_STRING PhCreateFullString(
__in PWSTR Buffer
);
PPH_FULL_STRING PhCreateFullString2(
__in SIZE_T InitialCapacity
);
PPH_FULL_STRING PhCreateFullStringEx(
__in_opt PWSTR Buffer,
__in SIZE_T Length,
__in_opt SIZE_T InitialCapacity
);
VOID PhResizeFullString(
__inout PPH_FULL_STRING String,
__in SIZE_T NewLength,
__in BOOLEAN Growing
);
VOID PhFullStringAppend(
__inout PPH_FULL_STRING String,
__in PPH_STRING ShortString
);
VOID PhFullStringAppend2(
__inout PPH_FULL_STRING String,
__in PWSTR StringZ
);
VOID PhFullStringAppendEx(
__inout PPH_FULL_STRING String,
__in PWSTR Buffer,
__in SIZE_T Length
);
VOID PhFullStringAppendChar(
__inout PPH_FULL_STRING String,
__in WCHAR Character
);
VOID PhFullStringAppendFormat(
__inout PPH_FULL_STRING String,
__in __format_string PWSTR Format,
...
);
VOID PhFullStringRemove(
__inout PPH_FULL_STRING String,
__in SIZE_T StartIndex,
__in SIZE_T Count
);
// stringbuilder
/**
@@ -1261,7 +1337,7 @@ PPH_ANSI_STRING PhCreateAnsiStringFromUnicodeEx(
*/
typedef struct _PH_STRING_BUILDER
{
/** Allocated length of the string. */
/** Allocated length of the string, not including the null terminator. */
ULONG AllocatedLength;
/**
* The constructed string.
+6
View File
@@ -431,6 +431,12 @@ VOID PhSetClipboardString(
__in PPH_STRINGREF String
);
VOID PhSetClipboardStringEx(
__in HWND hWnd,
__in PWSTR Buffer,
__in SIZE_T Length
);
#define PH_ANCHOR_LEFT 0x1
#define PH_ANCHOR_TOP 0x2
#define PH_ANCHOR_RIGHT 0x4
+1 -1
View File
@@ -862,7 +862,7 @@ __mayRaise VOID PhaDereferenceObject(
if (autoPool->DynamicCount == autoPool->DynamicAllocated)
{
autoPool->DynamicAllocated *= 2;
autoPool->DynamicObjects = PhReAlloc(
autoPool->DynamicObjects = PhReAllocate(
autoPool->DynamicObjects,
sizeof(PVOID) * autoPool->DynamicAllocated
);
+1 -1
View File
@@ -689,7 +689,7 @@ LRESULT CALLBACK PhpTreeListWndProc(
if (context->Columns)
{
context->Columns = PhReAlloc(
context->Columns = PhReAllocate(
context->Columns,
context->AllocatedColumns * sizeof(PPH_TREELIST_COLUMN)
);