Merge pull request #1738 from openmv/fix_unsigned_minmax

imlib: Fix possible signed/unsigned comparisons issues.
This commit is contained in:
Ibrahim Abdelkader 2022-10-16 15:16:32 +02:00 committed by GitHub
commit 580fea18ec
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 29 additions and 16 deletions

View File

@ -18,8 +18,8 @@
#include "fb_alloc.h"
#include "gc.h"
#define MAX_ROW (480)
#define MAX_CORNERS (2000)
#define MAX_ROW (480u)
#define MAX_CORNERS (2000u)
#define Compare(X, Y) ((X)>=(Y))
typedef struct {

View File

@ -56,7 +56,7 @@ static void bin_up(uint16_t *hist, uint16_t size, unsigned int max_size, uint16_
end = i;
}
int bin_count = end - start + 1; // >= 1
uint16_t bin_count = end - start + 1; // >= 1
*new_size = IM_MIN(max_size, bin_count);
*new_hist = xalloc0((*new_size) * sizeof(uint16_t));
float div_value = (*new_size) / ((float) bin_count); // Reversed so we can multiply below.
@ -74,7 +74,7 @@ static void merge_bins(int b_dst_start, int b_dst_end, uint16_t **b_dst_hist, ui
int start = IM_MIN(b_dst_start, b_src_start);
int end = IM_MAX(b_dst_end, b_src_end);
int bin_count = end - start + 1; // >= 1
uint16_t bin_count = end - start + 1; // >= 1
uint16_t new_size = IM_MIN(max_size, bin_count);
uint16_t *new_hist = xalloc0(new_size * sizeof(uint16_t));
float div_value = new_size / ((float) bin_count); // Reversed so we can multiply below.

View File

@ -316,8 +316,8 @@ void Interpolate (kz_pixel_t * pImage, int uiXRes, unsigned long * pulMapLU,
void imlib_clahe_histeq(image_t *img, float clip_limit, image_t *mask)
{
int xTileSize = IM_MAX(uiMAX_REG_X >> (10 - IM_MIN(IM_LOG2_32(img->w), 10)), 2);
int yTileSize = IM_MAX(uiMAX_REG_Y >> (10 - IM_MIN(IM_LOG2_32(img->h), 10)), 2);
int xTileSize = IM_MAX(uiMAX_REG_X >> (10 - IM_MIN(IM_LOG2_32(img->w), 10)), 2u);
int yTileSize = IM_MAX(uiMAX_REG_Y >> (10 - IM_MIN(IM_LOG2_32(img->h), 10)), 2u);
int pImageW = img->w + ((img->w % xTileSize) ? (xTileSize - (img->w % xTileSize)) : 0);
int pImageH = img->h + ((img->h % yTileSize) ? (yTileSize - (img->h % yTileSize)) : 0);
int xOffset = (pImageW - img->w) / 2;

View File

@ -37,8 +37,8 @@
#ifdef IMLIB_ENABLE_FAST
#define MAX_ROW (480)
#define MAX_CORNERS (2000)
#define MAX_ROW (480U)
#define MAX_CORNERS (2000U)
#define Compare(X, Y) ((X)>=(Y))
typedef struct {

View File

@ -628,7 +628,7 @@ void imlib_image_operation(image_t *img, const char *path, image_t *other, int s
// case we shrink the window size to how many lines we're buffering.
temp.pixels = alloc;
// Set the max buffer height to image height.
temp.h = IM_MIN(img->h, (size / (temp.w * temp.bpp)));
temp.h = IM_MIN((uint32_t) img->h, (size / (temp.w * temp.bpp)));
// This should never happen unless someone forgot to free.
if ((!temp.pixels) || (!temp.h)) {
mp_raise_msg(&mp_type_MemoryError, MP_ERROR_TEXT("Not enough memory available!"));

View File

@ -42,8 +42,21 @@
#define IM_LOG2_32(x) (((x) & 0xFFFF0000ULL) ? (16 + IM_LOG2_16((x) >> 16)) : IM_LOG2_16(x)) // NO ({ ... }) !
#define IM_LOG2(x) (((x) & 0xFFFFFFFF00000000ULL) ? (32 + IM_LOG2_32((x) >> 32)) : IM_LOG2_32(x)) // NO ({ ... }) !
#define IM_MAX(a,b) ({ __typeof__ (a) _a = (a); __typeof__ (b) _b = (b); _a > _b ? _a : _b; })
#define IM_MIN(a,b) ({ __typeof__ (a) _a = (a); __typeof__ (b) _b = (b); _a < _b ? _a : _b; })
#define IM_IS_SIGNED(a) (__builtin_types_compatible_p(__typeof__(a), signed) || \
__builtin_types_compatible_p(__typeof__(a), signed long))
#define IM_IS_UNSIGNED(a) (__builtin_types_compatible_p(__typeof__(a), unsigned) || \
__builtin_types_compatible_p(__typeof__(a), unsigned long))
#define IM_SIGN_COMPARE(a, b) ((IM_IS_SIGNED(a) && IM_IS_UNSIGNED(b)) || \
(IM_IS_SIGNED(b) && IM_IS_UNSIGNED(a)))
#define IM_MAX(a, b)\
({__typeof__ (a) _a = (a); __typeof__ (b) _b = (b);\
__builtin_choose_expr(IM_SIGN_COMPARE(_a, _b), (void) 0, (_a > _b ? _a : _b)); })
#define IM_MIN(a, b)\
({__typeof__ (a) _a = (a); __typeof__ (b) _b = (b);\
__builtin_choose_expr(IM_SIGN_COMPARE(_a, _b), (void) 0, (_a < _b ? _a : _b)); })
#define IM_DIV(a,b) ({ __typeof__ (a) _a = (a); __typeof__ (b) _b = (b); _b ? (_a / _b) : 0; })
#define IM_MOD(a,b) ({ __typeof__ (a) _a = (a); __typeof__ (b) _b = (b); _b ? (_a % _b) : 0; })
@ -486,8 +499,8 @@ struct { \
#endif
typedef struct image {
uint32_t w;
uint32_t h;
int32_t w;
int32_t h;
PIXFORMAT_STRUCT;
union {
uint8_t *pixels;

View File

@ -274,8 +274,8 @@ static void spi_lcd_callback(SPI_HandleTypeDef *hspi)
}
case SPI_TX_CB_MEMORY_WRITE: {
uint16_t *addr = spi_tx_cb_state_memory_write_addr;
size_t count = IM_MIN(spi_tx_cb_state_memory_write_count, (65536-8));
spi_tx_cb_state = (spi_tx_cb_state_memory_write_count > (65536-8)) ? SPI_TX_CB_MEMORY_WRITE : SPI_TX_CB_DISPLAY_ON;
size_t count = IM_MIN(spi_tx_cb_state_memory_write_count, (65536-8u));
spi_tx_cb_state = (spi_tx_cb_state_memory_write_count > (65536-8u)) ? SPI_TX_CB_MEMORY_WRITE : SPI_TX_CB_DISPLAY_ON;
spi_tx_cb_state_memory_write_addr += count;
spi_tx_cb_state_memory_write_count -= count;
if (spi_tx_cb_state_memory_write_first) {

View File

@ -580,7 +580,7 @@ static void spi_tv_callback(SPI_HandleTypeDef *hspi)
}
case SPI_TX_CB_MEMORY_WRITE: {
uint8_t *addr = spi_tx_cb_state_memory_write_addr;
size_t count = IM_MIN(spi_tx_cb_state_memory_write_count, (65536-16));
size_t count = IM_MIN(spi_tx_cb_state_memory_write_count, (65536-16u));
spi_tx_cb_state = (spi_tx_cb_state_memory_write_count > (65536-16))
? SPI_TX_CB_MEMORY_WRITE
: SPI_TX_CB_MEMORY_WRITE_CMD;