Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

additional memory allocation checks #18114

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 21 additions & 13 deletions src/bauhaus/bauhaus.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
This file is part of darktable,
Copyright (C) 2012-2023 darktable developers.
Copyright (C) 2012-2024 darktable developers.

darktable is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -145,21 +145,27 @@ static dt_bauhaus_combobox_entry_t *_new_combobox_entry
void (*free_func)(void *))
{
dt_bauhaus_combobox_entry_t *entry = calloc(1, sizeof(dt_bauhaus_combobox_entry_t));
entry->label = g_strdup(label);
entry->alignment = alignment;
entry->sensitive = sensitive;
entry->data = data;
entry->free_func = free_func;
if(entry)
{
entry->label = g_strdup(label);
entry->alignment = alignment;
entry->sensitive = sensitive;
entry->data = data;
entry->free_func = free_func;
}
return entry;
}

static void _free_combobox_entry(gpointer data)
{
dt_bauhaus_combobox_entry_t *entry = (dt_bauhaus_combobox_entry_t *)data;
g_free(entry->label);
if(entry->free_func)
entry->free_func(entry->data);
free(entry);
if(entry)
{
g_free(entry->label);
if(entry->free_func)
entry->free_func(entry->data);
free(entry);
}
}

static GdkRGBA * _default_color_assign()
Expand Down Expand Up @@ -1560,7 +1566,8 @@ void dt_bauhaus_combobox_add_full(GtkWidget *widget,
data =_combobox_entry(d, d->entries->len - 1)->data + 1;
dt_bauhaus_combobox_entry_t *entry = _new_combobox_entry(text, align,
sensitive, data, free_func);
g_ptr_array_add(d->entries, entry);
if(entry)
g_ptr_array_add(d->entries, entry);
if(d->active < 0)
d->active = 0;
if(d->defpos == -1 && sensitive)
Expand Down Expand Up @@ -1662,8 +1669,9 @@ void dt_bauhaus_combobox_insert_full(GtkWidget *widget,
dt_bauhaus_widget_t *w = DT_BAUHAUS_WIDGET(widget);
if(w->type != DT_BAUHAUS_COMBOBOX) return;
dt_bauhaus_combobox_data_t *d = &w->data.combobox;
g_ptr_array_insert(d->entries, pos,
_new_combobox_entry(text, align, TRUE, data, free_func));
dt_bauhaus_combobox_entry_t *entry = _new_combobox_entry(text, align, TRUE, data, free_func);
if(entry)
g_ptr_array_insert(d->entries, pos, entry);
if(d->active < 0) d->active = 0;
}

Expand Down
9 changes: 7 additions & 2 deletions src/common/opencl.c
Original file line number Diff line number Diff line change
Expand Up @@ -2178,7 +2178,9 @@ static gboolean _opencl_load_program(const int dev,

const size_t filesize = filestat.st_size;
char *file = malloc(filesize + 2048);
size_t rd = fread(file, sizeof(char), filesize, f);
size_t rd = 0;
if(file)
rd = fread(file, sizeof(char), filesize, f);
fclose(f);
if(rd != filesize)
{
Expand Down Expand Up @@ -2255,7 +2257,10 @@ static gboolean _opencl_load_program(const int dev,
size_t cached_filesize = cachedstat.st_size;

unsigned char *cached_content = malloc(cached_filesize + 1);
rd = fread(cached_content, sizeof(char), cached_filesize, cached);
if(cached_content)
rd = fread(cached_content, sizeof(char), cached_filesize, cached);
else
rd = 0;
if(rd != cached_filesize)
{
dt_print(DT_DEBUG_OPENCL,
Expand Down
32 changes: 25 additions & 7 deletions src/common/pdf.c
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,9 @@ static size_t _pdf_stream_encoder_Flate(dt_pdf_t *pdf, const unsigned char *data
int result;
uLongf destLen = compressBound(len);
unsigned char *buffer = malloc(destLen);

if(!buffer)
return 0;

result = compress(buffer, &destLen, data, len);

if(result != Z_OK)
Expand Down Expand Up @@ -786,11 +788,24 @@ float * read_ppm(const char * filename, int * wd, int * ht)
}

float *image = malloc(sizeof(float) * width * height * 3);
if(!image)
{
fprintf(stderr, "unable to allocate buffer for image data\n");
fclose(f);
return NULL;
}

if(max <= 255)
{
// read a 8 bit PPM
uint8_t *tmp = malloc(sizeof(uint8_t) * width * height * 3);
if(!tmp)
{
fprintf(stderr, "unable to allocate buffer for file data\n");
free(image);
fclose(f);
return NULL;
}
int res = fread(tmp, sizeof(uint8_t) * 3, width * height, f);
if(res != width * height)
{
Expand All @@ -810,6 +825,13 @@ float * read_ppm(const char * filename, int * wd, int * ht)
{
// read a 16 bit PPM
uint16_t *tmp = malloc(sizeof(uint16_t) * width * height * 3);
if(!tmp)
{
fprintf(stderr, "unable to allocate buffer for file data\n");
free(image);
fclose(f);
return NULL;
}
int res = fread(tmp, sizeof(uint16_t) * 3, width * height, f);
if(res != width * height)
{
Expand All @@ -819,14 +841,10 @@ float * read_ppm(const char * filename, int * wd, int * ht)
fclose(f);
return NULL;
}
// swap byte order
// swap byte order and transform values into 0..1 range
DT_OMP_FOR()
for(int k = 0; k < 3 * width * height; k++)
tmp[k] = ((tmp[k] & 0xff) << 8) | (tmp[k] >> 8);
// and transform it into 0..1 range
DT_OMP_FOR()
for(int i = 0; i < width * height * 3; i++)
image[i] = (float)tmp[i] / max;
image[k] = (float)(((tmp[k] & 0xff) << 8) | (tmp[k] >> 8)) / max;
free(tmp);
}
fclose(f);
Expand Down
5 changes: 5 additions & 0 deletions src/common/printprof.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ int dt_apply_printer_profile(void **in, uint32_t width, uint32_t height, int bpp
}

void *out = malloc((size_t)3 * width * height);
if(!out)
{
dt_print(DT_DEBUG_ALWAYS, "unable to allocate buffer for printer-proofed image");
return 1;
}

if(bpp == 8)
{
Expand Down
8 changes: 8 additions & 0 deletions src/common/utility.c
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,14 @@ static cairo_surface_t *_util_get_svg_img(gchar *logo, const float size)
const int stride = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, final_width);

guint8 *image_buffer = calloc(stride * final_height, sizeof(guint8));
if(!image_buffer)
{
dt_print(DT_DEBUG_ALWAYS, "warning: unable to allocate rasterization buffer for SVG '%s'", dtlogo);
g_free(logo);
g_free(dtlogo);
g_object_unref(svg);
return NULL;
}
if(darktable.gui)
surface = dt_cairo_image_surface_create_for_data(image_buffer, CAIRO_FORMAT_ARGB32, final_width,
final_height, stride);
Expand Down
Loading