Skip to content

Segmentation fault in Genesys backend #15

@bsdice

Description

@bsdice

Hi...

When no scanner is attached, there is a segfault happening here:

sane_exit();

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);
     }
  • Introduced mHandle2 to save mHandle before close(); nulls it
  • Don't null mHandle twice (in close() and here)
  • Only call sane_exit() if mHandle2 contains a handle
  • Print log(Finished) before closing all stdandard descriptors

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions