Make the MIN and MAX macros typesafe

This commit is contained in:
Kovid Goyal 2019-07-01 17:03:47 +05:30
parent f49f0d19f9
commit 6a31909557
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
13 changed files with 35 additions and 31 deletions

View File

@ -27,7 +27,7 @@ static inline bool
ensure_space(GlobalData *global, size_t sz) {
if (global->output_sz < sz + global->output_pos || !global->output) {
size_t before = global->output_sz;
global->output_sz += MAX(sz, (64 * 1024));
global->output_sz += MAX(sz, (64u * 1024u));
global->output = realloc(global->output, sizeof(text_t) * global->output_sz);
if (!global->output) {
global->output_sz = before;

View File

@ -1361,7 +1361,7 @@ read_from_peer(ChildMonitor *self, int s) {
if (rd->fd == s) {
if (rd->used >= rd->capacity) {
if (rd->capacity >= 1024 * 1024) failed("Ignoring too large message from peer");
rd->capacity = MAX(8192, rd->capacity * 2);
rd->capacity = MAX(8192u, rd->capacity * 2);
rd->data = realloc(rd->data, rd->capacity);
if (!rd->data) failed("Out of memory");
}

View File

@ -272,7 +272,7 @@ set_configured_colors(ColorProfile *self, PyObject *args) {
void
copy_color_table_to_buffer(ColorProfile *self, color_type *buf, int offset, size_t stride) {
size_t i;
stride = MAX(1, stride);
stride = MAX(1u, stride);
for (i = 0, buf = buf + offset; i < sizeof(self->color_table)/sizeof(self->color_table[0]); i++, buf += stride) {
*buf = self->color_table[i];
}

View File

@ -90,7 +90,7 @@ START_ALLOW_CASE_RANGE
case 3:
self->italic = true; break;
case 4:
if (i < count) { self->decoration = MIN(3, params[i]); i++; }
if (i < count) { self->decoration = MIN(3u, params[i]); i++; }
else self->decoration = 1;
break;
case 7:
@ -161,7 +161,7 @@ apply_sgr_to_cells(GPUCell *first_cell, unsigned int cell_count, unsigned int *p
case 3:
SET(ITALIC_SHIFT);
case 4:
if (i < count) { uint8_t val = MIN(3, params[i]); i++; SETM(val, DECORATION_MASK, DECORATION_SHIFT); }
if (i < count) { uint8_t val = MIN(3u, params[i]); i++; SETM(val, DECORATION_MASK, DECORATION_SHIFT); }
else { SETM(1, DECORATION_MASK, DECORATION_SHIFT); }
case 7:
SET(REVERSE_SHIFT);

View File

@ -22,8 +22,12 @@
#define EXPORTED __attribute__ ((visibility ("default")))
#define LIKELY(x) __builtin_expect (!!(x), 1)
#define UNLIKELY(x) __builtin_expect (!!(x), 0)
#define MAX(x, y) (((x) > (y)) ? (x) : (y))
#define MIN(x, y) (((x) > (y)) ? (y) : (x))
#define MAX(x, y) __extension__ ({ \
__typeof__ (x) a = (x); __typeof__ (y) b = (y); \
a > b ? a : b;})
#define MIN(x, y) __extension__ ({ \
__typeof__ (x) a = (x); __typeof__ (y) b = (y); \
a < b ? a : b;})
#define xstr(s) str(s)
#define str(s) #s
#define arraysz(x) (sizeof(x)/sizeof(x[0]))
@ -250,7 +254,7 @@ typedef struct {FONTS_DATA_HEAD} *FONTS_DATA_HANDLE;
#define ensure_space_for(base, array, type, num, capacity, initial_cap, zero_mem) \
if ((base)->capacity < num) { \
size_t _newcap = MAX(initial_cap, MAX(2 * (base)->capacity, num)); \
size_t _newcap = MAX((size_t)initial_cap, MAX(2 * (base)->capacity, (size_t)num)); \
(base)->array = realloc((base)->array, sizeof(type) * _newcap); \
if ((base)->array == NULL) fatal("Out of memory while ensuring space for %zu elements in array of %s", (size_t)num, #type); \
if (zero_mem) memset((base)->array + (base)->capacity, 0, sizeof(type) * (_newcap - (base)->capacity)); \

View File

@ -12,9 +12,9 @@
#include "unicode-data.h"
#define MISSING_GLYPH 4
#define MAX_NUM_EXTRA_GLYPHS 8
#define CELLS_IN_CANVAS ((MAX_NUM_EXTRA_GLYPHS + 1) * 3)
#define MAX_NUM_EXTRA_GLYPHS_PUA 4
#define MAX_NUM_EXTRA_GLYPHS 8u
#define CELLS_IN_CANVAS ((MAX_NUM_EXTRA_GLYPHS + 1u) * 3u)
#define MAX_NUM_EXTRA_GLYPHS_PUA 4u
typedef void (*send_sprite_to_gpu_func)(FONTS_DATA_HANDLE fg, unsigned int, unsigned int, unsigned int, pixel*);
send_sprite_to_gpu_func current_send_sprite_to_gpu = NULL;
@ -196,7 +196,7 @@ sprite_map_set_error(int error) {
void
sprite_tracker_set_limits(size_t max_texture_size_, size_t max_array_len_) {
max_texture_size = max_texture_size_;
max_array_len = MIN(0xfff, max_array_len_);
max_array_len = MIN(0xfffu, max_array_len_);
}
static inline void
@ -207,7 +207,7 @@ do_increment(FontGroup *fg, int *error) {
fg->sprite_tracker.ynum = MIN(MAX(fg->sprite_tracker.ynum, fg->sprite_tracker.y + 1), fg->sprite_tracker.max_y);
if (fg->sprite_tracker.y >= fg->sprite_tracker.max_y) {
fg->sprite_tracker.y = 0; fg->sprite_tracker.z++;
if (fg->sprite_tracker.z >= MIN(UINT16_MAX, max_array_len)) *error = 2;
if (fg->sprite_tracker.z >= MIN((size_t)UINT16_MAX, max_array_len)) *error = 2;
}
}
}
@ -329,8 +329,8 @@ clear_special_glyph_cache(Font *font) {
static void
sprite_tracker_set_layout(GPUSpriteTracker *sprite_tracker, unsigned int cell_width, unsigned int cell_height) {
sprite_tracker->xnum = MIN(MAX(1, max_texture_size / cell_width), UINT16_MAX);
sprite_tracker->max_y = MIN(MAX(1, max_texture_size / cell_height), UINT16_MAX);
sprite_tracker->xnum = MIN(MAX(1u, max_texture_size / cell_width), (size_t)UINT16_MAX);
sprite_tracker->max_y = MIN(MAX(1u, max_texture_size / cell_height), (size_t)UINT16_MAX);
sprite_tracker->ynum = 1;
sprite_tracker->x = 0; sprite_tracker->y = 0; sprite_tracker->z = 0;
}
@ -586,7 +586,7 @@ render_alpha_mask(uint8_t *alpha_mask, pixel* dest, Region *src_rect, Region *de
for(size_t sc = src_rect->left, dc = dest_rect->left; sc < src_rect->right && dc < dest_rect->right; sc++, dc++) {
pixel val = d[dc];
uint8_t alpha = s[sc];
d[dc] = 0xffffff00 | MIN(0xff, alpha + (val & 0xff));
d[dc] = 0xffffff00 | MIN(0xffu, alpha + (val & 0xff));
}
}
}
@ -716,7 +716,7 @@ num_codepoints_in_cell(CPUCell *cell) {
static inline void
shape(CPUCell *first_cpu_cell, GPUCell *first_gpu_cell, index_type num_cells, hb_font_t *font, bool disable_ligature) {
if (group_state.groups_capacity <= 2 * num_cells) {
group_state.groups_capacity = MAX(128, 2 * num_cells); // avoid unnecessary reallocs
group_state.groups_capacity = MAX(128u, 2 * num_cells); // avoid unnecessary reallocs
group_state.groups = realloc(group_state.groups, sizeof(Group) * group_state.groups_capacity);
if (!group_state.groups) fatal("Out of memory");
}
@ -837,7 +837,7 @@ shape_run(CPUCell *first_cpu_cell, GPUCell *first_gpu_cell, index_type num_cells
uint32_t cluster, next_cluster;
bool add_to_current_group;
#define G(x) (group_state.x)
#define MAX_GLYPHS_IN_GROUP (MAX_NUM_EXTRA_GLYPHS + 1)
#define MAX_GLYPHS_IN_GROUP (MAX_NUM_EXTRA_GLYPHS + 1u)
while (G(glyph_idx) < G(num_glyphs) && G(cell_idx) < G(num_cells)) {
glyph_index glyph_id = G(info)[G(glyph_idx)].codepoint;
cluster = G(info)[G(glyph_idx)].cluster;
@ -1072,7 +1072,7 @@ render_line(FONTS_DATA_HANDLE fg_, Line *line, index_type lnum, Cursor *cursor,
if (is_private_use(cpu_cell->ch)
&& cell_font_idx != BOX_FONT
&& cell_font_idx != MISSING_FONT) {
int desired_cells = 1;
unsigned int desired_cells = 1;
if (cell_font_idx > 0) {
Font *font = (fg->fonts + cell_font_idx);
glyph_index glyph_id = glyph_id_for_codepoint(font->face, cpu_cell->ch);
@ -1081,7 +1081,7 @@ render_line(FONTS_DATA_HANDLE fg_, Line *line, index_type lnum, Cursor *cursor,
desired_cells = ceilf((float)width / fg->cell_width);
}
int num_spaces = 0;
unsigned int num_spaces = 0;
while ((line->cpu_cells[i+num_spaces+1].ch == ' ')
&& num_spaces < MAX_NUM_EXTRA_GLYPHS_PUA
&& num_spaces < desired_cells

View File

@ -267,7 +267,7 @@ find_or_create_image(GraphicsManager *self, uint32_t id, bool *existing) {
static Image*
handle_add_command(GraphicsManager *self, const GraphicsCommand *g, const uint8_t *payload, bool *is_dirty, uint32_t iid) {
#define ABRT(code, ...) { set_add_response(#code, __VA_ARGS__); self->loading_image = 0; if (img) img->data_loaded = false; return NULL; }
#define MAX_DATA_SZ (4 * 100000000)
#define MAX_DATA_SZ (4u * 100000000u)
has_add_respose = false;
bool existing, init_img = true;
Image *img = NULL;

View File

@ -77,7 +77,7 @@ free_pagerhist(HistoryBuf *self) {
static inline bool
pagerhist_extend(PagerHistoryBuf *ph, size_t minsz) {
if (ph->bufsize >= ph->maxsz) return false;
size_t newsz = ph->bufsize + MAX(1024 * 1024, minsz);
size_t newsz = ph->bufsize + MAX(1024u * 1024u, minsz);
void *newbuf = PyMem_Realloc(ph->buffer, newsz * sizeof(Py_UCS4));
if (!newbuf) return false;
ph->buffer = newbuf;
@ -186,7 +186,7 @@ pagerhist_push(HistoryBuf *self) {
ph->bufend = ph->end; ph->end = 0; \
} \
}
size_t sz = MAX(1024, ph->bufsize - ph->end);
size_t sz = MAX(1024u, ph->bufsize - ph->end);
sz = MAX(sz, self->xnum + self->xnum);
EXPAND_IF_FULL(sz);
if (ph->start != ph->end && !l.continued) {

View File

@ -34,7 +34,7 @@ void
set_special_key_combo(int glfw_key, int mods, bool is_native) {
if (is_native) {
if (native_special_keys_count >= native_special_keys_capacity) {
native_special_keys_capacity = MAX(128, 2 * native_special_keys_capacity);
native_special_keys_capacity = MAX(128u, 2 * native_special_keys_capacity);
native_special_keys = realloc(native_special_keys, sizeof(native_special_keys[0]) * native_special_keys_capacity);
if (native_special_keys == NULL) fatal("Out of memory");
}

View File

@ -58,7 +58,7 @@ find_colon_slash(Line *self, index_type x, index_type limit) {
index_type pos = x;
enum URL_PARSER_STATES {ANY, FIRST_SLASH, SECOND_SLASH};
enum URL_PARSER_STATES state = ANY;
limit = MAX(2, limit);
limit = MAX(2u, limit);
if (pos < limit) return 0;
do {
char_type ch = self->cpu_cells[pos].ch;

View File

@ -306,7 +306,7 @@ dispatch_osc(Screen *screen, PyObject DUMP_UNUSED *dump_callback) {
#define SET_COLOR(name) REPORT_OSC2(name, code, string); name(screen, code, string);
const unsigned int limit = screen->parser_buf_pos;
unsigned int code=0, i;
for (i = 0; i < MIN(limit, 5); i++) {
for (i = 0; i < MIN(limit, 5u); i++) {
if (screen->parser_buf[i] < '0' || screen->parser_buf[i] > '9') break;
}
if (i > 0) {
@ -380,7 +380,7 @@ screen_cursor_up2(Screen *s, unsigned int count) { screen_cursor_up(s, count, fa
static inline void
screen_cursor_back1(Screen *s, unsigned int count) { screen_cursor_back(s, count, -1); }
static inline void
screen_tabn(Screen *s, unsigned int count) { for (index_type i=0; i < MAX(1, count); i++) screen_tab(s); }
screen_tabn(Screen *s, unsigned int count) { for (index_type i=0; i < MAX(1u, count); i++) screen_tab(s); }
static inline const char*
repr_csi_params(unsigned int *params, unsigned int num_params) {

View File

@ -64,7 +64,7 @@ rewrap_inner(BufType *src, BufType *dest, const index_type src_limit, HistoryBuf
while(src_x_limit && (src->line->cpu_cells[src_x_limit - 1].ch) == BLANK_CHAR) src_x_limit--;
}
if (is_tracked_line && *track_x >= src_x_limit) *track_x = MAX(1, src_x_limit) - 1;
if (is_tracked_line && *track_x >= src_x_limit) *track_x = MAX(1u, src_x_limit) - 1;
while (src_x < src_x_limit) {
if (dest_x >= dest->xnum) { next_dest_line(true); dest_x = 0; }
num = MIN(src->line->xnum - src_x, dest->xnum - dest_x);

View File

@ -185,7 +185,7 @@ realloc_lb(LineBuf *old, unsigned int lines, unsigned int columns, index_type *n
static bool
screen_resize(Screen *self, unsigned int lines, unsigned int columns) {
if (self->overlay_line.is_active) deactivate_overlay_line(self);
lines = MAX(1, lines); columns = MAX(1, columns);
lines = MAX(1u, lines); columns = MAX(1u, columns);
bool is_main = self->linebuf == self->main_linebuf;
index_type num_content_lines_before, num_content_lines_after, num_content_lines;
@ -325,7 +325,7 @@ move_widened_char(Screen *self, CPUCell* cpu_cell, GPUCell *gpu_cell, index_type
linebuf_init_line(self->linebuf, self->cursor->y);
dest_cpu = self->linebuf->line->cpu_cells;
dest_gpu = self->linebuf->line->gpu_cells;
self->cursor->x = MIN(2, self->columns);
self->cursor->x = MIN(2u, self->columns);
linebuf_mark_line_dirty(self->linebuf, self->cursor->y);
} else {
dest_cpu = cpu_cell - 1;
@ -818,7 +818,7 @@ screen_cursor_down1(Screen *self, unsigned int count/*=1*/) {
void
screen_cursor_to_column(Screen *self, unsigned int column) {
unsigned int x = MAX(column, 1) - 1;
unsigned int x = MAX(column, 1u) - 1;
if (x != self->cursor->x) {
self->cursor->x = x;
screen_ensure_bounds(self, false, cursor_within_margins(self));