wayland: Move buffer copy into mime_data_list_add()
authorRyan C. Gordon
Thu, 28 May 2020 14:57:10 -0400
changeset 1386825b93f5a6cbf
parent 13867 cf1e807e68e2
child 13869 1cd318c35a97
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.)
src/video/wayland/SDL_waylanddatamanager.c
     1.1 --- a/src/video/wayland/SDL_waylanddatamanager.c	Thu May 28 14:47:55 2020 -0400
     1.2 +++ b/src/video/wayland/SDL_waylanddatamanager.c	Thu May 28 14:57:10 2020 -0400
     1.3 @@ -185,8 +185,16 @@
     1.4  {
     1.5      int status = 0;
     1.6      size_t mime_type_length = 0;
     1.7 +    SDL_MimeDataList *mime_data = NULL;
     1.8 +    void *internal_buffer = NULL;
     1.9  
    1.10 -    SDL_MimeDataList *mime_data = NULL;
    1.11 +    if (buffer != NULL) {
    1.12 +        internal_buffer = SDL_malloc(length);
    1.13 +        if (internal_buffer == NULL) {
    1.14 +            return SDL_OutOfMemory();
    1.15 +        }
    1.16 +        SDL_memcpy(internal_buffer, buffer, length);
    1.17 +    }
    1.18  
    1.19      mime_data = mime_data_list_find(list, mime_type);
    1.20  
    1.21 @@ -211,8 +219,10 @@
    1.22          if (mime_data->data != NULL) {
    1.23              SDL_free(mime_data->data);
    1.24          }
    1.25 -        mime_data->data = buffer;
    1.26 +        mime_data->data = internal_buffer;
    1.27          mime_data->length = length;
    1.28 +    } else {
    1.29 +        SDL_free(internal_buffer);
    1.30      }
    1.31  
    1.32      return status;
    1.33 @@ -264,18 +274,7 @@
    1.34                                   const void *buffer,
    1.35                                   size_t length) 
    1.36  {
    1.37 -    int status = 0;
    1.38 -    if (length > 0) {
    1.39 -        void *internal_buffer = SDL_malloc(length);
    1.40 -        if (internal_buffer == NULL) {
    1.41 -            status = SDL_OutOfMemory();
    1.42 -        } else {
    1.43 -            SDL_memcpy(internal_buffer, buffer, length);
    1.44 -            status = mime_data_list_add(&source->mimes, mime_type, 
    1.45 -                                        internal_buffer, length);
    1.46 -        }
    1.47 -    }
    1.48 -    return status;
    1.49 +    return mime_data_list_add(&source->mimes, mime_type, buffer, length);
    1.50  }
    1.51  
    1.52  SDL_bool