Skip to content

Commit e91c585

Browse files
committed
Add small tweaks from AI code review
1 parent 1c5b761 commit e91c585

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

src_c/draw.c

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
#include "doc/draw_doc.h"
3131

32+
#include <limits.h> // for CHAR_BIT
3233
#include <math.h>
3334

3435
#include <float.h>
@@ -1394,7 +1395,7 @@ swap(float *a, float *b)
13941395
*b = temp;
13951396
}
13961397

1397-
#define WORD_BITS (8 * sizeof(unsigned int))
1398+
#define WORD_BITS (CHAR_BIT * sizeof(unsigned int))
13981399

13991400
struct point2d {
14001401
Uint32 x;
@@ -1404,18 +1405,19 @@ struct point2d {
14041405
static inline void
14051406
_bitarray_set(unsigned int *bitarray, size_t idx, SDL_bool value)
14061407
{
1408+
const unsigned int mask = (1u << (idx % WORD_BITS));
14071409
if (value) {
1408-
bitarray[idx / WORD_BITS] |= (1 << (idx % WORD_BITS));
1410+
bitarray[idx / WORD_BITS] |= mask;
14091411
}
14101412
else {
1411-
bitarray[idx / WORD_BITS] &= (~(1) << (idx % WORD_BITS));
1413+
bitarray[idx / WORD_BITS] &= ~mask;
14121414
}
14131415
}
14141416

14151417
static inline SDL_bool
14161418
_bitarray_get(unsigned int *bitarray, size_t idx)
14171419
{
1418-
if (bitarray[idx / WORD_BITS] & (1 << (idx % WORD_BITS)))
1420+
if (bitarray[idx / WORD_BITS] & (1u << (idx % WORD_BITS)))
14191421
return SDL_TRUE;
14201422
else
14211423
return SDL_FALSE;
@@ -2502,8 +2504,9 @@ flood_fill_inner(SDL_Surface *surf, int x1, int y1, Uint32 new_color,
25022504
// breadth first flood fill, like graph search
25032505
SDL_Rect cliprect;
25042506
size_t mask_idx;
2505-
2506-
SDL_GetClipRect(surf, &cliprect);
2507+
if (!PG_GetSurfaceClipRect(surf, &cliprect)) {
2508+
return RAISE(pgExc_SDLError, SDL_GetError());
2509+
}
25072510
size_t frontier_bufsize = 8, frontier_size = 1, next_frontier_size = 0;
25082511

25092512
// Instead of a queue, we use two arrays and swap them between steps.
@@ -2524,8 +2527,9 @@ flood_fill_inner(SDL_Surface *surf, int x1, int y1, Uint32 new_color,
25242527

25252528
// 2D bitmask for queued nodes
25262529
// we could check drawn color, but that doesnt work for patterns
2527-
size_t mask_size = cliprect.w * cliprect.h;
2528-
unsigned int *mask = calloc((mask_size) / 8 + 1, sizeof(unsigned int));
2530+
size_t mask_size = (size_t)cliprect.w * (size_t)cliprect.h;
2531+
size_t mask_words = (mask_size + WORD_BITS - 1) / WORD_BITS;
2532+
unsigned int *mask = calloc(mask_words, sizeof(unsigned int));
25292533

25302534
if (mask == NULL) {
25312535
free(frontier);

0 commit comments

Comments
 (0)