Skip to content

Commit

Permalink
Try to ensure that width * y computations are unsigned.
Browse files Browse the repository at this point in the history
UB!!! Found while looking at #179.
  • Loading branch information
kohler committed Jan 24, 2022
1 parent 15c36c2 commit b89757f
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 18 deletions.
22 changes: 11 additions & 11 deletions src/gifunopt.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,16 @@ static void
put_image_in_screen(Gif_Stream *gfs, Gif_Image *gfi, uint16_t *screen)
{
int transparent = gfi->transparent;
int x, y;
int w = gfi->width;
int h = gfi->height;
unsigned x, y;
unsigned w = gfi->width;
unsigned h = gfi->height;
if (gfi->left + w > gfs->screen_width)
w = gfs->screen_width - gfi->left;
if (gfi->top + h > gfs->screen_height)
h = gfs->screen_height - gfi->top;

for (y = 0; y < h; y++) {
uint16_t *move = screen + gfs->screen_width * (y + gfi->top) + gfi->left;
uint16_t *move = screen + (unsigned) gfs->screen_width * (y + gfi->top) + gfi->left;
uint8_t *line = gfi->img[y];
for (x = 0; x < w; x++, move++, line++)
if (*line != transparent)
Expand All @@ -45,9 +45,9 @@ static void
put_background_in_screen(Gif_Stream *gfs, Gif_Image *gfi, uint16_t *screen)
{
uint16_t solid;
int x, y;
int w = gfi->width;
int h = gfi->height;
unsigned x, y;
unsigned w = gfi->width;
unsigned h = gfi->height;
if (gfi->left + w > gfs->screen_width) w = gfs->screen_width - gfi->left;
if (gfi->top + h > gfs->screen_height) h = gfs->screen_height - gfi->top;

Expand All @@ -58,7 +58,7 @@ put_background_in_screen(Gif_Stream *gfs, Gif_Image *gfi, uint16_t *screen)
solid = TRANSPARENT;

for (y = 0; y < h; y++) {
uint16_t *move = screen + gfs->screen_width * (y + gfi->top) + gfi->left;
uint16_t *move = screen + (unsigned) gfs->screen_width * (y + gfi->top) + gfi->left;
for (x = 0; x < w; x++, move++)
*move = solid;
}
Expand All @@ -71,7 +71,7 @@ create_image_data(Gif_Stream *gfs, Gif_Image *gfi, uint16_t *screen,
{
int have[257];
int transparent = -1;
unsigned pos, size = gfs->screen_width * gfs->screen_height;
unsigned pos, size = (unsigned) gfs->screen_width * (unsigned) gfs->screen_height;
uint16_t *move;
int i;

Expand Down Expand Up @@ -116,7 +116,7 @@ create_image_data(Gif_Stream *gfs, Gif_Image *gfi, uint16_t *screen,
static int
unoptimize_image(Gif_Stream *gfs, Gif_Image *gfi, uint16_t *screen)
{
unsigned size = gfs->screen_width * gfs->screen_height;
unsigned size = (unsigned) gfs->screen_width * (unsigned) gfs->screen_height;
int used_transparent;
uint8_t *new_data = Gif_NewArray(uint8_t, size);
uint16_t *new_screen = screen;
Expand Down Expand Up @@ -190,7 +190,7 @@ Gif_FullUnoptimize(Gif_Stream *gfs, int flags)
return 0;

Gif_CalculateScreenSize(gfs, 0);
size = gfs->screen_width * gfs->screen_height;
size = (unsigned) gfs->screen_width * (unsigned) gfs->screen_height;

screen = Gif_NewArray(uint16_t, size);
gfi = gfs->images[0];
Expand Down
2 changes: 1 addition & 1 deletion src/gifwrite.c
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ write_compressed_data(Gif_Stream *gfs, Gif_Image *gfi,
unsigned pos;
unsigned clear_bufpos, clear_pos;
unsigned line_endpos;
const unsigned image_endpos = gfi->height * gfi->width;
const unsigned image_endpos = (size_t) gfi->height * (size_t) gfi->width;
const uint8_t *imageline;

unsigned run = 0;
Expand Down
2 changes: 1 addition & 1 deletion src/ungifwrt.c
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ write_compressed_data(Gif_Image *gfi,

#ifndef GIF_NO_COMPRESSION
if (!do_clear) {
unsigned pixels_left = gfi->width * gfi->height - pos;
unsigned pixels_left = (unsigned) gfi->width * (unsigned) gfi->height - pos;
if (pixels_left) {
/* Always clear if run_ewma gets small relative to
min_code_bits. Otherwise, clear if #images/run is smaller
Expand Down
10 changes: 5 additions & 5 deletions src/xform.c
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ rotate_image(Gif_Image* gfi, Gt_Frame* fr, int rotation)
int width = gfi->width;
int height = gfi->height;
uint8_t **img = gfi->img;
uint8_t *new_data = Gif_NewArray(uint8_t, (unsigned) width * height);
uint8_t *new_data = Gif_NewArray(uint8_t, (unsigned) width * (unsigned) height);
uint8_t *trav = new_data;

/* this function can only rotate by 90 or 270 degrees */
Expand Down Expand Up @@ -390,7 +390,7 @@ static void kcscreen_init(kcscreen* kcs, Gif_Stream* gfs, int sw, int sh) {
assert(!kcs->data && !kcs->scratch);
kcs->width = sw <= 0 ? gfs->screen_width : sw;
kcs->height = sh <= 0 ? gfs->screen_height : sh;
sz = (unsigned) kcs->width * kcs->height;
sz = kcs->width * kcs->height;
kcs->data = Gif_NewArray(kacolor, sz);
if ((gfs->nimages == 0 || gfs->images[0]->transparent < 0)
&& gfs->global && gfs->background < gfs->global->ncol) {
Expand Down Expand Up @@ -476,7 +476,7 @@ static void ksscreen_init(ksscreen* kss, Gif_Stream* gfs, int sw, int sh) {
assert(!kss->data && !kss->scratch);
kss->width = sw <= 0 ? gfs->screen_width : sw;
kss->height = sh <= 0 ? gfs->screen_height : sh;
sz = (unsigned) kss->width * kss->height;
sz = kss->width * kss->height;
kss->data = Gif_NewArray(scale_color, sz);
if ((gfs->nimages == 0 || gfs->images[0]->transparent < 0)
&& gfs->global && gfs->background < gfs->global->ncol) {
Expand Down Expand Up @@ -1084,7 +1084,7 @@ static void scale_image_data_weighted(scale_context* sctx, Gif_Image* gfo,
/* skip */;
for (yi = yi0; yi != yi1; ++yi) {
const scale_color* iscr = &sctx->iscr.data[sctx->iscr.width * yi];
scale_color* oscr = &kcx[gfo->width * yi];
scale_color* oscr = &kcx[(unsigned) gfo->width * yi];
for (xo = 0; xo != gfo->width; ++xo)
sc_clear(&oscr[xo]);
for (w = ww; w->opos < gfo->left + gfo->width; ++w)
Expand All @@ -1098,7 +1098,7 @@ static void scale_image_data_weighted(scale_context* sctx, Gif_Image* gfo,
for (xo = 0; xo != gfo->width; ++xo)
sc_clear(&sc[xo]);
for (; w->opos < gfo->top + yo + 1; ++w) {
const scale_color* iscr = &kcx[gfo->width * w->ipos];
const scale_color* iscr = &kcx[(unsigned) gfo->width * w->ipos];
assert(w->ipos >= yi0 && w->ipos < yi1);
for (xo = 0; xo != gfo->width; ++xo)
SCVEC_ADDVxF(sc[xo], iscr[xo], w->w);
Expand Down

0 comments on commit b89757f

Please sign in to comment.