Skip to content

Commit 96df6d6

Browse files
authored
Merge pull request #17582 from ralfbrown/view_alloc_checks
[maint] check memory allocs in views
2 parents 517c0ed + 0d8a738 commit 96df6d6

File tree

3 files changed

+93
-65
lines changed

3 files changed

+93
-65
lines changed

src/views/map.c

Lines changed: 60 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1170,7 +1170,8 @@ static void _view_map_draw_main_location(dt_map_t *lib,
11701170
if(!d)
11711171
{
11721172
d = g_malloc0(sizeof(dt_location_draw_t));
1173-
lib->loc.others = g_list_append(lib->loc.others, d);
1173+
if(d)
1174+
lib->loc.others = g_list_append(lib->loc.others, d);
11741175
}
11751176
if(d)
11761177
{
@@ -1545,52 +1546,58 @@ static void _view_map_changed_callback_delayed(gpointer user_data)
15451546
if(p[i].cluster_id == NOISE)
15461547
{
15471548
dt_map_image_t *entry = (dt_map_image_t *)calloc(1, sizeof(dt_map_image_t));
1548-
entry->imgid = p[i].imgid;
1549-
entry->group = p[i].cluster_id;
1550-
entry->group_count = 1;
1551-
entry->longitude = p[i].x * 180 / M_PI;
1552-
entry->latitude = p[i].y * 180 / M_PI;
1553-
entry->group_same_loc = TRUE;
1554-
if(sel_imgs)
1555-
entry->selected_in_group = g_list_find((GList *)sel_imgs,
1556-
GINT_TO_POINTER(entry->imgid))
1557-
? TRUE : FALSE;
1558-
lib->images = g_slist_prepend(lib->images, entry);
1549+
if(entry)
1550+
{
1551+
entry->imgid = p[i].imgid;
1552+
entry->group = p[i].cluster_id;
1553+
entry->group_count = 1;
1554+
entry->longitude = p[i].x * 180 / M_PI;
1555+
entry->latitude = p[i].y * 180 / M_PI;
1556+
entry->group_same_loc = TRUE;
1557+
if(sel_imgs)
1558+
entry->selected_in_group = g_list_find((GList *)sel_imgs,
1559+
GINT_TO_POINTER(entry->imgid))
1560+
? TRUE : FALSE;
1561+
lib->images = g_slist_prepend(lib->images, entry);
1562+
}
15591563
}
15601564
else if(!processed[p[i].cluster_id])
15611565
{
15621566
processed[p[i].cluster_id] = TRUE;
15631567
group = p[i].cluster_id;
15641568
dt_map_image_t *entry = (dt_map_image_t *)calloc(1, sizeof(dt_map_image_t));
1565-
entry->imgid = p[i].imgid;
1566-
entry->group = p[i].cluster_id;
1567-
entry->group_same_loc = TRUE;
1568-
entry->selected_in_group = (sel_imgs && g_list_find((GList *)sel_imgs,
1569-
GINT_TO_POINTER(p[i].imgid)))
1570-
? TRUE : FALSE;
1571-
const double lon = p[i].x, lat = p[i].y;
1572-
1573-
for(int j = 0; j < img_count; j++)
1569+
if(entry)
15741570
{
1575-
if(p[j].cluster_id == group)
1571+
entry->imgid = p[i].imgid;
1572+
entry->group = p[i].cluster_id;
1573+
entry->group_same_loc = TRUE;
1574+
entry->selected_in_group = (sel_imgs && g_list_find((GList *)sel_imgs,
1575+
GINT_TO_POINTER(p[i].imgid)))
1576+
? TRUE : FALSE;
1577+
const double lon = p[i].x, lat = p[i].y;
1578+
1579+
for(int j = 0; j < img_count; j++)
15761580
{
1577-
entry->group_count++;
1578-
entry->longitude += p[j].x;
1579-
entry->latitude += p[j].y;
1580-
if(entry->group_same_loc && (p[j].x != lon || p[j].y != lat))
1581+
if(p[j].cluster_id == group)
15811582
{
1582-
entry->group_same_loc = FALSE;
1583-
}
1584-
if(sel_imgs && !entry->selected_in_group)
1585-
{
1586-
if(g_list_find((GList *)sel_imgs, GINT_TO_POINTER(p[j].imgid)))
1587-
entry->selected_in_group = TRUE;
1583+
entry->group_count++;
1584+
entry->longitude += p[j].x;
1585+
entry->latitude += p[j].y;
1586+
if(entry->group_same_loc && (p[j].x != lon || p[j].y != lat))
1587+
{
1588+
entry->group_same_loc = FALSE;
1589+
}
1590+
if(sel_imgs && !entry->selected_in_group)
1591+
{
1592+
if(g_list_find((GList *)sel_imgs, GINT_TO_POINTER(p[j].imgid)))
1593+
entry->selected_in_group = TRUE;
1594+
}
15881595
}
15891596
}
1597+
entry->latitude = entry->latitude * 180 / M_PI / entry->group_count;
1598+
entry->longitude = entry->longitude * 180 / M_PI / entry->group_count;
1599+
lib->images = g_slist_prepend(lib->images, entry);
15901600
}
1591-
entry->latitude = entry->latitude * 180 / M_PI / entry->group_count;
1592-
entry->longitude = entry->longitude * 180 / M_PI / entry->group_count;
1593-
lib->images = g_slist_prepend(lib->images, entry);
15941601
}
15951602
}
15961603
free(processed);
@@ -3092,28 +3099,29 @@ static void _view_map_dnd_get_callback(GtkWidget *widget,
30923099
if(imgs_nb)
30933100
{
30943101
uint32_t *imgs = malloc(sizeof(uint32_t) * imgs_nb);
3095-
int i = 0;
3096-
for(GList *l = lib->selected_images;
3097-
l;
3098-
l = g_list_next(l))
3102+
if(imgs)
30993103
{
3100-
imgs[i++] = GPOINTER_TO_INT(l->data);
3104+
int i = 0;
3105+
for(GList *l = lib->selected_images;
3106+
l;
3107+
l = g_list_next(l))
3108+
{
3109+
imgs[i++] = GPOINTER_TO_INT(l->data);
3110+
}
3111+
gtk_selection_data_set(selection_data,
3112+
gtk_selection_data_get_target(selection_data),
3113+
_DWORD, (guchar *)imgs, imgs_nb * sizeof(uint32_t));
3114+
free(imgs);
31013115
}
3102-
gtk_selection_data_set(selection_data,
3103-
gtk_selection_data_get_target(selection_data),
3104-
_DWORD, (guchar *)imgs, imgs_nb * sizeof(uint32_t));
3105-
free(imgs);
31063116
}
31073117
}
31083118
else if(lib->loc.main.id > 0)
31093119
{
31103120
// move of location
3111-
uint32_t *imgs = malloc(sizeof(uint32_t));
3112-
imgs[0] = -1;
3121+
uint32_t imgs[1] = { -1 };
31133122
gtk_selection_data_set(selection_data,
31143123
gtk_selection_data_get_target(selection_data),
31153124
_DWORD, (guchar *)imgs, sizeof(uint32_t));
3116-
free(imgs);
31173125
}
31183126
}
31193127
break;
@@ -3304,9 +3312,11 @@ static void _bin_points(const dt_map_t *lib,
33043312
// allocate pointers for all of the latitude bins at this
33053313
// longitude
33063314
db.geo_bins[lon_bin] = (geo_bin_t*)calloc(num_lat_bins, sizeof(geo_bin_t));
3307-
3308-
for(int l = 0; l < num_lat_bins; l++)
3309-
db.geo_bins[lon_bin][l].points = NO_NEXT_POINT;
3315+
if(db.geo_bins[lon_bin])
3316+
{
3317+
for(int l = 0; l < num_lat_bins; l++)
3318+
db.geo_bins[lon_bin][l].points = NO_NEXT_POINT;
3319+
}
33103320
}
33113321
// push the point on the front of the linked list for its bin
33123322
db.points[i].next = db.geo_bins[lon_bin][lat_bin].points;

src/views/tethering.c

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -211,8 +211,13 @@ static int _tethering_write_image(dt_imageio_module_data_t *data,
211211
{
212212
_tethering_format_t *d = (_tethering_format_t *)data;
213213
d->buf = (float *)malloc(sizeof(float) * 4 * d->head.width * d->head.height);
214-
memcpy(d->buf, in, sizeof(float) * 4 * d->head.width * d->head.height);
215-
return 0;
214+
if(d->buf)
215+
{
216+
memcpy(d->buf, in, sizeof(float) * 4 * d->head.width * d->head.height);
217+
return 0;
218+
}
219+
else
220+
return 1;
216221
}
217222

218223
#define MARGIN DT_PIXEL_APPLY_DPI(20)
@@ -589,11 +594,14 @@ void enter(dt_view_t *self)
589594

590595
// register listener
591596
lib->listener = g_malloc0(sizeof(dt_camctl_listener_t));
592-
lib->listener->data = lib;
593-
lib->listener->image_downloaded = _camera_capture_image_downloaded;
594-
lib->listener->request_image_path = _camera_request_image_path;
595-
lib->listener->request_image_filename = _camera_request_image_filename;
596-
dt_camctl_register_listener(darktable.camctl, lib->listener);
597+
if(lib->listener)
598+
{
599+
lib->listener->data = lib;
600+
lib->listener->image_downloaded = _camera_capture_image_downloaded;
601+
lib->listener->request_image_path = _camera_request_image_path;
602+
lib->listener->request_image_filename = _camera_request_image_filename;
603+
dt_camctl_register_listener(darktable.camctl, lib->listener);
604+
}
597605
}
598606

599607
void leave(dt_view_t *self)

src/views/view.c

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1325,10 +1325,15 @@ GSList *dt_mouse_action_create_simple(GSList *actions,
13251325
const char *const description)
13261326
{
13271327
dt_mouse_action_t *a = (dt_mouse_action_t *)calloc(1, sizeof(dt_mouse_action_t));
1328-
a->action = type;
1329-
a->mods = accel;
1330-
g_strlcpy(a->name, description, sizeof(a->name));
1331-
return g_slist_append(actions, a);
1328+
if(a)
1329+
{
1330+
a->action = type;
1331+
a->mods = accel;
1332+
g_strlcpy(a->name, description, sizeof(a->name));
1333+
return g_slist_append(actions, a);
1334+
}
1335+
else
1336+
return actions;
13321337
}
13331338

13341339
GSList *dt_mouse_action_create_format(GSList *actions,
@@ -1338,10 +1343,15 @@ GSList *dt_mouse_action_create_format(GSList *actions,
13381343
const char *const replacement)
13391344
{
13401345
dt_mouse_action_t *a = (dt_mouse_action_t *)calloc(1, sizeof(dt_mouse_action_t));
1341-
a->action = type;
1342-
a->mods = accel;
1343-
g_snprintf(a->name, sizeof(a->name), format_string, replacement);
1344-
return g_slist_append(actions, a);
1346+
if(a)
1347+
{
1348+
a->action = type;
1349+
a->mods = accel;
1350+
g_snprintf(a->name, sizeof(a->name), format_string, replacement);
1351+
return g_slist_append(actions, a);
1352+
}
1353+
else
1354+
return actions;
13451355
}
13461356

13471357
static gchar *_mouse_action_get_string(dt_mouse_action_t *ma)

0 commit comments

Comments
 (0)