glsl/cache: correct asprintf error handling
authorNicolai Hähnle <nicolai.haehnle@amd.com>
Thu, 3 Nov 2016 09:23:17 +0000 (10:23 +0100)
committerNicolai Hähnle <nicolai.haehnle@amd.com>
Fri, 4 Nov 2016 09:28:08 +0000 (10:28 +0100)
From the manpage of asprintf:

   "If memory allocation wasn't possible, or some other error occurs,
    these functions will return -1, and the contents of strp are
    undefined."

Reviewed-by: Iago Toral Quiroga <itoral@igalia.com>
Reviewed-by: Edward O'Callaghan <funfunctor@folklore1984.net>
src/compiler/glsl/cache.c

index 64a34f06217b05952ed3af1a17622ebb6fee0877..e74c27d3000c6a3dcb2055d15617d53662bc9fb6 100644 (file)
@@ -416,7 +416,8 @@ choose_random_file_matching(const char *dir_path,
       return NULL;
    }
 
-   asprintf(&filename, "%s/%s", dir_path, entry->d_name);
+   if (asprintf(&filename, "%s/%s", dir_path, entry->d_name) < 0)
+      filename = NULL;
 
    closedir(dir);
 
@@ -497,8 +498,7 @@ evict_random_item(struct program_cache *cache)
    a = rand() % 16;
    b = rand() % 16;
 
-   asprintf (&dir_path, "%s/%c%c", cache->path, hex[a], hex[b]);
-   if (dir_path == NULL)
+   if (asprintf(&dir_path, "%s/%c%c", cache->path, hex[a], hex[b]) < 0)
       return;
 
    size = unlink_random_file_from_directory(dir_path);