From 241f29fba6ab4c584eeb4e7f53302aa92d2c11f4 Mon Sep 17 00:00:00 2001 From: Andrew Burgess Date: Wed, 11 Oct 2023 14:39:37 +0100 Subject: [PATCH] bfd/cache: change type used to track cached BFDs from int to unsigned Within bfd/cache.c change the type for max_open_files and open_files variables from int to unsigned. As a consequence of this, the return type for bfd_cache_max_open() is also changed from int to unsigned. Within bfd_cache_max_open I've left the local 'max' variable as an int, this should ensure that if the sysconf call fails, and returns -1, then the computed max value will be less than 10, which means max_open_files will be set to 10. If 'max' was changed to unsigned then, should the sysconf call fail, we'd end up with max becoming a very large positive number ... which is clearly not what we want. And, while I was auditing how open_files is used, I added an assert within bfd_cache_delete to ensure that we don't try to reduce open_files below zero. There should be no user visible change with this commit. --- bfd/cache.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/bfd/cache.c b/bfd/cache.c index 357a38da599..87c4bcd3148 100644 --- a/bfd/cache.c +++ b/bfd/cache.c @@ -67,11 +67,11 @@ enum cache_flag { /* The maximum number of files which the cache will keep open at one time. When needed call bfd_cache_max_open to initialize. */ -static int max_open_files = 0; +static unsigned max_open_files = 0; /* Set max_open_files, if not already set, to 12.5% of the allowed open file descriptors, but at least 10, and return the value. */ -static int +static unsigned bfd_cache_max_open (void) { if (max_open_files == 0) @@ -115,7 +115,7 @@ bfd_cache_max_open (void) /* The number of BFD files we have open. */ -static int open_files; +static unsigned open_files; /* Zero, or a pointer to the topmost BFD on the chain. This is used by the <> macro in @file{libbfd.h} to @@ -176,6 +176,7 @@ bfd_cache_delete (bfd *abfd) snip (abfd); abfd->iostream = NULL; + BFD_ASSERT (open_files > 0); --open_files; abfd->flags |= BFD_CLOSED_BY_CACHE; -- 2.30.2