Skip to content

Commit

Permalink
arch/arm64/src/imx9/imx9_lowputc.c: Fix an arithmetic sign error in d…
Browse files Browse the repository at this point in the history
…ivisor calculation

In the algorithm there is a subtraction (int - unsigned), which results (potentially overflowed)
unsigned.

Passing this to macro ABS and the assigning to int doesn't work ( unsigned is always >= 0 ).

Fix this by replacing (dangerous) ABS macro with stdlib's standard "int abs(int)"
and change the substraction to (int - int).

Signed-off-by: Jukka Laitinen <[email protected]>
  • Loading branch information
Jukka Laitinen committed Jul 8, 2024
1 parent 333a26d commit 1b0d6e1
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 deletions arch/arm64/src/imx9/imx9_lowputc.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
#include <nuttx/config.h>

#include <stdint.h>
#include <fixedmath.h>
#include <stdlib.h>
#include <assert.h>
#include <errno.h>

Expand Down Expand Up @@ -106,8 +106,6 @@
# define IMX9_CONSOLE_2STOP CONFIG_LPUART8_2STOP
#endif

#define ABS(n) (((n) < 0) ? -(n) : (n))

/* Clocking *****************************************************************/

/* Functional clocking is provided via the PCC. The PCC clocking must
Expand Down Expand Up @@ -309,6 +307,7 @@ int imx9_lpuart_configure(uint32_t base, int uartnum,
uint32_t osr;
uint32_t temp_osr;
int temp_diff;
int configured_baud = config->baud;
int calculated_baud;
int baud_diff;
uint32_t regval;
Expand All @@ -330,15 +329,15 @@ int imx9_lpuart_configure(uint32_t base, int uartnum,
* baud_diff iterate through the rest of the supported values of OSR
*/

baud_diff = config->baud;
baud_diff = configured_baud;
osr = 0;
sbr = 0;

for (temp_osr = 4; temp_osr <= 32; temp_osr++)
{
/* Calculate the temporary sbr value */

temp_sbr = (lpuart_freq / (config->baud * temp_osr));
temp_sbr = (lpuart_freq / (configured_baud * temp_osr));

/* Set temp_sbr to 1 if the sourceClockInHz can not satisfy the
* desired baud rate.
Expand All @@ -352,15 +351,15 @@ int imx9_lpuart_configure(uint32_t base, int uartnum,
/* Calculate the baud rate based on the temporary OSR and SBR values */

calculated_baud = (lpuart_freq / (temp_osr * temp_sbr));
temp_diff = ABS(calculated_baud - config->baud);
temp_diff = abs(calculated_baud - configured_baud);

/* Select the better value between srb and (sbr + 1) */

calculated_baud = (lpuart_freq / (temp_osr * (temp_sbr + 1)));
if (temp_diff >
ABS(calculated_baud - config->baud))
abs(calculated_baud - configured_baud))
{
temp_diff = ABS(calculated_baud - config->baud);
temp_diff = abs(calculated_baud - configured_baud);
temp_sbr++;
}

Expand All @@ -372,7 +371,7 @@ int imx9_lpuart_configure(uint32_t base, int uartnum,
}
}

if (baud_diff > ((config->baud * 3) / 100))
if (baud_diff > ((configured_baud * 3) / 100))
{
/* Unacceptable baud rate difference of more than 3% */

Expand Down

0 comments on commit 1b0d6e1

Please sign in to comment.