From f1ce134b030b82aeb751179eb24efd6e5aff1b04 Mon Sep 17 00:00:00 2001 From: Vardan Mikayelyan Date: Wed, 17 Aug 2016 03:16:41 +0400 Subject: [PATCH] Fix for CORE-11538. --- reactos/drivers/hid/hidparse/hidparse.c | 27 ++++++++++++++++- reactos/sdk/lib/drivers/hidparser/context.c | 6 ++-- reactos/sdk/lib/drivers/hidparser/hidparser.c | 13 ++++++-- reactos/sdk/lib/drivers/hidparser/parser.c | 43 ++++++++------------------- 4 files changed, 52 insertions(+), 37 deletions(-) diff --git a/reactos/drivers/hid/hidparse/hidparse.c b/reactos/drivers/hid/hidparse/hidparse.c index 6daed49..b3002a2 100644 --- a/reactos/drivers/hid/hidparse/hidparse.c +++ b/reactos/drivers/hid/hidparse/hidparse.c @@ -126,6 +126,29 @@ HidP_GetCaps( } NTSTATUS +TranslateStatusForUpperLayer( + IN HIDPARSER_STATUS Status) +{ + // + // now we are handling only this values, for others just return + // status as it is. + // + switch (Status) + { + case HIDPARSER_STATUS_INSUFFICIENT_RESOURCES: + return STATUS_INSUFFICIENT_RESOURCES; + case HIDPARSER_STATUS_INVALID_REPORT_TYPE: + return HIDP_STATUS_INVALID_REPORT_TYPE; + case HIDPARSER_STATUS_BUFFER_TOO_SMALL: + return STATUS_BUFFER_TOO_SMALL; + case HIDPARSER_STATUS_COLLECTION_NOT_FOUND: + return STATUS_NO_DATA_DETECTED; + default: + return Status; + } +} + +NTSTATUS NTAPI HidP_GetCollectionDescription( IN PHIDP_REPORT_DESCRIPTOR ReportDesc, @@ -134,6 +157,7 @@ HidP_GetCollectionDescription( OUT PHIDP_DEVICE_DESC DeviceDescription) { HID_PARSER Parser; + NTSTATUS Status; // // init parser @@ -143,7 +167,8 @@ HidP_GetCollectionDescription( // // get description; // - return HidParser_GetCollectionDescription(&Parser, ReportDesc, DescLength, PoolType, DeviceDescription); + Status = HidParser_GetCollectionDescription(&Parser, ReportDesc, DescLength, PoolType, DeviceDescription); + return TranslateStatusForUpperLayer(Status); } HIDAPI diff --git a/reactos/sdk/lib/drivers/hidparser/context.c b/reactos/sdk/lib/drivers/hidparser/context.c index 3c66b3c..455d9af 100644 --- a/reactos/sdk/lib/drivers/hidparser/context.c +++ b/reactos/sdk/lib/drivers/hidparser/context.c @@ -163,7 +163,7 @@ HidParser_StoreCollection( // // store offset // - TargetCollection->Offsets[Collection->NodeCount + Index] = CurrentOffset; + TargetCollection->Offsets[Collection->ReportCount + Index] = CurrentOffset; // // store sub collections @@ -254,7 +254,7 @@ HidParser_SearchReportInCollection( // // get collection // - SubCollection = (PHID_COLLECTION)(CollectionContext->RawData + Collection->Offsets[Collection->NodeCount + Index]); + SubCollection = (PHID_COLLECTION)(CollectionContext->RawData + Collection->Offsets[Collection->ReportCount + Index]); // // recursively search collection @@ -314,7 +314,7 @@ HidParser_GetCollectionCount( // // get offset to sub collection // - SubCollection = (PHID_COLLECTION)(CollectionContext->RawData + Collection->Offsets[Collection->NodeCount + Index]); + SubCollection = (PHID_COLLECTION)(CollectionContext->RawData + Collection->Offsets[Collection->ReportCount + Index]); // // count collection for sub nodes diff --git a/reactos/sdk/lib/drivers/hidparser/hidparser.c b/reactos/sdk/lib/drivers/hidparser/hidparser.c index ca6be3c..6e9d4ef 100644 --- a/reactos/sdk/lib/drivers/hidparser/hidparser.c +++ b/reactos/sdk/lib/drivers/hidparser/hidparser.c @@ -68,7 +68,7 @@ HidParser_GetCollectionDescription( // failed to parse report descriptor // Parser->Debug("[HIDPARSER] Failed to parse report descriptor with %x\n", ParserStatus); - return TranslateHidParserStatus(ParserStatus); + return ParserStatus; } // @@ -126,7 +126,9 @@ HidParser_GetCollectionDescription( // // no memory // - return TranslateHidParserStatus(ParserStatus); + Parser->Free(DeviceDescription->CollectionDesc); + Parser->Free(DeviceDescription->ReportIDs); + return ParserStatus; } // @@ -153,6 +155,13 @@ HidParser_GetCollectionDescription( // get collection usage page // ParserStatus = HidParser_GetCollectionUsagePage((PVOID)DeviceDescription->CollectionDesc[Index].PreparsedData, &DeviceDescription->CollectionDesc[Index].Usage, &DeviceDescription->CollectionDesc[Index].UsagePage); + if (ParserStatus != HIDPARSER_STATUS_SUCCESS) + { + // collection not found + Parser->Free(DeviceDescription->CollectionDesc); + Parser->Free(DeviceDescription->ReportIDs); + return ParserStatus; + } // // windows seems to prepend the report id, regardless if it is required diff --git a/reactos/sdk/lib/drivers/hidparser/parser.c b/reactos/sdk/lib/drivers/hidparser/parser.c index c314a96..e0208f6 100644 --- a/reactos/sdk/lib/drivers/hidparser/parser.c +++ b/reactos/sdk/lib/drivers/hidparser/parser.c @@ -714,30 +714,6 @@ HidParser_AddMainItem( } HIDPARSER_STATUS -AllocateParserContext( - IN PHID_PARSER Parser, - OUT PHID_PARSER_CONTEXT *OutParserContext) -{ - PHID_PARSER_CONTEXT ParserContext; - - ParserContext = Parser->Alloc(sizeof(HID_PARSER_CONTEXT)); - if (!ParserContext) - { - // - // failed - // - return HIDPARSER_STATUS_INSUFFICIENT_RESOURCES; - } - - // - // store result - // - *OutParserContext = ParserContext; - return HIDPARSER_STATUS_SUCCESS; -} - - -HIDPARSER_STATUS HidParser_ParseReportDescriptor( IN PHID_PARSER Parser, IN PUCHAR ReportDescriptor, @@ -760,12 +736,18 @@ HidParser_ParseReportDescriptor( PMAIN_ITEM_DATA MainItemData; PHID_PARSER_CONTEXT ParserContext; + CurrentOffset = ReportDescriptor; + ReportEnd = ReportDescriptor + ReportLength; + + if (ReportDescriptor >= ReportEnd) + return HIDPARSER_STATUS_COLLECTION_NOT_FOUND; + // // allocate parser // - Status = AllocateParserContext(Parser, &ParserContext); - if (Status != HIDPARSER_STATUS_SUCCESS) - return Status; + ParserContext = Parser->Alloc(sizeof(HID_PARSER_CONTEXT));; + if (!ParserContext) + return HIDPARSER_STATUS_INSUFFICIENT_RESOURCES; // @@ -778,6 +760,7 @@ HidParser_ParseReportDescriptor( // // no memory // + Parser->Free(ParserContext); return HIDPARSER_STATUS_INSUFFICIENT_RESOURCES; } @@ -792,6 +775,7 @@ HidParser_ParseReportDescriptor( // Parser->Free(ParserContext->LocalItemState.UsageStack); ParserContext->LocalItemState.UsageStack = NULL; + Parser->Free(ParserContext); return HIDPARSER_STATUS_INSUFFICIENT_RESOURCES; } @@ -799,8 +783,6 @@ HidParser_ParseReportDescriptor( // start parsing // CurrentCollection = ParserContext->RootCollection; - CurrentOffset = ReportDescriptor; - ReportEnd = ReportDescriptor + ReportLength; do { @@ -1230,8 +1212,7 @@ HidParser_ParseReportDescriptor( // CurrentOffset += CurrentItemSize + sizeof(ITEM_PREFIX); - - }while(CurrentOffset < ReportEnd); + }while (CurrentOffset < ReportEnd); // -- 2.8.2