Reviewed-by: Alexei Fedorov <Alexei.Fedorov@arm.com>

Alexei


From: Sami Mujawar <sami.mujawar@arm.com>
Sent: 23 August 2019 11:55
To: devel@edk2.groups.io <devel@edk2.groups.io>
Cc: Sami Mujawar <Sami.Mujawar@arm.com>; Alexei Fedorov <Alexei.Fedorov@arm.com>; ard.biesheuvel@linaro.org <ard.biesheuvel@linaro.org>; leif.lindholm@linaro.org <leif.lindholm@linaro.org>; Matteo Carlini <Matteo.Carlini@arm.com>; nd <nd@arm.com>
Subject: [PATCH v1 17/19] ArmPlatformPkg: Fix UART divisor warning
 
The VS2017 compiler reports 'warning C4244: '=': conversion
from 'UINT64' to 'UINT32', possible loss of data' for the
calculation of the UART Divisor value.

Fix this warning by adding appropriate typecast and a validation
that ensures that the UART divisor value generated does not exceed
MAX_UINT32.

Signed-off-by: Sami Mujawar <sami.mujawar@arm.com>
---
 ArmPlatformPkg/Library/PL011UartLib/PL011UartLib.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/ArmPlatformPkg/Library/PL011UartLib/PL011UartLib.c b/ArmPlatformPkg/Library/PL011UartLib/PL011UartLib.c
index 801990d9551a638c17d560d4226137b8a3ee47bb..2d3c279cce49304959953ec4a34b50e09a7d0045 100644
--- a/ArmPlatformPkg/Library/PL011UartLib/PL011UartLib.c
+++ b/ArmPlatformPkg/Library/PL011UartLib/PL011UartLib.c
@@ -2,7 +2,7 @@
   Serial I/O Port library functions with no library constructor/destructor
 
   Copyright (c) 2008 - 2010, Apple Inc. All rights reserved.<BR>
-  Copyright (c) 2011 - 2016, ARM Ltd. All rights reserved.<BR>
+  Copyright (c) 2011 - 2019, ARM Ltd. All rights reserved.<BR>
 
   SPDX-License-Identifier: BSD-2-Clause-Patent
 
@@ -78,6 +78,7 @@ PL011UartInitializePort (
   UINT32      Integer;
   UINT32      Fractional;
   UINT32      HardwareFifoDepth;
+  UINT64      DivisorValue;
 
   HardwareFifoDepth = (PL011_UARTPID2_VER (MmioRead32 (UartBase + UARTPID2)) \
                        > PL011_VER_R1P4) \
@@ -188,7 +189,12 @@ PL011UartInitializePort (
       return RETURN_INVALID_PARAMETER;
     }
 
-    Divisor = (UartClkInHz * 4) / *BaudRate;
+    DivisorValue = (((UINT64)UartClkInHz * 4) / *BaudRate);
+    if (DivisorValue > MAX_UINT32) {
+      return RETURN_INVALID_PARAMETER;
+    }
+
+    Divisor = (UINT32)DivisorValue;
     Integer = Divisor >> FRACTION_PART_SIZE_IN_BITS;
     Fractional = Divisor & FRACTION_PART_MASK;
   }
--
'Guid(CE165669-3EF3-493F-B85D-6190EE5B9759)'


IMPORTANT NOTICE: The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Thank you.