Skip to content

Commit

Permalink
Merge pull request #17580 from ralfbrown/lib_alloc_checks
Browse files Browse the repository at this point in the history
[maint] check memory allocs in utility modules
  • Loading branch information
TurboGit authored Oct 3, 2024
2 parents 7334056 + 103a719 commit 517c0ed
Show file tree
Hide file tree
Showing 9 changed files with 181 additions and 106 deletions.
34 changes: 23 additions & 11 deletions src/libs/geotagging.c
Original file line number Diff line number Diff line change
Expand Up @@ -676,6 +676,11 @@ static void _show_gpx_tracks(dt_lib_module_t *self)
GList *trkseg = dt_gpx_get_trkseg(d->map.gpx);
d->map.nb_tracks = g_list_length(trkseg);
d->map.tracks = g_malloc0(sizeof(dt_lib_tracks_data_t) * d->map.nb_tracks);
if(!d->map.tracks)
{
d->map.nb_tracks = 0;
dt_print(DT_DEBUG_ALWAYS, "[geotagging] unable to allocate storage for tracks\n");
}

_remove_images_from_map(self);
for(GList *i = d->imgs; i; i = g_list_next(i))
Expand Down Expand Up @@ -1098,9 +1103,12 @@ static GList *_lib_geotagging_get_timezones(void)
size_t last_char = strlen(name) - 1;
if(name[last_char] == '\n') name[last_char] = '\0';
tz_tuple_t *tz_tuple = (tz_tuple_t *)malloc(sizeof(tz_tuple_t));
tz_tuple->display = name;
tz_tuple->name = name;
timezones = g_list_prepend(timezones, tz_tuple);
if(tz_tuple)
{
tz_tuple->display = name;
tz_tuple->name = name;
timezones = g_list_prepend(timezones, tz_tuple);
}
}

fclose(fp);
Expand All @@ -1109,9 +1117,12 @@ static GList *_lib_geotagging_get_timezones(void)
timezones = g_list_sort(timezones, _sort_timezones);

tz_tuple_t *utc = (tz_tuple_t *)malloc(sizeof(tz_tuple_t));
utc->display = g_strdup("UTC");
utc->name = utc->display;
timezones = g_list_prepend(timezones, utc);
if(utc)
{
utc->display = g_strdup("UTC");
utc->name = utc->display;
timezones = g_list_prepend(timezones, utc);
}

#undef MAX_LINE_LENGTH

Expand Down Expand Up @@ -1177,11 +1188,12 @@ static GList *_lib_geotagging_get_timezones(void)
&buffer_size) == ERROR_SUCCESS)
{
tz_tuple_t *tz = (tz_tuple_t *)malloc(sizeof(tz_tuple_t));

tz->name = subkeyname_utf8;
tz->display = g_utf16_to_utf8(display_name, -1, NULL, NULL, NULL);
timezones = g_list_prepend(timezones, tz);

if(tz)
{
tz->name = subkeyname_utf8;
tz->display = g_utf16_to_utf8(display_name, -1, NULL, NULL, NULL);
timezones = g_list_prepend(timezones, tz);
}
subkeyname_utf8 = NULL; // to not free it later
}
free(display_name);
Expand Down
75 changes: 50 additions & 25 deletions src/libs/location.c
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ void gui_init(dt_lib_module_t *self)
{
self->data = calloc(1, sizeof(dt_lib_location_t));
dt_lib_location_t *lib = self->data;
if(!lib)
return;

self->widget = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);

Expand Down Expand Up @@ -209,13 +211,15 @@ static GtkWidget *_lib_location_place_widget_new(dt_lib_location_t *lib,

/* connect button press signal for result item */
_callback_param_t *param = (_callback_param_t *)malloc(sizeof(_callback_param_t));
lib->callback_params = g_list_append(lib->callback_params, param);
param->lib = lib;
param->result = place;
g_signal_connect(G_OBJECT(eb), "button-press-event",
G_CALLBACK(_lib_location_result_item_activated),
(gpointer)param);

if(param)
{
lib->callback_params = g_list_append(lib->callback_params, param);
param->lib = lib;
param->result = place;
g_signal_connect(G_OBJECT(eb), "button-press-event",
G_CALLBACK(_lib_location_result_item_activated),
(gpointer)param);
}
return eb;
}

Expand All @@ -227,13 +231,15 @@ static size_t _lib_location_curl_write_data(void *buffer,
dt_lib_location_t *lib = (dt_lib_location_t *)userp;

char *newdata = g_malloc0(lib->response_size + nmemb + 1);
if(lib->response != NULL)
memcpy(newdata, lib->response, lib->response_size);
memcpy(newdata + lib->response_size, buffer, nmemb);
g_free(lib->response);
lib->response = newdata;
lib->response_size += nmemb;

if(newdata)
{
if(lib->response != NULL)
memcpy(newdata, lib->response, lib->response_size);
memcpy(newdata + lib->response_size, buffer, nmemb);
g_free(lib->response);
lib->response = newdata;
lib->response_size += nmemb;
}
return nmemb;
}

Expand Down Expand Up @@ -560,9 +566,12 @@ static void _lib_location_parser_start_element(GMarkupParseContext *cxt,
{
place->marker_type = MAP_DISPLAY_POINT;
dt_geo_map_display_point_t *p = malloc(sizeof(dt_geo_map_display_point_t));
p->lon = lon;
p->lat = lat;
place->marker_points = g_list_append(place->marker_points, p);
if(p)
{
p->lon = lon;
p->lat = lat;
place->marker_points = g_list_append(place->marker_points, p);
}
}
}
else if(g_str_has_prefix(*avalue, "LINESTRING")
Expand Down Expand Up @@ -630,9 +639,12 @@ static void _lib_location_parser_start_element(GMarkupParseContext *cxt,
break;
}
dt_geo_map_display_point_t *p = malloc(sizeof(dt_geo_map_display_point_t));
p->lon = lon;
p->lat = lat;
place->marker_points = g_list_prepend(place->marker_points, p);
if(p)
{
p->lon = lon;
p->lat = lat;
place->marker_points = g_list_prepend(place->marker_points, p);
}
startptr = endptr+1;
i++;
}
Expand Down Expand Up @@ -718,6 +730,12 @@ void *get_params(dt_lib_module_t *self,
const size_t size_total = size_fixed + size_name + size_points;

void *params = malloc(size_total);
if(!params)
{
*size = 0;
return NULL;
}

struct params_fixed_t *params_fixed = (struct params_fixed_t *)params;
params_fixed->relevance = location->relevance;
params_fixed->type = location->type;
Expand Down Expand Up @@ -765,8 +783,12 @@ int set_params(dt_lib_module_t *self,

if(size_points % 2 * sizeof(float) != 0) return 1;

_lib_location_result_t *location =
(_lib_location_result_t *)malloc(sizeof(_lib_location_result_t));
_lib_location_result_t *location = malloc(sizeof(_lib_location_result_t));
if(!location)
{
dt_print(DT_DEBUG_ALWAYS, "[location] out of memory\n");
return 1;
}

location->relevance = params_fixed->relevance;
location->type = params_fixed->type;
Expand All @@ -786,9 +808,12 @@ int set_params(dt_lib_module_t *self,
{
dt_geo_map_display_point_t *p =
(dt_geo_map_display_point_t *)malloc(sizeof(dt_geo_map_display_point_t));
p->lat = points[0];
p->lon = points[1];
location->marker_points = g_list_prepend(location->marker_points, p);
if(p)
{
p->lat = points[0];
p->lon = points[1];
location->marker_points = g_list_prepend(location->marker_points, p);
}
}
location->marker_points = g_list_reverse(location->marker_points);

Expand Down
28 changes: 17 additions & 11 deletions src/libs/masks.c
Original file line number Diff line number Diff line change
Expand Up @@ -380,12 +380,15 @@ static void _tree_group(GtkButton *button, dt_lib_module_t *self)
{
dt_masks_point_group_t *fpt =
(dt_masks_point_group_t *)malloc(sizeof(dt_masks_point_group_t));
fpt->formid = id;
fpt->parentid = grp->formid;
fpt->opacity = 1.0f;
fpt->state = DT_MASKS_STATE_USE;
if(pos > 0) fpt->state |= DT_MASKS_STATE_UNION;
grp->points = g_list_append(grp->points, fpt);
if(fpt)
{
fpt->formid = id;
fpt->parentid = grp->formid;
fpt->opacity = 1.0f;
fpt->state = DT_MASKS_STATE_USE;
if(pos > 0) fpt->state |= DT_MASKS_STATE_UNION;
grp->points = g_list_append(grp->points, fpt);
}
pos++;
}
}
Expand Down Expand Up @@ -819,11 +822,14 @@ static void _tree_selection_change(GtkTreeSelection *selection, dt_lib_masks_t *
{
dt_masks_point_group_t *fpt =
(dt_masks_point_group_t *)malloc(sizeof(dt_masks_point_group_t));
fpt->formid = id;
fpt->parentid = grid;
fpt->state = DT_MASKS_STATE_USE;
fpt->opacity = 1.0f;
grp->points = g_list_append(grp->points, fpt);
if(fpt)
{
fpt->formid = id;
fpt->parentid = grid;
fpt->state = DT_MASKS_STATE_USE;
fpt->opacity = 1.0f;
grp->points = g_list_append(grp->points, fpt);
}
// we eventually set the "show masks" icon of iops
if(nb == 1 && (form->type & DT_MASKS_GROUP))
{
Expand Down
13 changes: 9 additions & 4 deletions src/libs/metadata.c
Original file line number Diff line number Diff line change
Expand Up @@ -789,10 +789,13 @@ static void add_rights_preset(dt_lib_module_t *self, char *name, char *string)
const unsigned int params_size = strlen(string) + metadata_nb;

char *params = calloc(sizeof(char), params_size);
memcpy(params + 4, string, params_size - metadata_nb);
dt_lib_presets_add(name, self->plugin_name, self->version(),
params, params_size, TRUE, 0);
free(params);
if(params)
{
memcpy(params + 4, string, params_size - metadata_nb);
dt_lib_presets_add(name, self->plugin_name, self->version(),
params, params_size, TRUE, 0);
free(params);
}
}

void init_presets(dt_lib_module_t *self)
Expand Down Expand Up @@ -908,6 +911,8 @@ void *get_params(dt_lib_module_t *self, int *size)
}

char *params = (char *)malloc(*size);
if(!params)
return NULL;

int pos = 0;

Expand Down
13 changes: 8 additions & 5 deletions src/libs/metadata_view.c
Original file line number Diff line number Diff line change
Expand Up @@ -232,11 +232,14 @@ static void _lib_metadata_init_queue(dt_lib_module_t *self)
for(int i = md_size - 1; i >= 0; i--)
{
dt_lib_metadata_info_t *m = g_malloc0(sizeof(dt_lib_metadata_info_t));
m->name = (char *)_get_label(i);
m->value = g_strdup(NODATA_STRING);
m->index = m->order = i;
m->visible = _is_metadata_ui(i);
d->metadata = g_list_prepend(d->metadata, m);
if(m)
{
m->name = (char *)_get_label(i);
m->value = g_strdup(NODATA_STRING);
m->index = m->order = i;
m->visible = _is_metadata_ui(i);
d->metadata = g_list_prepend(d->metadata, m);
}
}
}

Expand Down
68 changes: 38 additions & 30 deletions src/libs/modulegroups.c
Original file line number Diff line number Diff line change
Expand Up @@ -1402,6 +1402,8 @@ static void _preset_from_string(dt_lib_module_t *self, gchar *txt, gboolean edit
{
dt_lib_modulegroups_basic_item_t *item
= (dt_lib_modulegroups_basic_item_t *)g_malloc0(sizeof(dt_lib_modulegroups_basic_item_t));
if(!item)
continue;
item->id = g_strdup(gr2[j]);
_basics_init_item(item);

Expand All @@ -1425,16 +1427,18 @@ static void _preset_from_string(dt_lib_module_t *self, gchar *txt, gboolean edit
const int nb = g_strv_length(gr2);
if(nb > 2)
{
dt_lib_modulegroups_group_t *group
= (dt_lib_modulegroups_group_t *)g_malloc0(sizeof(dt_lib_modulegroups_group_t));
group->name = g_strdup(gr2[0]);
group->icon = g_strdup(gr2[1]);
// gr2[2] is reserved for eventual future use
for(int j = 3; j < nb; j++)
dt_lib_modulegroups_group_t *group = g_malloc0(sizeof(dt_lib_modulegroups_group_t));
if(group)
{
group->modules = g_list_append(group->modules, g_strdup(gr2[j]));
group->name = g_strdup(gr2[0]);
group->icon = g_strdup(gr2[1]);
// gr2[2] is reserved for eventual future use
for(int j = 3; j < nb; j++)
{
group->modules = g_list_append(group->modules, g_strdup(gr2[j]));
}
res = g_list_prepend(res, group);
}
res = g_list_prepend(res, group);
}
g_strfreev(gr2);
}
Expand Down Expand Up @@ -2204,12 +2208,13 @@ static int _lib_modulegroups_basics_module_toggle_action(dt_lib_module_t *self,

if(!found_item)
{
dt_lib_modulegroups_basic_item_t *item
= (dt_lib_modulegroups_basic_item_t *)g_malloc0(sizeof(dt_lib_modulegroups_basic_item_t));
item->id = action_id;
_basics_init_item(item);

d->basics = g_list_append(d->basics, item);
dt_lib_modulegroups_basic_item_t *item = g_malloc0(sizeof(dt_lib_modulegroups_basic_item_t));
if(item)
{
item->id = action_id;
_basics_init_item(item);
d->basics = g_list_append(d->basics, item);
}
}
else
{
Expand Down Expand Up @@ -2264,12 +2269,13 @@ static void _manage_editor_basics_add(GtkWidget *widget,
g_free(action_id);
else
{
dt_lib_modulegroups_basic_item_t *item
= (dt_lib_modulegroups_basic_item_t *)g_malloc0(sizeof(dt_lib_modulegroups_basic_item_t));
item->id = action_id;
_basics_init_item(item);

d->edit_basics = g_list_append(d->edit_basics, item);
dt_lib_modulegroups_basic_item_t *item = g_malloc0(sizeof(dt_lib_modulegroups_basic_item_t));
if(item)
{
item->id = action_id;
_basics_init_item(item);
d->edit_basics = g_list_append(d->edit_basics, item);
}
_manage_editor_basics_update_list(self);
}
}
Expand Down Expand Up @@ -3468,16 +3474,18 @@ static void _manage_editor_group_add(GtkWidget *widget,
dt_lib_module_t *self)
{
dt_lib_modulegroups_t *d = (dt_lib_modulegroups_t *)self->data;
dt_lib_modulegroups_group_t *gr =
(dt_lib_modulegroups_group_t *)g_malloc0(sizeof(dt_lib_modulegroups_group_t));
gr->name = g_strdup(_("new"));
gr->icon = g_strdup("basic");
d->edit_groups = g_list_append(d->edit_groups, gr);

// we update the group list
GtkWidget *vb2 = _manage_editor_group_init_modules_box(self, gr);
gtk_box_pack_start(GTK_BOX(d->preset_groups_box), vb2, FALSE, TRUE, 0);
gtk_widget_show_all(vb2);
dt_lib_modulegroups_group_t *gr = g_malloc0(sizeof(dt_lib_modulegroups_group_t));
if(gr)
{
gr->name = g_strdup(_("new"));
gr->icon = g_strdup("basic");
d->edit_groups = g_list_append(d->edit_groups, gr);

// we update the group list
GtkWidget *vb2 = _manage_editor_group_init_modules_box(self, gr);
gtk_box_pack_start(GTK_BOX(d->preset_groups_box), vb2, FALSE, TRUE, 0);
gtk_widget_show_all(vb2);
}

// and we update arrows
_manage_editor_group_update_arrows(d->preset_groups_box);
Expand Down
5 changes: 5 additions & 0 deletions src/libs/print_settings.c
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,11 @@ static int write_image(dt_imageio_module_data_t *data,

d->params->buf =
(uint16_t *)malloc((size_t)3 * (d->bpp == 8?1:2) * d->head.width * d->head.height);
if(!d->params->buf)
{
dt_print(DT_DEBUG_ALWAYS, "[print] unable to allocate memory for image %s\n", filename);
return 1;
}

if(d->bpp == 8)
{
Expand Down
Loading

0 comments on commit 517c0ed

Please sign in to comment.