public inbox for libabigail@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] elf-reader: reclaim fd and mem before break
@ 2022-12-20 13:06 Xiaole He
  2022-12-21 11:01 ` Dodji Seketeli
  0 siblings, 1 reply; 3+ messages in thread
From: Xiaole He @ 2022-12-20 13:06 UTC (permalink / raw)
  To: libabigail; +Cc: Xiaole He, Xiaole He

In 'src/abg-elf-reader.cc':

/* src/abg-elf-reader.cc begin */
1  void
2  locate_alt_ctf_debug_info()
3  {
4      ...
5      for (const auto& path : rdr.debug_info_root_paths())
6      {
7          ...
8          int fd;
9          if ((fd = open(file_path.c_str(), O_RDONLY)) == -1)
10             continue;
11
12         ...
13         Elf *hdl;
14         if ((hdl = elf_begin(fd, ELF_C_READ, nullptr)) == nullptr)
15             ...
16
17         alt_ctf_section =
18             elf_helpers::find_section(hdl, ".ctf", SHT_PROGBITS);
19         break;
20
21         elf_end(hdl);
22         close(fd);
23     }
24     ...
25 }
/* src/abg-elf-reader.cc end */

The file descriptor 'fd' and the memory that 'hdl' pointed to can have
a chance where they were only created but nerver been destroyed when
above code reach the line 19. Thus cause the leakage of file descriptor
and memory.

This leakage problem had already occured on our system, and the problem
finally cause process can not open any more file and complaint
'Errno 24: Too many open files'.

This patch fix above problem.

        * src/abg-elf-reader.cc (locate_alt_ctf_debug_info):
        reclaim fd and mem before break.

Signed-off-by: Xiaole He <hexiaole@kylinos.cn>
---
 src/abg-elf-reader.cc | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/abg-elf-reader.cc b/src/abg-elf-reader.cc
index c07f0655..979f0aae 100644
--- a/src/abg-elf-reader.cc
+++ b/src/abg-elf-reader.cc
@@ -453,10 +453,10 @@ struct reader::priv
 	  // unlikely .ctf was designed to be present in stripped file
 	  alt_ctf_section =
 	    elf_helpers::find_section(hdl, ".ctf", SHT_PROGBITS);
-          break;
 
 	  elf_end(hdl);
 	  close(fd);
+          break;
 	}
   }
 
-- 
2.27.0


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2022-12-22  1:10 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-12-20 13:06 [PATCH] elf-reader: reclaim fd and mem before break Xiaole He
2022-12-21 11:01 ` Dodji Seketeli
2022-12-22  1:09   ` Xiaole He

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).