OSDN Git Service

amdgpu: Cleanly handle ENOMEM on result in amdgpu_bo_list_create()
authorTom St Denis <tom.stdenis@amd.com>
Fri, 9 Oct 2015 16:46:40 +0000 (12:46 -0400)
committerAlex Deucher <alexander.deucher@amd.com>
Thu, 22 Oct 2015 16:36:55 +0000 (12:36 -0400)
Move the allocation of result prior to the IOCTL so we can cleanly
backtrack if the allocation fails.

Signed-off-by: Tom St Denis <tom.stdenis@amd.com>
Reviewed-by: Alex Deucher <alexander.deucher@amd.com>
amdgpu/amdgpu_bo.c

index 348da00..1a5a401 100644 (file)
@@ -591,6 +591,12 @@ int amdgpu_bo_list_create(amdgpu_device_handle dev,
        if (!list)
                return -ENOMEM;
 
+       *result = malloc(sizeof(struct amdgpu_bo_list));
+       if (!*result) {
+               free(list);
+               return -ENOMEM;
+       }
+
        memset(&args, 0, sizeof(args));
        args.in.operation = AMDGPU_BO_LIST_OP_CREATE;
        args.in.bo_number = number_of_resources;
@@ -608,10 +614,11 @@ int amdgpu_bo_list_create(amdgpu_device_handle dev,
        r = drmCommandWriteRead(dev->fd, DRM_AMDGPU_BO_LIST,
                                &args, sizeof(args));
        free(list);
-       if (r)
+       if (r) {
+               free(*result);
                return r;
+       }
 
-       *result = malloc(sizeof(struct amdgpu_bo_list));
        (*result)->dev = dev;
        (*result)->handle = args.out.list_handle;
        return 0;