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 sys
|
||||||
import ctypes
|
import ctypes
|
||||||
import platform
|
import platform
|
||||||
|
from contextlib import ExitStack
|
||||||
|
|
||||||
from pycriu import images
|
from pycriu import images
|
||||||
from . import elf
|
from . import elf
|
||||||
|
|
@ -872,6 +873,9 @@ class coredump_generator:
|
||||||
# current process.
|
# current process.
|
||||||
return b"\0" * size
|
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 \
|
if vma["status"] & status["VMA_FILE_SHARED"] or \
|
||||||
vma["status"] & status["VMA_FILE_PRIVATE"]:
|
vma["status"] & status["VMA_FILE_PRIVATE"]:
|
||||||
# Open file before iterating vma pages
|
# Open file before iterating vma pages
|
||||||
|
|
@ -879,10 +883,11 @@ class coredump_generator:
|
||||||
off = vma["pgoff"]
|
off = vma["pgoff"]
|
||||||
|
|
||||||
files = self.reg_files
|
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:
|
try:
|
||||||
f = open(fname, 'rb')
|
f = stack.enter_context(open(fname, 'rb'))
|
||||||
except FileNotFoundError:
|
except FileNotFoundError:
|
||||||
sys.exit('Required file %s not found.' % fname)
|
sys.exit('Required file %s not found.' % fname)
|
||||||
|
|
||||||
|
|
@ -947,10 +952,6 @@ class coredump_generator:
|
||||||
|
|
||||||
buf += page[n_skip:n_skip + n_read]
|
buf += page[n_skip:n_skip + n_read]
|
||||||
|
|
||||||
# Don't forget to close file.
|
|
||||||
if f is not None:
|
|
||||||
f.close()
|
|
||||||
|
|
||||||
return buf
|
return buf
|
||||||
|
|
||||||
def _gen_cmdline(self, pid):
|
def _gen_cmdline(self, pid):
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue