Skip to content

Commit

Permalink
Fix warnings from sanitizer
Browse files Browse the repository at this point in the history
- loader: fix leak of pipe fd
- application: remove always true variable
- config: fix possible leak
- jxl: fix possible leak
- info: fix potential null-pointer passed into memcpy
- Fix dereferencing possible NULL
- Avoid the loss of a fractional part
- Fix sanitizer reports: excessive shift, signed integer overflow

Signed-off-by: Alexandr Pashchenko <[email protected]>
  • Loading branch information
appashchenko authored Aug 5, 2024
1 parent 8e77962 commit 200f41a
Show file tree
Hide file tree
Showing 9 changed files with 30 additions and 16 deletions.
5 changes: 2 additions & 3 deletions src/application.c
Original file line number Diff line number Diff line change
Expand Up @@ -380,9 +380,8 @@ bool app_init(const char** sources, size_t num)
}
if (ctx.window.width == SIZE_FULLSCREEN) {
ui_toggle_fullscreen();
} else if (first_image &&
(ctx.window.width == SIZE_FROM_IMAGE ||
ctx.window.width == SIZE_FROM_PARENT)) {
} else if (ctx.window.width == SIZE_FROM_IMAGE ||
ctx.window.width == SIZE_FROM_PARENT) {
// fixup window size form the first image
const struct pixmap* pm = &first_image->frames[0].pm;
ctx.window.width = pm->width;
Expand Down
8 changes: 6 additions & 2 deletions src/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ static bool load_config(const char* path)
// check for section beginning
if (*line == '[') {
ssize_t len;
char* new_section;
++line;
delim = strchr(line, ']');
if (!delim || line + 1 == delim) {
Expand All @@ -125,8 +126,11 @@ static bool load_config(const char* path)
}
*delim = 0;
len = delim - line + 1;
section = realloc(section, len);
memcpy(section, line, len);
new_section = realloc(section, len);
if (new_section) {
section = new_section;
memcpy(section, line, len);
}
continue;
}

Expand Down
6 changes: 4 additions & 2 deletions src/formats/jpeg.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ enum loader_status decode_jpeg(struct image* ctx, const uint8_t* data,
uint32_t* pixel = (uint32_t*)line;
for (int x = jpg.output_width - 1; x >= 0; --x) {
const uint8_t src = *(line + x);
pixel[x] = (0xff << 24) | src << 16 | src << 8 | src;
pixel[x] = ((argb_t)0xff << 24) | (argb_t)src << 16 |
(argb_t)src << 8 | src;
}
}

Expand All @@ -86,7 +87,8 @@ enum loader_status decode_jpeg(struct image* ctx, const uint8_t* data,
uint32_t* pixel = (uint32_t*)line;
for (int x = jpg.output_width - 1; x >= 0; --x) {
const uint8_t* src = line + x * 3;
pixel[x] = (0xff << 24) | src[0] << 16 | src[1] << 8 | src[2];
pixel[x] = ((argb_t)0xff << 24) | (argb_t)src[0] << 16 |
(argb_t)src[1] << 8 | src[2];
}
}
#endif // LIBJPEG_TURBO_VERSION
Expand Down
8 changes: 5 additions & 3 deletions src/formats/jxl.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ enum loader_status decode_jxl(struct image* ctx, const uint8_t* data,
JxlBasicInfo info = { 0 };
JxlDecoderStatus status;
size_t buffer_sz;
struct image_frame* frames;
size_t frame_num = 0;

const JxlPixelFormat jxl_format = { .num_channels = 4, // ARBG
Expand Down Expand Up @@ -72,11 +73,12 @@ enum loader_status decode_jxl(struct image* ctx, const uint8_t* data,
frame_num = ctx->num_frames;
break;
case JXL_DEC_FRAME:
ctx->frames = realloc(
ctx->frames, sizeof(*ctx->frames) * (ctx->num_frames + 1));
if (!ctx->frames) {
frames = realloc(ctx->frames,
sizeof(*ctx->frames) * (ctx->num_frames + 1));
if (!frames) {
goto fail;
}
ctx->frames = frames;
if (!pixmap_create(&ctx->frames[frame_num].pm, info.xsize,
info.ysize)) {
goto fail;
Expand Down
1 change: 1 addition & 0 deletions src/info.c
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ static const struct field_scheme default_gallery_br[] = {
#define SET_DEFAULT(m, p, d) \
ctx.scheme[m][p].fields_num = ARRAY_SIZE(d); \
ctx.scheme[m][p].fields = malloc(sizeof(d)); \
if (ctx.scheme[m][p].fields) \
memcpy(ctx.scheme[m][p].fields, d, sizeof(d))

/** Info scheme: set of fields in one of screen positions. */
Expand Down
2 changes: 2 additions & 0 deletions src/loader.c
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,8 @@ static enum loader_status image_from_exec(struct image* img, const char* cmd)

pid = fork();
if (pid == -1) {
close(pfd[1]);
close(pfd[0]);
return ldr_ioerror;
}

Expand Down
8 changes: 4 additions & 4 deletions src/pixmap.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ typedef uint32_t argb_t;
#define ARGB_GET_B(c) (((c) >> ARGB_B_SHIFT) & 0xff)

// create argb_t from channel value
#define ARGB_SET_A(a) (((a) & 0xff) << ARGB_A_SHIFT)
#define ARGB_SET_R(r) (((r) & 0xff) << ARGB_R_SHIFT)
#define ARGB_SET_G(g) (((g) & 0xff) << ARGB_G_SHIFT)
#define ARGB_SET_B(b) (((b) & 0xff) << ARGB_B_SHIFT)
#define ARGB_SET_A(a) (((argb_t)(a) & 0xff) << ARGB_A_SHIFT)
#define ARGB_SET_R(r) (((argb_t)(r) & 0xff) << ARGB_R_SHIFT)
#define ARGB_SET_G(g) (((argb_t)(g) & 0xff) << ARGB_G_SHIFT)
#define ARGB_SET_B(b) (((argb_t)(b) & 0xff) << ARGB_B_SHIFT)

// convert ABGR to ARGB
#define ABGR_TO_ARGB(c) \
Expand Down
4 changes: 4 additions & 0 deletions src/str.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ bool str_to_num(const char* text, size_t len, ssize_t* value, int base)
char buffer[32];
const char* ptr;

if (!text) {
return false;
}

if (!*text) {
return false;
}
Expand Down
4 changes: 2 additions & 2 deletions src/viewer.c
Original file line number Diff line number Diff line change
Expand Up @@ -257,8 +257,8 @@ static void zoom_image(const char* params)
} else if (str_to_num(params, 0, &percent, 0) && percent != 0 &&
percent > -1000 && percent < 1000) {
// zoom in %
const double wnd_half_w = ui_get_width() / 2;
const double wnd_half_h = ui_get_height() / 2;
const double wnd_half_w = (double)ui_get_width() / 2;
const double wnd_half_h = (double)ui_get_height() / 2;
const float step = (ctx.scale / 100) * percent;
const double center_x = wnd_half_w / ctx.scale - ctx.img_x / ctx.scale;
const double center_y = wnd_half_h / ctx.scale - ctx.img_y / ctx.scale;
Expand Down

0 comments on commit 200f41a

Please sign in to comment.