\r
ASSERT (DevicePath != NULL);\r
\r
+ if (MaxSize == 0) {\r
+ MaxSize = MAX_UINTN;\r
+ }\r
+\r
+ //\r
+ // Validate the input size big enough to touch the first node.\r
+ //\r
+ if (MaxSize < sizeof (EFI_DEVICE_PATH_PROTOCOL)) {\r
+ return FALSE;\r
+ }\r
+\r
for (Count = 0, Size = 0; !IsDevicePathEnd (DevicePath); DevicePath = NextDevicePathNode (DevicePath)) {\r
NodeLength = DevicePathNodeLength (DevicePath);\r
if (NodeLength < sizeof (EFI_DEVICE_PATH_PROTOCOL)) {\r
return FALSE;\r
}\r
\r
- if (MaxSize > 0) {\r
- Size += NodeLength;\r
- if (Size + END_DEVICE_PATH_LENGTH > MaxSize) {\r
- return FALSE;\r
- }\r
+ if (NodeLength > MAX_UINTN - Size) {\r
+ return FALSE;\r
+ }\r
+ Size += NodeLength;\r
+\r
+ //\r
+ // Validate next node before touch it.\r
+ //\r
+ if (Size > MaxSize - END_DEVICE_PATH_LENGTH ) {\r
+ return FALSE;\r
}\r
\r
if (PcdGet32 (PcdMaximumDevicePathNodeCount) > 0) {\r