abusenius / insaned

Simple daemon for polling button presses on SANE scanners
GNU General Public License v2.0
43 stars 13 forks source link

Segmentation fault in Genesys backend #15

Open bsdice opened 4 years ago

bsdice commented 4 years ago

Hi...

When no scanner is attached, there is a segfault happening here: https://github.com/abusenius/insaned/blob/5a74d858d8e36a1bd337eaace01d82d9c64c573d/src/InsaneDaemon.cpp#L61

Backtrace:

Thread 1 "insaned" received signal SIGSEGV, Segmentation fault.
0x00007ffff3d89d70 in std::_Function_handler<void (), genesys::StaticInit<std::vector<genesys::Genesys_USB_Device_Entry, std::allocator<genesys::Genesys_USB_Device_Entry> > >::init<>()::{lambda()#1}>::_M_invoke(std::_Any_data const&) () from /usr/lib/sane/libsane-genesys.so.1
(gdb) bt
#0  0x00007ffff3d89d70 in std::_Function_handler<void (), genesys::StaticInit<std::vector<genesys::Genesys_USB_Device_Entry, std::allocator<genesys::Genesys_USB_Device_Entry> > >::init<>()::{lambda()#1}>::_M_invoke(std::_Any_data const&) () from /usr/lib/sane/libsane-genesys.so.1
#1  0x00007ffff3d76645 in genesys::run_functions_at_backend_exit() () from /usr/lib/sane/libsane-genesys.so.1
#2  0x00007ffff3d03bb6 in genesys::sane_exit_impl() () from /usr/lib/sane/libsane-genesys.so.1
#3  0x00007ffff3d03bf7 in sane_genesys_exit () from /usr/lib/sane/libsane-genesys.so.1
#4  0x00007ffff7f9641e in sane_dll_exit () from /usr/lib/libsane.so.1
#5  0x000055555555a7a4 in InsaneDaemon::~InsaneDaemon (this=0x55555556a760 <InsaneDaemon::mInstance>, __in_chrg=<optimized out>) at src/InsaneDaemon.cpp:61
#6  0x00007ffff7ac8db7 in __run_exit_handlers () from /usr/lib/libc.so.6
#7  0x00007ffff7ac8f5e in exit () from /usr/lib/libc.so.6
#8  0x00007ffff7ab1159 in __libc_start_main () from /usr/lib/libc.so.6
#9  0x000055555555762e in _start ()

So I modified the function like this, no idea if this is correct though:

diff --git a/src/InsaneDaemon.cpp b/src/InsaneDaemon.cpp
index db8ff1c..41bbd71 100644
--- a/src/InsaneDaemon.cpp
+++ b/src/InsaneDaemon.cpp
@@ -53,18 +53,18 @@ InsaneDaemon::InsaneDaemon()

 InsaneDaemon::~InsaneDaemon() noexcept
 {
+    SANE_Handle mHandle2 = mHandle;
     log("Exiting...", 1);
     close();
     try {
-        mHandle = nullptr;
-        log("Calling sane_exit", 1);
-        sane_exit();
-
+        if (mHandle2) {
+            log("Calling sane_exit", 1);
+            sane_exit();
+        }
+        log("Finished", 1);
         ::close(0);
         ::close(1);
         ::close(2);
-
-        log("Finished", 1);
     } catch (...) {
         log("Error calling sane_exit!", 0);
     }