Skip to content

Commit 8e00c68

Browse files
dt_image_t not available fixes (proposal shutdown part 5)
If we get `dt_image_t *img = dt_image_cache_get()` from the image cache img can be NULL for various reasons. In all those cases we must not de-reference img in any way and must use a meaningful value - like when getting the imgid - and avoid further processing on that data. We had that being checked in most cases, a few ones have been overseen yet.
1 parent f2eb1bb commit 8e00c68

File tree

2 files changed

+36
-22
lines changed

2 files changed

+36
-22
lines changed

src/common/grouping.c

+26-14
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ void dt_grouping_add_to_group(const dt_imgid_t group_id,
3939
dt_grouping_remove_from_group(image_id);
4040

4141
dt_image_t *img = dt_image_cache_get(darktable.image_cache, image_id, 'w');
42+
if(!img) return;
4243
img->group_id = group_id;
4344
dt_image_cache_write_release_info(darktable.image_cache, img,
4445
DT_IMAGE_CACHE_SAFE, "dt_grouping_add_to_group");
@@ -65,7 +66,7 @@ dt_imgid_t dt_grouping_remove_from_group(const dt_imgid_t image_id)
6566
GList *imgs = NULL;
6667

6768
const dt_image_t *img = dt_image_cache_get(darktable.image_cache, image_id, 'r');
68-
const int img_group_id = img->group_id;
69+
const dt_imgid_t img_group_id = img ? img->group_id : NO_IMGID;
6970
dt_image_cache_read_release(darktable.image_cache, img);
7071
if(img_group_id == image_id)
7172
{
@@ -84,10 +85,13 @@ dt_imgid_t dt_grouping_remove_from_group(const dt_imgid_t image_id)
8485
if(!dt_is_valid_imgid(new_group_id))
8586
new_group_id = other_id;
8687
dt_image_t *other_img = dt_image_cache_get(darktable.image_cache, other_id, 'w');
87-
other_img->group_id = new_group_id;
88-
dt_image_cache_write_release_info(darktable.image_cache, other_img,
88+
if(other_img)
89+
{
90+
other_img->group_id = new_group_id;
91+
dt_image_cache_write_release_info(darktable.image_cache, other_img,
8992
DT_IMAGE_CACHE_SAFE, "dt_grouping_add_to_group");
90-
imgs = g_list_prepend(imgs, GINT_TO_POINTER(other_id));
93+
imgs = g_list_prepend(imgs, GINT_TO_POINTER(other_id));
94+
}
9195
}
9296
sqlite3_finalize(stmt);
9397
if(dt_is_valid_imgid(new_group_id))
@@ -122,13 +126,15 @@ dt_imgid_t dt_grouping_remove_from_group(const dt_imgid_t image_id)
122126
{
123127
// change the group_id for this image.
124128
dt_image_t *wimg = dt_image_cache_get(darktable.image_cache, image_id, 'w');
125-
new_group_id = wimg->group_id;
126-
wimg->group_id = image_id;
127-
dt_image_cache_write_release_info(darktable.image_cache, wimg,
129+
if(wimg)
130+
{
131+
new_group_id = wimg->group_id;
132+
wimg->group_id = image_id;
133+
dt_image_cache_write_release_info(darktable.image_cache, wimg,
128134
DT_IMAGE_CACHE_SAFE, "dt_grouping_add_to_group");
129-
imgs = g_list_prepend(imgs, GINT_TO_POINTER(image_id));
130-
// refresh also the group leader which may be alone now
131-
imgs = g_list_prepend(imgs, GINT_TO_POINTER(img_group_id));
135+
imgs = g_list_prepend(imgs, GINT_TO_POINTER(image_id));
136+
// refresh also the group leader which may be alone now
137+
imgs = g_list_prepend(imgs, GINT_TO_POINTER(img_group_id));
132138
#ifdef USE_LUA
133139
dt_lua_async_call_alien(dt_lua_event_trigger_wrapper,
134140
0, NULL, NULL,
@@ -138,6 +144,7 @@ dt_imgid_t dt_grouping_remove_from_group(const dt_imgid_t image_id)
138144
LUA_ASYNC_TYPENAME, "dt_lua_image_t", GINT_TO_POINTER(img_group_id),
139145
LUA_ASYNC_DONE);
140146
#endif
147+
}
141148
}
142149
DT_CONTROL_SIGNAL_RAISE(DT_SIGNAL_IMAGE_INFO_CHANGED, imgs);
143150

@@ -150,8 +157,10 @@ dt_imgid_t dt_grouping_change_representative(const dt_imgid_t image_id)
150157
sqlite3_stmt *stmt;
151158

152159
dt_image_t *img = dt_image_cache_get(darktable.image_cache, image_id, 'r');
153-
const dt_imgid_t group_id = img->group_id;
160+
const dt_imgid_t group_id = img ? img->group_id : NO_IMGID;
154161
dt_image_cache_read_release(darktable.image_cache, img);
162+
if(!dt_is_valid_imgid(group_id))
163+
return group_id;
155164

156165
GList *imgs = NULL;
157166
DT_DEBUG_SQLITE3_PREPARE_V2(dt_database_get(darktable.db), "SELECT id FROM main.images WHERE group_id = ?1", -1,
@@ -161,11 +170,14 @@ dt_imgid_t dt_grouping_change_representative(const dt_imgid_t image_id)
161170
{
162171
const dt_imgid_t other_id = sqlite3_column_int(stmt, 0);
163172
dt_image_t *other_img = dt_image_cache_get(darktable.image_cache, other_id, 'w');
164-
other_img->group_id = image_id;
165-
dt_image_cache_write_release_info(darktable.image_cache, other_img,
173+
if(other_img)
174+
{
175+
other_img->group_id = image_id;
176+
dt_image_cache_write_release_info(darktable.image_cache, other_img,
166177
DT_IMAGE_CACHE_SAFE,
167178
"dt_grouping_change_representative");
168-
imgs = g_list_prepend(imgs, GINT_TO_POINTER(other_id));
179+
imgs = g_list_prepend(imgs, GINT_TO_POINTER(other_id));
180+
}
169181
}
170182
sqlite3_finalize(stmt);
171183
DT_CONTROL_SIGNAL_RAISE(DT_SIGNAL_IMAGE_INFO_CHANGED, imgs);

src/common/image.c

+10-8
Original file line numberDiff line numberDiff line change
@@ -1097,11 +1097,11 @@ void dt_image_set_raw_aspect_ratio(const dt_imgid_t imgid)
10971097
image->aspect_ratio = (float )image->p_width / (float )(MAX(1, image->p_height));
10981098
else
10991099
image->aspect_ratio = (float )image->p_height / (float )(MAX(1, image->p_width));
1100-
}
1101-
/* store */
1102-
dt_image_cache_write_release_info(darktable.image_cache, image,
1100+
/* store */
1101+
dt_image_cache_write_release_info(darktable.image_cache, image,
11031102
DT_IMAGE_CACHE_SAFE,
11041103
"dt_image_set_raw_aspect_ratio");
1104+
}
11051105
}
11061106

11071107
void dt_image_set_aspect_ratio_to(const dt_imgid_t imgid,
@@ -1160,17 +1160,19 @@ void dt_image_reset_aspect_ratio(const dt_imgid_t imgid, const gboolean raise)
11601160
dt_image_t *image = dt_image_cache_get(darktable.image_cache, imgid, 'w');
11611161

11621162
/* set image aspect ratio */
1163-
if(image) image->aspect_ratio = 0.f;
1164-
1165-
/* store in db, but don't synch XMP */
1166-
dt_image_cache_write_release_info(darktable.image_cache, image,
1163+
if(image)
1164+
{
1165+
image->aspect_ratio = 0.f;
1166+
/* store in db, but don't synch XMP */
1167+
dt_image_cache_write_release_info(darktable.image_cache, image,
11671168
DT_IMAGE_CACHE_RELAXED,
11681169
"dt_image_reset_aspect_ratio");
11691170

1170-
if(image && raise && darktable.collection->params.sorts[DT_COLLECTION_SORT_ASPECT_RATIO])
1171+
if(raise && darktable.collection->params.sorts[DT_COLLECTION_SORT_ASPECT_RATIO])
11711172
dt_collection_update_query(darktable.collection, DT_COLLECTION_CHANGE_RELOAD,
11721173
DT_COLLECTION_PROP_ASPECT_RATIO,
11731174
g_list_prepend(NULL, GINT_TO_POINTER(imgid)));
1175+
}
11741176
}
11751177

11761178
float dt_image_set_aspect_ratio(const dt_imgid_t imgid, const gboolean raise)

0 commit comments

Comments
 (0)