]> git.proxmox.com Git - mirror_edk2.git/commitdiff
MdePkg/BaseSafeIntLib: fix undefined behavior in SafeInt64Add()
authorLaszlo Ersek <lersek@redhat.com>
Thu, 15 Feb 2018 13:47:11 +0000 (14:47 +0100)
committerLaszlo Ersek <lersek@redhat.com>
Wed, 21 Feb 2018 10:57:33 +0000 (11:57 +0100)
The addition in the assignment

  SignedResult = Augend + Addend;

is performed with unchecked INT64 operands. According to ISO C, if the
mathematical result of signed integer addition cannot be represented in
the result type, the behavior is undefined. (Refer to ISO C99 6.5p5.
6.2.5p9 only exempts unsigned integers, and 6.3.1.3p3 does not apply
because it treats the conversion of integers that have been successfully
evaluated first.)

Replace the after-the-fact result checking with checks on the operands,
and only perform the addition if it is safe.

Cc: Bret Barkelew <Bret.Barkelew@microsoft.com>
Cc: Liming Gao <liming.gao@intel.com>
Cc: Michael D Kinney <michael.d.kinney@intel.com>
Cc: Sean Brogan <sean.brogan@microsoft.com>
Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Laszlo Ersek <lersek@redhat.com>
Reviewed-by: Ard Biesheuvel <ard.biesheuvel@linaro.org>
Tested-by: Michael D Kinney <michael.d.kinney@intel.com>
MdePkg/Library/BaseSafeIntLib/SafeIntLib.c

index 8e857927b0672e1b022ed9430ce1fe2dd07619c3..56d97cf65601835aacd0d1e79cf2318368f3e9cf 100644 (file)
@@ -3631,26 +3631,62 @@ SafeInt64Add (
   )\r
 {\r
   RETURN_STATUS  Status;\r
-  INT64          SignedResult;\r
 \r
   if (Result == NULL) {\r
     return RETURN_INVALID_PARAMETER;\r
   }\r
 \r
-  SignedResult = Augend + Addend;\r
-\r
   //\r
-  // Adding positive to negative never overflows.\r
-  // If you add two positive numbers, you expect a positive result.\r
-  // If you add two negative numbers, you expect a negative result.\r
-  // Overflow if inputs are the same sign and output is not that sign.\r
+  // * An Addend of zero can never cause underflow or overflow.\r
+  //\r
+  // * A positive Addend can only cause overflow. The overflow condition is\r
+  //\r
+  //     (Augend + Addend) > MAX_INT64\r
+  //\r
+  //   Subtracting Addend from both sides yields\r
+  //\r
+  //     Augend > (MAX_INT64 - Addend)\r
+  //\r
+  //   This condition can be coded directly in C because the RHS will neither\r
+  //   underflow nor overflow. That is due to the starting condition:\r
+  //\r
+  //     0 < Addend <= MAX_INT64\r
+  //\r
+  //   Multiplying all three sides by (-1) yields\r
+  //\r
+  //     0 > (-Addend) >= (-MAX_INT64)\r
+  //\r
+  //   Adding MAX_INT64 to all three sides yields\r
+  //\r
+  //     MAX_INT64 > (MAX_INT64 - Addend) >= 0\r
+  //\r
+  // * A negative Addend can only cause underflow. The underflow condition is\r
+  //\r
+  //     (Augend + Addend) < MIN_INT64\r
+  //\r
+  //   Subtracting Addend from both sides yields\r
+  //\r
+  //     Augend < (MIN_INT64 - Addend)\r
+  //\r
+  //   This condition can be coded directly in C because the RHS will neither\r
+  //   underflow nor overflow. That is due to the starting condition:\r
+  //\r
+  //     MIN_INT64 <= Addend < 0\r
+  //\r
+  //   Multiplying all three sides by (-1) yields\r
+  //\r
+  //     (-MIN_INT64) >= (-Addend) > 0\r
+  //\r
+  //   Adding MIN_INT64 to all three sides yields\r
+  //\r
+  //     0 >= (MIN_INT64 - Addend) > MIN_INT64\r
   //\r
-  if (((Augend < 0) == (Addend < 0))  &&\r
-      ((Augend < 0) != (SignedResult < 0))) {\r
+  if (((Addend > 0) && (Augend > (MAX_INT64 - Addend))) ||\r
+      ((Addend < 0) && (Augend < (MIN_INT64 - Addend)))) {\r
     *Result = INT64_ERROR;\r
     Status = RETURN_BUFFER_TOO_SMALL;\r
   } else {\r
-    *Result = SignedResult;\r
+    *Result = Augend + Addend;\r
     Status = RETURN_SUCCESS;\r
   }\r
 \r