@@ -1694,6 +1694,81 @@ size_t FreeRTOS_min_size_t( size_t a,
16941694}
16951695/*-----------------------------------------------------------*/
16961696
1697+ /**
1698+ * @brief Performs a safe addition of two 32-bit integers, preventing overflow and underflow.
1699+ * @param[in] a the first value.
1700+ * @param[in] b the second value.
1701+ * @return The result of a + b if no overflow/underflow occurs, or INT32_MAX/INT32_MIN if overflow/underflow would occur.
1702+ */
1703+ int32_t FreeRTOS_add_int32 ( int32_t a ,
1704+ int32_t b )
1705+ {
1706+ int32_t ret ;
1707+
1708+ if ( ( a > 0 ) && ( b > ipINT32_MAX_VALUE - a ) )
1709+ {
1710+ ret = ipINT32_MAX_VALUE ; /* Positive overflow */
1711+ }
1712+ else if ( ( a < 0 ) && ( b < ipINT32_MIN_VALUE - a ) )
1713+ {
1714+ ret = ipINT32_MIN_VALUE ; /* Negative underflow */
1715+ }
1716+ else
1717+ {
1718+ ret = a + b ;
1719+ }
1720+
1721+ return ret ;
1722+ }
1723+ /*-----------------------------------------------------------*/
1724+
1725+ /**
1726+ * @brief Performs a safe multiplication of two 32-bit integers, preventing overflow and underflow.
1727+ * @param[in] a the first value.
1728+ * @param[in] b the second value.
1729+ * @return The result of a * b if no overflow occurs, or ipINT32_MAX_VALUE if an overflow would occur.
1730+ */
1731+ int32_t FreeRTOS_multiply_int32 ( int32_t a ,
1732+ int32_t b )
1733+ {
1734+ int32_t ret ;
1735+
1736+ /* Check for overflow/underflow */
1737+ if ( a > 0 )
1738+ {
1739+ if ( ( b > 0 ) && ( a > ipINT32_MAX_VALUE / b ) )
1740+ {
1741+ ret = ipINT32_MAX_VALUE ; /* Positive overflow */
1742+ }
1743+ else if ( ( b < 0 ) && ( b < ipINT32_MIN_VALUE / a ) )
1744+ {
1745+ ret = ipINT32_MIN_VALUE ; /* Negative underflow */
1746+ }
1747+ else
1748+ {
1749+ ret = a * b ;
1750+ }
1751+ }
1752+ else
1753+ {
1754+ if ( ( b > 0 ) && ( a < ipINT32_MIN_VALUE / b ) )
1755+ {
1756+ ret = ipINT32_MIN_VALUE ; /* Negative underflow */
1757+ }
1758+ else if ( ( b < 0 ) && ( a < ipINT32_MAX_VALUE / b ) )
1759+ {
1760+ ret = ipINT32_MAX_VALUE ; /* Positive overflow */
1761+ }
1762+ else
1763+ {
1764+ ret = a * b ;
1765+ }
1766+ }
1767+
1768+ return ret ;
1769+ }
1770+ /*-----------------------------------------------------------*/
1771+
16971772/**
16981773 * @brief Round-up a number to a multiple of 'd'.
16991774 * @param[in] a the first value.
0 commit comments