diff options
author | Marc Mutz <marc.mutz@kdab.com> | 2016-09-15 17:31:25 +0200 |
---|---|---|
committer | Marc Mutz <marc.mutz@kdab.com> | 2016-09-18 10:55:27 +0000 |
commit | 29d64bc8e06d6809ac0c68b7b5459a8a51667769 (patch) | |
tree | 36f42b943be5365cd1f7ce27d2e7c820697da28b /src/platformsupport/input/evdevkeyboard/qevdevkeyboardhandler_p.h | |
parent | 76adb6c29f9284a3d7cceb8fb09c3bb7c4cd5e1b (diff) |
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 <thiago.macieira@intel.com>
Diffstat (limited to 'src/platformsupport/input/evdevkeyboard/qevdevkeyboardhandler_p.h')
-rw-r--r-- | src/platformsupport/input/evdevkeyboard/qevdevkeyboardhandler_p.h | 17 |
1 files changed, 15 insertions, 2 deletions
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 |