Author: akhaldi Date: Tue Jan 17 23:42:13 2012 New Revision: 55002
URL: http://svn.reactos.org/svn/reactos?rev=55002&view=rev Log: [PORTCLS] * Fix some memory leaks. * Add some comments. * Fix broken PcNewResourceSublist which ignored the MaximumEntries parameter. * Add more checks.
Modified: trunk/reactos/drivers/wdm/audio/backpln/portcls/resource.cpp
Modified: trunk/reactos/drivers/wdm/audio/backpln/portcls/resource.cpp URL: http://svn.reactos.org/svn/reactos/trunk/reactos/drivers/wdm/audio/backpln/p... ============================================================================== --- trunk/reactos/drivers/wdm/audio/backpln/portcls/resource.cpp [iso-8859-1] (original) +++ trunk/reactos/drivers/wdm/audio/backpln/portcls/resource.cpp [iso-8859-1] Tue Jan 17 23:42:13 2012 @@ -34,11 +34,9 @@ }
IMP_IResourceList; -#ifdef BUILD_WDK - ULONG NTAPI NumberOfEntries(); -#endif - CResourceList(IUnknown * OuterUnknown) : m_OuterUnknown(OuterUnknown), m_PoolType(NonPagedPool), m_TranslatedResourceList(0), m_UntranslatedResourceList(0), m_NumberOfEntries(0) {} - virtual ~CResourceList() {} + + CResourceList(IUnknown * OuterUnknown) : m_OuterUnknown(OuterUnknown), m_PoolType(NonPagedPool), m_TranslatedResourceList(0), m_UntranslatedResourceList(0), m_NumberOfEntries(0), m_MaxEntries(0), m_Ref(0) {} + virtual ~CResourceList();
public: PUNKNOWN m_OuterUnknown; @@ -46,10 +44,24 @@ PCM_RESOURCE_LIST m_TranslatedResourceList; PCM_RESOURCE_LIST m_UntranslatedResourceList; ULONG m_NumberOfEntries; - + ULONG m_MaxEntries; LONG m_Ref; };
+CResourceList::~CResourceList() +{ + if (m_TranslatedResourceList) + { + /* Free resource list */ + FreeItem(m_TranslatedResourceList, TAG_PORTCLASS); + } + + if (m_UntranslatedResourceList) + { + /* Free resource list */ + FreeItem(m_UntranslatedResourceList, TAG_PORTCLASS); + } +}
NTSTATUS NTAPI @@ -75,7 +87,7 @@
return STATUS_UNSUCCESSFUL; } -#if 1 + ULONG NTAPI CResourceList::NumberOfEntries() @@ -84,7 +96,6 @@
return m_NumberOfEntries; } -#endif
ULONG NTAPI @@ -92,38 +103,26 @@ IN CM_RESOURCE_TYPE Type) { ULONG Index, Count = 0; - PCM_PARTIAL_RESOURCE_DESCRIPTOR PartialDescriptor, UnPartialDescriptor; - - PC_ASSERT_IRQL_EQUAL(PASSIVE_LEVEL); - - if (!m_TranslatedResourceList) - { - // no resource list + PCM_PARTIAL_RESOURCE_DESCRIPTOR PartialDescriptor; + + PC_ASSERT_IRQL_EQUAL(PASSIVE_LEVEL); + + /* Is there a resource list? */ + if (!m_UntranslatedResourceList) + { + /* No resource list provided */ return 0; } - PC_ASSERT(m_TranslatedResourceList->List[0].PartialResourceList.Count == m_UntranslatedResourceList->List[0].PartialResourceList.Count); - // I guess the translated and untranslated lists will be same length? - for (Index = 0; Index < m_TranslatedResourceList->List[0].PartialResourceList.Count; Index ++ ) - { - PartialDescriptor = &m_TranslatedResourceList->List[0].PartialResourceList.PartialDescriptors[Index]; - UnPartialDescriptor = &m_UntranslatedResourceList->List[0].PartialResourceList.PartialDescriptors[Index]; - DPRINT("Descriptor Type %u\n", PartialDescriptor->Type); + + for (Index = 0; Index < m_NumberOfEntries; Index ++ ) + { + + /* Get descriptor */ + PartialDescriptor = &m_UntranslatedResourceList->List[0].PartialResourceList.PartialDescriptors[Index]; if (PartialDescriptor->Type == Type) { - // Yay! Finally found one that matches! + /* Yay! Finally found one that matches! */ Count++; - } - - if (PartialDescriptor->Type == CmResourceTypeInterrupt) - { - DPRINT("Index %u TRANS Interrupt Number Affinity %x Level %u Vector %u Flags %x Share %x\n", Index, PartialDescriptor->u.Interrupt.Affinity, PartialDescriptor->u.Interrupt.Level, PartialDescriptor->u.Interrupt.Vector, PartialDescriptor->Flags, PartialDescriptor->ShareDisposition); - DPRINT("Index %u UNTRANS Interrupt Number Affinity %x Level %u Vector %u Flags %x Share %x\n", Index, UnPartialDescriptor->u.Interrupt.Affinity, UnPartialDescriptor->u.Interrupt.Level, UnPartialDescriptor->u.Interrupt.Vector, UnPartialDescriptor->Flags, UnPartialDescriptor->ShareDisposition); - - } - else if (PartialDescriptor->Type == CmResourceTypePort) - { - DPRINT("Index %u TRANS Port Length %u Start %u %u Flags %x Share %x\n", Index, PartialDescriptor->u.Port.Length, PartialDescriptor->u.Port.Start.HighPart, PartialDescriptor->u.Port.Start.LowPart, PartialDescriptor->Flags, PartialDescriptor->ShareDisposition); - DPRINT("Index %u UNTRANS Port Length %u Start %u %u Flags %x Share %x\n", Index, UnPartialDescriptor->u.Port.Length, UnPartialDescriptor->u.Port.Start.HighPart, UnPartialDescriptor->u.Port.Start.LowPart, UnPartialDescriptor->Flags, UnPartialDescriptor->ShareDisposition); } }
@@ -142,27 +141,33 @@
PC_ASSERT_IRQL_EQUAL(PASSIVE_LEVEL);
+ /* Is there a resource list? */ if (!m_TranslatedResourceList) { - // no resource list + /* No resource list */ return NULL; }
- for (DescIndex = 0; DescIndex < m_TranslatedResourceList->List[0].PartialResourceList.Count; DescIndex ++ ) - { + for (DescIndex = 0; DescIndex < m_NumberOfEntries; DescIndex ++ ) + { + /* Get descriptor */ PartialDescriptor = &m_TranslatedResourceList->List[0].PartialResourceList.PartialDescriptors[DescIndex];
if (PartialDescriptor->Type == Type) { - // Yay! Finally found one that matches! + /* Found type, is it the requested index? */ if (Index == Count) { + /* Found */ return PartialDescriptor; } + + /* Need to continue search */ Count++; } }
+ /* No such descriptor */ return NULL; }
@@ -177,26 +182,34 @@
PC_ASSERT_IRQL_EQUAL(PASSIVE_LEVEL);
+ /* Is there a resource list? */ if (!m_UntranslatedResourceList) { - // no resource list + /* Empty resource list */ return NULL; }
- for (DescIndex = 0; DescIndex < m_UntranslatedResourceList->List[0].PartialResourceList.Count; DescIndex ++ ) - { + /* Search descriptors */ + for (DescIndex = 0; DescIndex < m_NumberOfEntries; DescIndex ++ ) + { + /* Get descriptor */ PartialDescriptor = &m_UntranslatedResourceList->List[0].PartialResourceList.PartialDescriptors[DescIndex];
if (PartialDescriptor->Type == Type) { - // Yay! Finally found one that matches! + /* Found type, is it the requested index? */ if (Index == Count) { + /* Found */ return PartialDescriptor; } + + /* Need to continue search */ Count++; } } + + /* No such descriptor */ return NULL; }
@@ -206,68 +219,37 @@ IN PCM_PARTIAL_RESOURCE_DESCRIPTOR Translated, IN PCM_PARTIAL_RESOURCE_DESCRIPTOR Untranslated) { - PCM_RESOURCE_LIST NewUntranslatedResources, NewTranslatedResources; - ULONG NewTranslatedSize, NewUntranslatedSize, TranslatedSize, UntranslatedSize, ResourceCount; - - PC_ASSERT_IRQL_EQUAL(PASSIVE_LEVEL); - - // calculate translated resource list size - ResourceCount = m_TranslatedResourceList->List[0].PartialResourceList.Count; -#ifdef _MSC_VER - NewTranslatedSize = FIELD_OFFSET(CM_RESOURCE_LIST, List[0].PartialResourceList.PartialDescriptors[ResourceCount+1]); - TranslatedSize = FIELD_OFFSET(CM_RESOURCE_LIST, List[0].PartialResourceList.PartialDescriptors[ResourceCount]); -#else - NewTranslatedSize = sizeof(CM_RESOURCE_LIST) - sizeof(CM_PARTIAL_RESOURCE_DESCRIPTOR) + (ResourceCount+1) * sizeof(CM_PARTIAL_RESOURCE_DESCRIPTOR); - TranslatedSize = sizeof(CM_RESOURCE_LIST) - sizeof(CM_PARTIAL_RESOURCE_DESCRIPTOR) + (ResourceCount) * sizeof(CM_PARTIAL_RESOURCE_DESCRIPTOR); -#endif - NewTranslatedResources = (PCM_RESOURCE_LIST)AllocateItem(m_PoolType, NewTranslatedSize, TAG_PORTCLASS); - if (!NewTranslatedResources) - return STATUS_INSUFFICIENT_RESOURCES; - - - // calculate untranslated resouce list size - ResourceCount = m_UntranslatedResourceList->List[0].PartialResourceList.Count; - -#ifdef _MSC_VER - NewUntranslatedSize = FIELD_OFFSET(CM_RESOURCE_LIST, List[0].PartialResourceList.PartialDescriptors[ResourceCount+1]); - UntranslatedSize = FIELD_OFFSET(CM_RESOURCE_LIST, List[0].PartialResourceList.PartialDescriptors[ResourceCount]); -#else - NewUntranslatedSize = sizeof(CM_RESOURCE_LIST) - sizeof(CM_PARTIAL_RESOURCE_DESCRIPTOR) + (ResourceCount+1) * sizeof(CM_PARTIAL_RESOURCE_DESCRIPTOR); - UntranslatedSize = sizeof(CM_RESOURCE_LIST) - sizeof(CM_PARTIAL_RESOURCE_DESCRIPTOR) + (ResourceCount) * sizeof(CM_PARTIAL_RESOURCE_DESCRIPTOR); -#endif - - - // allocate untranslated resource list size - NewUntranslatedResources = (PCM_RESOURCE_LIST)AllocateItem(m_PoolType, NewUntranslatedSize, TAG_PORTCLASS); - if (!NewUntranslatedResources) - { - FreeItem(NewTranslatedResources, TAG_PORTCLASS); - return STATUS_INSUFFICIENT_RESOURCES; - } - - // now copy translated resource list - RtlMoveMemory(NewTranslatedResources, m_TranslatedResourceList, TranslatedSize); - RtlMoveMemory((PUCHAR)NewTranslatedResources + TranslatedSize, Translated, sizeof(CM_PARTIAL_RESOURCE_DESCRIPTOR)); - - // now copy untranslated resource list - RtlMoveMemory(NewUntranslatedResources, m_UntranslatedResourceList, UntranslatedSize); - RtlMoveMemory((PUCHAR)NewUntranslatedResources + UntranslatedSize, Untranslated, sizeof(CM_PARTIAL_RESOURCE_DESCRIPTOR)); - - // free old lists - FreeItem(m_TranslatedResourceList, TAG_PORTCLASS); - FreeItem(m_UntranslatedResourceList, TAG_PORTCLASS); - - // store new lists - m_UntranslatedResourceList = NewUntranslatedResources; - m_TranslatedResourceList = NewTranslatedResources; - - // increment descriptor count - NewUntranslatedResources->List[0].PartialResourceList.Count++; - NewTranslatedResources->List[0].PartialResourceList.Count++; - - // add entry count + PCM_PARTIAL_RESOURCE_DESCRIPTOR PartialDescriptor; + + /* Sanity check */ + PC_ASSERT_IRQL_EQUAL(PASSIVE_LEVEL); + + + /* Is there still room for another entry */ + if (m_NumberOfEntries >= m_MaxEntries) + { + /* No more space */ + return STATUS_INSUFFICIENT_RESOURCES; + } + + /* Get free descriptor */ + PartialDescriptor = &m_UntranslatedResourceList->List[0].PartialResourceList.PartialDescriptors[m_NumberOfEntries]; + + /* Copy descriptor */ + RtlCopyMemory(PartialDescriptor, Untranslated, sizeof(CM_PARTIAL_RESOURCE_DESCRIPTOR)); + + /* Get free descriptor */ + PartialDescriptor = &m_TranslatedResourceList->List[0].PartialResourceList.PartialDescriptors[m_NumberOfEntries]; + + /* Copy descriptor */ + RtlCopyMemory(PartialDescriptor, Translated, sizeof(CM_PARTIAL_RESOURCE_DESCRIPTOR)); + + /* Add entry count */ m_NumberOfEntries++; - + m_UntranslatedResourceList->List[0].PartialResourceList.Count++; + m_TranslatedResourceList->List[0].PartialResourceList.Count++; + + /* Done */ return STATUS_SUCCESS; }
@@ -282,16 +264,18 @@
PC_ASSERT_IRQL_EQUAL(PASSIVE_LEVEL);
+ /* Get entries from parent */ Translated = Parent->FindTranslatedEntry(Type, Index); Untranslated = Parent->FindUntranslatedEntry(Type, Index);
+ /* Are both found? */ if (Translated && Untranslated) { - // add entry from parent + /* Add entry from parent */ return AddEntry(Translated, Untranslated); }
- // entry not found + /* Entry not found */ return STATUS_INVALID_PARAMETER; }
@@ -325,99 +309,92 @@ IN PCM_RESOURCE_LIST UntranslatedResourceList) { PCM_RESOURCE_LIST NewUntranslatedResources, NewTranslatedResources; - ULONG NewTranslatedSize, NewUntranslatedSize, ResourceCount; + ULONG ResourceSize, ResourceCount; CResourceList* NewList; NTSTATUS Status;
if (!TranslatedResourceList) { - // - // if the untranslated resource list is also not provided, it becomes an empty resource list - // + /* If the untranslated resource list is also not provided, it becomes an empty resource list */ if (UntranslatedResourceList) { - // invalid parameter mix + /* Invalid parameter mix */ return STATUS_INVALID_PARAMETER; } } else { - // - // if the translated resource list is also not provided, it becomes an empty resource list - // + /* If the translated resource list is also not provided, it becomes an empty resource list */ if (!UntranslatedResourceList) { - // invalid parameter mix + /* Invalid parameter mix */ return STATUS_INVALID_PARAMETER; } }
+ /* Allocate resource list */ NewList = new(PoolType, TAG_PORTCLASS)CResourceList(OuterUnknown); if (!NewList) return STATUS_INSUFFICIENT_RESOURCES;
+ /* Query resource list */ Status = NewList->QueryInterface(IID_IResourceList, (PVOID*)OutResourceList); - if (!NT_SUCCESS(Status)) { - // - // Ouch, FIX ME - // + /* Ouch, FIX ME */ delete NewList; return STATUS_INVALID_PARAMETER; }
+ /* Is there a resource list */ if (!TranslatedResourceList) { - // - // empty resource list - // + /* Empty resource list */ return STATUS_SUCCESS; }
- // calculate translated resource list size - ResourceCount = TranslatedResourceList->List[0].PartialResourceList.Count; -#ifdef _MSC_VER - NewTranslatedSize = FIELD_OFFSET(CM_RESOURCE_LIST, List[0].PartialResourceList.PartialDescriptors[ResourceCount]); -#else - NewTranslatedSize = sizeof(CM_RESOURCE_LIST) - sizeof(CM_PARTIAL_RESOURCE_DESCRIPTOR) + (ResourceCount) * sizeof(CM_PARTIAL_RESOURCE_DESCRIPTOR); -#endif - - // store resource count - NewList->m_NumberOfEntries = ResourceCount; - - // calculate untranslated resouce list size + /* Sanity check */ + ASSERT(UntranslatedResourceList->List[0].PartialResourceList.Count == TranslatedResourceList->List[0].PartialResourceList.Count); + + /* Get resource count */ ResourceCount = UntranslatedResourceList->List[0].PartialResourceList.Count; #ifdef _MSC_VER - NewUntranslatedSize = FIELD_OFFSET(CM_RESOURCE_LIST, List[0].PartialResourceList.PartialDescriptors[ResourceCount]); + ResourceSize = FIELD_OFFSET(CM_RESOURCE_LIST, List[0].PartialResourceList.PartialDescriptors[ResourceCount]); #else - NewUntranslatedSize = sizeof(CM_RESOURCE_LIST) - sizeof(CM_PARTIAL_RESOURCE_DESCRIPTOR) + (ResourceCount) * sizeof(CM_PARTIAL_RESOURCE_DESCRIPTOR); + ResourceSize = sizeof(CM_RESOURCE_LIST) - sizeof(CM_PARTIAL_RESOURCE_DESCRIPTOR) + (ResourceCount) * sizeof(CM_PARTIAL_RESOURCE_DESCRIPTOR); #endif
- // allocate translated resource list - NewTranslatedResources = (PCM_RESOURCE_LIST)AllocateItem(PoolType, NewTranslatedSize, TAG_PORTCLASS); + /* Allocate translated resource list */ + NewTranslatedResources = (PCM_RESOURCE_LIST)AllocateItem(PoolType, ResourceSize, TAG_PORTCLASS); if (!NewTranslatedResources) { + /* No memory */ delete NewList; return STATUS_INSUFFICIENT_RESOURCES; }
- // allocate untranslated resource list - NewUntranslatedResources = (PCM_RESOURCE_LIST)AllocateItem(PoolType, NewUntranslatedSize, TAG_PORTCLASS); + /* Allocate untranslated resource list */ + NewUntranslatedResources = (PCM_RESOURCE_LIST)AllocateItem(PoolType, ResourceSize, TAG_PORTCLASS); if (!NewUntranslatedResources) { + /* No memory */ delete NewList; - return STATUS_INSUFFICIENT_RESOURCES; - } - - // copy resource lists - RtlCopyMemory(NewTranslatedResources, TranslatedResourceList, NewTranslatedSize); - RtlCopyMemory(NewUntranslatedResources, UntranslatedResourceList, NewUntranslatedSize); - - // store resource lists + FreeItem(NewTranslatedResources, TAG_PORTCLASS); + return STATUS_INSUFFICIENT_RESOURCES; + } + + /* Copy resource lists */ + RtlCopyMemory(NewTranslatedResources, TranslatedResourceList, ResourceSize); + RtlCopyMemory(NewUntranslatedResources, UntranslatedResourceList, ResourceSize); + + /* Init resource list */ NewList->m_TranslatedResourceList= NewTranslatedResources; NewList->m_UntranslatedResourceList = NewUntranslatedResources; - + NewList->m_NumberOfEntries = ResourceCount; + NewList->m_MaxEntries = ResourceCount; + NewList->m_PoolType = PoolType; + + /* Done */ return STATUS_SUCCESS; }
@@ -431,53 +408,60 @@ IN PRESOURCELIST ParentList, IN ULONG MaximumEntries) { - CResourceList* NewList, *Parent; + CResourceList* NewList; + ULONG ResourceSize;
if (!OutResourceList || !ParentList || !MaximumEntries) return STATUS_INVALID_PARAMETER;
- Parent = (CResourceList*)ParentList; - - if (!Parent->m_TranslatedResourceList->List[0].PartialResourceList.Count || - !Parent->m_UntranslatedResourceList->List[0].PartialResourceList.Count) - { - // parent list can't be empty - return STATUS_INVALID_PARAMETER; - } - + /* Allocate new list */ NewList = new(PoolType, TAG_PORTCLASS) CResourceList(OuterUnknown); if (!NewList) return STATUS_INSUFFICIENT_RESOURCES;
+ /* Get resource size */ +#ifdef _MSC_VER + ResourceSize = FIELD_OFFSET(CM_RESOURCE_LIST, List[0].PartialResourceList.PartialDescriptors[MaximumEntries]); +#else + ResourceSize = sizeof(CM_RESOURCE_LIST) - sizeof(CM_PARTIAL_RESOURCE_DESCRIPTOR) + (MaximumEntries) * sizeof(CM_PARTIAL_RESOURCE_DESCRIPTOR); +#endif + + /* Allocate resource list */ NewList->m_TranslatedResourceList = (PCM_RESOURCE_LIST)AllocateItem(PoolType, sizeof(CM_RESOURCE_LIST), TAG_PORTCLASS); if (!NewList->m_TranslatedResourceList) { + /* No memory */ delete NewList; return STATUS_INSUFFICIENT_RESOURCES; }
+ /* Allocate resource list */ NewList->m_UntranslatedResourceList = (PCM_RESOURCE_LIST)AllocateItem(PoolType, sizeof(CM_RESOURCE_LIST), TAG_PORTCLASS); if (!NewList->m_UntranslatedResourceList) { + /* No memory */ delete NewList; return STATUS_INSUFFICIENT_RESOURCES; }
- RtlCopyMemory(NewList->m_TranslatedResourceList, Parent->m_TranslatedResourceList, sizeof(CM_RESOURCE_LIST)); - RtlCopyMemory(NewList->m_UntranslatedResourceList, Parent->m_UntranslatedResourceList, sizeof(CM_RESOURCE_LIST)); - - // mark list as empty + /* Copy resource lists */ + RtlCopyMemory(NewList->m_TranslatedResourceList, ParentList->TranslatedList(), sizeof(CM_RESOURCE_LIST)); + RtlCopyMemory(NewList->m_UntranslatedResourceList, ParentList->UntranslatedList(), sizeof(CM_RESOURCE_LIST)); + + /* Resource list is empty */ + NewList->m_UntranslatedResourceList->List[0].PartialResourceList.Count = 0; NewList->m_TranslatedResourceList->List[0].PartialResourceList.Count = 0; - NewList->m_UntranslatedResourceList->List[0].PartialResourceList.Count = 0; - // store members + + /* Store members */ NewList->m_OuterUnknown = OuterUnknown; NewList->m_PoolType = PoolType; NewList->m_Ref = 1; NewList->m_NumberOfEntries = 0; - + NewList->m_MaxEntries = MaximumEntries; + + /* Store result */ *OutResourceList = (IResourceList*)NewList;
- DPRINT("PcNewResourceSublist OutResourceList %p OuterUnknown %p ParentList %p\n", *OutResourceList, OuterUnknown, ParentList); + /* Done */ return STATUS_SUCCESS; } -