[Mesa-dev] [PATCH] glsl/cache: correct asprintf error handling
Edward O'Callaghan
funfunctor at folklore1984.net
Fri Nov 4 09:08:26 UTC 2016
Good spot,
Reviewed-by: Edward O'Callaghan <funfunctor at folklore1984.net>
On 11/03/2016 09:03 PM, Nicolai Hähnle wrote:
> 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;
> }
>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: OpenPGP digital signature
URL: <https://lists.freedesktop.org/archives/mesa-dev/attachments/20161104/04b02666/attachment.sig>
More information about the mesa-dev
mailing list