wayland: Move buffer copy into mime_data_list_add() It makes it clearer who owns the memory, and more reasonable to free it on failure in the creating function. (and, of course, pacifies static analysis.)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
diff --git a/src/video/wayland/SDL_waylanddatamanager.c b/src/video/wayland/SDL_waylanddatamanager.c
index 72dfdda..2a4c9c5 100644
--- a/src/video/wayland/SDL_waylanddatamanager.c
+++ b/src/video/wayland/SDL_waylanddatamanager.c
@@ -185,8 +185,16 @@ mime_data_list_add(struct wl_list* list,
{
int status = 0;
size_t mime_type_length = 0;
-
SDL_MimeDataList *mime_data = NULL;
+ void *internal_buffer = NULL;
+
+ if (buffer != NULL) {
+ internal_buffer = SDL_malloc(length);
+ if (internal_buffer == NULL) {
+ return SDL_OutOfMemory();
+ }
+ SDL_memcpy(internal_buffer, buffer, length);
+ }
mime_data = mime_data_list_find(list, mime_type);
@@ -211,8 +219,10 @@ mime_data_list_add(struct wl_list* list,
if (mime_data->data != NULL) {
SDL_free(mime_data->data);
}
- mime_data->data = buffer;
+ mime_data->data = internal_buffer;
mime_data->length = length;
+ } else {
+ SDL_free(internal_buffer);
}
return status;
@@ -264,18 +274,7 @@ int Wayland_data_source_add_data(SDL_WaylandDataSource *source,
const void *buffer,
size_t length)
{
- int status = 0;
- if (length > 0) {
- void *internal_buffer = SDL_malloc(length);
- if (internal_buffer == NULL) {
- status = SDL_OutOfMemory();
- } else {
- SDL_memcpy(internal_buffer, buffer, length);
- status = mime_data_list_add(&source->mimes, mime_type,
- internal_buffer, length);
- }
- }
- return status;
+ return mime_data_list_add(&source->mimes, mime_type, buffer, length);
}
SDL_bool