From 29d64bc8e06d6809ac0c68b7b5459a8a51667769 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Thu, 15 Sep 2016 17:31:25 +0200 Subject: QEvdevKeyboardHandler: use RAII in create()/ctor Coverity somewhat rightfully complained that the FD may be leaked in certain cases, e.g. when the new-expression throws. Yes, the plugin is compiled with exceptions disabled, but the code is still a bug waiting to happen, because it's too easy to just add an early return to the function and leak the FD that way. Fix by writing a small RAII class for FDs (can't use QSharedPointer, since it's not a pointer we're dealing with). It's quite generically named, in anticipation that it might come in handy elsewhere, too. Coverity-Id: 89046 Change-Id: I83d1ed3f11219065d2248c129ed191a651f617c7 Reviewed-by: Thiago Macieira --- .../input/evdevkeyboard/qevdevkeyboardhandler_p.h | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) (limited to 'src/platformsupport/input/evdevkeyboard/qevdevkeyboardhandler_p.h') diff --git a/src/platformsupport/input/evdevkeyboard/qevdevkeyboardhandler_p.h b/src/platformsupport/input/evdevkeyboard/qevdevkeyboardhandler_p.h index 84c251c3c2..b08f30b6ee 100644 --- a/src/platformsupport/input/evdevkeyboard/qevdevkeyboardhandler_p.h +++ b/src/platformsupport/input/evdevkeyboard/qevdevkeyboardhandler_p.h @@ -123,12 +123,25 @@ inline QDataStream &operator<<(QDataStream &ds, const QEvdevKeyboardMap::Composi return ds << c.first << c.second << c.result; } +class QFdContainer +{ + int m_fd; + Q_DISABLE_COPY(QFdContainer); +public: + explicit QFdContainer(int fd = -1) Q_DECL_NOTHROW : m_fd(fd) {} + ~QFdContainer() { reset(); } + + int get() const Q_DECL_NOTHROW { return m_fd; } + + int release() Q_DECL_NOTHROW { int result = m_fd; m_fd = -1; return result; } + void reset() Q_DECL_NOTHROW; +}; class QEvdevKeyboardHandler : public QObject { Q_OBJECT public: - QEvdevKeyboardHandler(const QString &device, int fd, bool disableZap, bool enableCompose, const QString &keymapFile); + QEvdevKeyboardHandler(const QString &device, QFdContainer &fd, bool disableZap, bool enableCompose, const QString &keymapFile); ~QEvdevKeyboardHandler(); enum KeycodeAction { @@ -181,7 +194,7 @@ private: void switchLed(int, bool); QString m_device; - int m_fd; + QFdContainer m_fd; QSocketNotifier *m_notify; // keymap handling -- cgit v1.2.3