diff mbox series

[kms-test,4/7] kmstest: Fix exception handling in CRCReader constructor and destructor

Message ID 20220731152024.24090-5-laurent.pinchart@ideasonboard.com (mailing list archive)
State New
Delegated to: Kieran Bingham
Headers show
Series Miscellaneous fixes and improvements | expand

Commit Message

Laurent Pinchart July 31, 2022, 3:20 p.m. UTC
If an exception occurs in the CRCReader constructor, the self.dir,
self.ctrl and self.data members may not be set. This causes another
exception to be thrown by the destructor when it tries to access those
members. Fix it by initializing all the members first, and only closing
the dir and ctrl file descriptors if they have been successfully opened.

Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
---
 tests/kmstest.py | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
diff mbox series

Patch

diff --git a/tests/kmstest.py b/tests/kmstest.py
index 177110ebd894..960c27f301ce 100755
--- a/tests/kmstest.py
+++ b/tests/kmstest.py
@@ -168,17 +168,21 @@  class CRCReader(object):
 
     def __init__(self, crtc):
         self.pipe = crtc.idx
+        self.ctrl = -1
+        self.dir = -1
+        self.data = -1
 
         # Hardcode the device minor to 0 as the KMSTest constructor opens the
         # default card object.
         self.dir = os.open(f'/sys/kernel/debug/dri/0/crtc-{self.pipe}/crc', 0)
         self.ctrl = os.open('control', os.O_WRONLY, dir_fd = self.dir)
-        self.data = -1
 
     def __del__(self):
         self.stop()
-        os.close(self.ctrl)
-        os.close(self.dir)
+        if self.ctrl != -1:
+            os.close(self.ctrl)
+        if self.dir != -1:
+            os.close(self.dir)
 
     def start(self, source):
         os.write(self.ctrl, source.encode('ascii'))