fix(uart): Fix uart_ll_set_baudrate div-by-zero crash due to uint32_t overflow

Merges https://github.com/espressif/esp-idf/pull/12179
This commit is contained in:
Eduardo Lacerda Campos 2023-09-01 18:33:08 -04:00 committed by Song Ruo Jing
parent e9d442d2b7
commit a2682d4ffb
3 changed files with 14 additions and 5 deletions

View File

@ -10,6 +10,7 @@
#pragma once #pragma once
#include <stdlib.h>
#include "hal/misc.h" #include "hal/misc.h"
#include "hal/uart_types.h" #include "hal/uart_types.h"
#include "soc/uart_periph.h" #include "soc/uart_periph.h"
@ -152,7 +153,9 @@ static inline void uart_ll_set_baudrate(uart_dev_t *hw, uint32_t baud)
#define DIV_UP(a, b) (((a) + (b) - 1) / (b)) #define DIV_UP(a, b) (((a) + (b) - 1) / (b))
uint32_t sclk_freq = uart_ll_get_sclk_freq(hw); uint32_t sclk_freq = uart_ll_get_sclk_freq(hw);
const uint32_t max_div = BIT(12) - 1; // UART divider integer part only has 12 bits const uint32_t max_div = BIT(12) - 1; // UART divider integer part only has 12 bits
int sclk_div = DIV_UP(sclk_freq, max_div * baud); uint32_t sclk_div = DIV_UP(sclk_freq, (uint64_t)max_div * baud);
if (sclk_div == 0) abort();
uint32_t clk_div = ((sclk_freq) << 4) / (baud * sclk_div); uint32_t clk_div = ((sclk_freq) << 4) / (baud * sclk_div);
// The baud rate configuration register is divided into // The baud rate configuration register is divided into

View File

@ -18,6 +18,7 @@
#pragma once #pragma once
#include <stdlib.h>
#include "hal/misc.h" #include "hal/misc.h"
#include "hal/uart_types.h" #include "hal/uart_types.h"
#include "soc/uart_periph.h" #include "soc/uart_periph.h"
@ -160,7 +161,9 @@ static inline void uart_ll_set_baudrate(uart_dev_t *hw, uint32_t baud)
#define DIV_UP(a, b) (((a) + (b) - 1) / (b)) #define DIV_UP(a, b) (((a) + (b) - 1) / (b))
uint32_t sclk_freq = uart_ll_get_sclk_freq(hw); uint32_t sclk_freq = uart_ll_get_sclk_freq(hw);
const uint32_t max_div = BIT(12) - 1; // UART divider integer part only has 12 bits const uint32_t max_div = BIT(12) - 1; // UART divider integer part only has 12 bits
int sclk_div = DIV_UP(sclk_freq, max_div * baud); uint32_t sclk_div = DIV_UP(sclk_freq, (uint64_t)max_div * baud);
if (sclk_div == 0) abort();
uint32_t clk_div = ((sclk_freq) << 4) / (baud * sclk_div); uint32_t clk_div = ((sclk_freq) << 4) / (baud * sclk_div);
// The baud rate configuration register is divided into // The baud rate configuration register is divided into

View File

@ -10,6 +10,7 @@
#pragma once #pragma once
#include <stdlib.h>
#include "hal/misc.h" #include "hal/misc.h"
#include "hal/uart_types.h" #include "hal/uart_types.h"
#include "soc/uart_periph.h" #include "soc/uart_periph.h"
@ -150,7 +151,9 @@ FORCE_INLINE_ATTR void uart_ll_set_baudrate(uart_dev_t *hw, uint32_t baud)
#define DIV_UP(a, b) (((a) + (b) - 1) / (b)) #define DIV_UP(a, b) (((a) + (b) - 1) / (b))
uint32_t sclk_freq = uart_ll_get_sclk_freq(hw); uint32_t sclk_freq = uart_ll_get_sclk_freq(hw);
const uint32_t max_div = BIT(12) - 1; // UART divider integer part only has 12 bits const uint32_t max_div = BIT(12) - 1; // UART divider integer part only has 12 bits
int sclk_div = DIV_UP(sclk_freq, max_div * baud); uint32_t sclk_div = DIV_UP(sclk_freq, (uint64_t)max_div * baud);
if (sclk_div == 0) abort();
uint32_t clk_div = ((sclk_freq) << 4) / (baud * sclk_div); uint32_t clk_div = ((sclk_freq) << 4) / (baud * sclk_div);
// The baud rate configuration register is divided into // The baud rate configuration register is divided into