mirror of
https://github.com/checkpoint-restore/criu.git
synced 2026-07-17 16:47:50 +00:00
coredump: fix file handle leak in _gen_mem_chunk
Use ExitStack to ensure the conditionally-opened file for FILE_SHARED/FILE_PRIVATE VMAs is always closed, even if an exception occurs during seek or page iteration. Assisted-by: Claude Code (claude-opus-4-6) Signed-off-by: Adrian Reber <areber@redhat.com>
This commit is contained in:
parent
b0057d1873
commit
7e482031b6
1 changed files with 67 additions and 66 deletions
|
|
@ -32,6 +32,7 @@ import io
|
|||
import sys
|
||||
import ctypes
|
||||
import platform
|
||||
from contextlib import ExitStack
|
||||
|
||||
from pycriu import images
|
||||
from . import elf
|
||||
|
|
@ -872,6 +873,9 @@ class coredump_generator:
|
|||
# current process.
|
||||
return b"\0" * size
|
||||
|
||||
# ExitStack ensures the file opened below (if any) is closed
|
||||
# when the block exits, even if an exception is raised.
|
||||
with ExitStack() as stack:
|
||||
if vma["status"] & status["VMA_FILE_SHARED"] or \
|
||||
vma["status"] & status["VMA_FILE_PRIVATE"]:
|
||||
# Open file before iterating vma pages
|
||||
|
|
@ -879,10 +883,11 @@ class coredump_generator:
|
|||
off = vma["pgoff"]
|
||||
|
||||
files = self.reg_files
|
||||
fname = next(filter(lambda x: x["id"] == shmid, files))["name"]
|
||||
fname = next(
|
||||
filter(lambda x: x["id"] == shmid, files))["name"]
|
||||
|
||||
try:
|
||||
f = open(fname, 'rb')
|
||||
f = stack.enter_context(open(fname, 'rb'))
|
||||
except FileNotFoundError:
|
||||
sys.exit('Required file %s not found.' % fname)
|
||||
|
||||
|
|
@ -947,10 +952,6 @@ class coredump_generator:
|
|||
|
||||
buf += page[n_skip:n_skip + n_read]
|
||||
|
||||
# Don't forget to close file.
|
||||
if f is not None:
|
||||
f.close()
|
||||
|
||||
return buf
|
||||
|
||||
def _gen_cmdline(self, pid):
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue