[Mesa-dev] [PATCH] glsl/cache: correct asprintf error handling

Nicolai Hähnle nhaehnle at gmail.com
Thu Nov 3 10:03:54 UTC 2016


From: Nicolai Hähnle <nicolai.haehnle at amd.com>

>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."
---
 src/compiler/glsl/cache.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/src/compiler/glsl/cache.c b/src/compiler/glsl/cache.c
index 64a34f0..e74c27d 100644
--- a/src/compiler/glsl/cache.c
+++ b/src/compiler/glsl/cache.c
@@ -409,21 +409,22 @@ choose_random_file_matching(const char *dir_path,
          break;
 
       count++;
    }
 
    if (entry == NULL) {
       closedir(dir);
       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);
 
    return filename;
 }
 
 /* Is entry a regular file, and not having a name with a trailing
  * ".tmp"
  */
 static bool
@@ -490,22 +491,21 @@ evict_random_item(struct program_cache *cache)
    int a, b;
    size_t size;
 
    /* With a reasonably-sized, full cache, (and with keys generated
     * from a cryptographic hash), we can choose two random hex digits
     * and reasonably expect the directory to exist with a file in it.
     */
    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);
 
    free(dir_path);
 
    if (size) {
       p_atomic_add(cache->size, - size);
       return;
    }
-- 
2.7.4



More information about the mesa-dev mailing list