Check if alignment to word boundary would cause an overflow

Bug: 36860231
Test: FMQ unit tests
Change-Id: Ie69bc1baebb10526e831ff7688b670dee4a062bb
Merged-In: Ie69bc1baebb10526e831ff7688b670dee4a062bb
diff --git a/base/include/hidl/MQDescriptor.h b/base/include/hidl/MQDescriptor.h
index bf2f8a4..3ed3685 100644
--- a/base/include/hidl/MQDescriptor.h
+++ b/base/include/hidl/MQDescriptor.h
@@ -120,6 +120,14 @@
         if (kAlignmentSize % __WORDSIZE != 0) {
             details::logAlwaysFatal("Incompatible word size");
         }
+
+        /*
+         * Check if alignment to word boundary would cause an overflow.
+         */
+        if (length > SIZE_MAX - kAlignmentSize/8 + 1) {
+            details::logAlwaysFatal("Queue size too large");
+        }
+
         return (length + kAlignmentSize/8 - 1) & ~(kAlignmentSize/8 - 1U);
     }