[med-svn] [aghermann] 01/23: handle SIGBUS on unreadable edfs

andrei zavada hmmr-guest at moszumanska.debian.org
Wed Jan 4 22:59:24 UTC 2017


This is an automated email from the git hooks/post-receive script.

hmmr-guest pushed a commit to tag 1.1.2
in repository aghermann.

commit e4b3933aba5b2a8a24e7271266bf9743f67f8a17
Author: Andrei Zavada <hmmr at frdg>
Date:   Mon Jan 2 05:55:06 2017 +0200

    handle SIGBUS on unreadable edfs
    
    assuming it will get triggered in _parse_header or not at all, which can
    well be wrong.
---
 upstream/src/common/lang.hh      | 13 +++++++++++--
 upstream/src/common/libcommon.cc | 16 +++++++++++++++-
 upstream/src/libsigfile/edf.cc   | 35 +++++++++++++++++++++++++----------
 3 files changed, 51 insertions(+), 13 deletions(-)

diff --git a/upstream/src/common/lang.hh b/upstream/src/common/lang.hh
index 34e04b6..e8fe3ef 100644
--- a/upstream/src/common/lang.hh
+++ b/upstream/src/common/lang.hh
@@ -16,13 +16,22 @@
 #  include "config.h"
 #endif
 
-#include <cfloat>
-#include <cmath>
 #include <unistd.h>
 #include <stdio.h>
+#include <signal.h>
+#include <setjmp.h>
+#include <cfloat>
+#include <cmath>
 
 using namespace std;
 
+extern "C" {
+extern sigjmp_buf sj_env;
+
+void trip_SIGBUS(int sig, siginfo_t *siginfo, void *context);
+}
+
+
 namespace agh {
 
 // for functions to suppress some possibly benign exceptions:
diff --git a/upstream/src/common/libcommon.cc b/upstream/src/common/libcommon.cc
index cd8d896..4637d87 100644
--- a/upstream/src/common/libcommon.cc
+++ b/upstream/src/common/libcommon.cc
@@ -19,7 +19,7 @@
 #include <unistd.h>
 #include <errno.h>
 #include <wchar.h>
-#include <iconv.h>
+#include <setjmp.h>
 
 #include "string.hh"
 #include "alg.hh"
@@ -33,6 +33,20 @@
 using namespace std;
 
 
+extern "C" {
+
+sigjmp_buf sj_env;
+
+void trip_SIGBUS(int sig, siginfo_t *siginfo, void *context)
+{
+        // "The behaviour of a process is undefined after it returns
+        // normally from a signal-catching function for a SIGBUS,..."
+        // That said:
+
+        siglongjmp( sj_env, 1);
+}
+}
+
 string
 agh::str::
 svasprintf( const char* fmt, va_list ap)
diff --git a/upstream/src/libsigfile/edf.cc b/upstream/src/libsigfile/edf.cc
index ab01141..0f392c0 100644
--- a/upstream/src/libsigfile/edf.cc
+++ b/upstream/src/libsigfile/edf.cc
@@ -10,10 +10,11 @@
  */
 
 
-#include <sys/stat.h>
-#include <sys/mman.h>
 #include <unistd.h>
 #include <fcntl.h>
+#include <signal.h>
+#include <sys/stat.h>
+#include <sys/mman.h>
 #include <cinttypes>
 #include <fstream>
 #include <sstream>
@@ -103,11 +104,6 @@ set_recording_time( const string& s)
 
 
 
-
-
-
-
-
 #define EOA '$'
 
 namespace {
@@ -136,11 +132,16 @@ CEDFFile (const string& fname_, const int flags_, agh::log::CLogFacility* log_fa
         _mmapping =
                 mmap( NULL,
                       _fsize,
-                      PROT_READ | PROT_WRITE, MAP_SHARED,
+                      PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_POPULATE,
                       _fd, 0);
         if ( _mmapping == (void*)-1 ) {
                 close( _fd);
-                throw length_error ("CEDFFile::CEDFFile(): mmap error");
+                throw invalid_argument (explain_status(_status |= TStatus::mmap_error));
+        }
+        if ( sigsetjmp(sj_env, 1) ) {
+                munmap( _mmapping, _fsize);
+                close( _fd);
+                throw invalid_argument (explain_status(_status |= TStatus::mmap_error));
         }
 
       // parse header
@@ -213,13 +214,18 @@ CEDFFile (const string& fname_, const TSubtype subtype_, const int flags_,
         _mmapping =
                 mmap( NULL,
                       _fsize,
-                      PROT_READ | PROT_WRITE, MAP_SHARED,
+                      PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_POPULATE,
                       _fd,
                       0);
         if ( _mmapping == (void*)-1 ) {
                 close( _fd);
                 throw invalid_argument (explain_status(_status |= TStatus::mmap_error));
         }
+        if ( sigsetjmp(sj_env, 1) ) {
+                munmap( _mmapping, _fsize);
+                close( _fd);
+                throw invalid_argument (explain_status(_status |= TStatus::mmap_error));
+        }
 
       // fill out some essential header fields
         channels.resize( channels_.size());
@@ -446,6 +452,15 @@ int
 CEDFFile::
 _parse_header()
 {
+        {
+                struct sigaction lsa;
+                memset( (void*)&lsa, 0, sizeof(lsa));
+                lsa.sa_sigaction = trip_SIGBUS;
+                sigemptyset( &lsa.sa_mask);
+                sigaddset( &lsa.sa_mask, SIGBUS);
+                lsa.sa_flags = SA_SIGINFO;
+                sigaction(SIGBUS, &lsa, NULL);
+        }
         size_t  n_channels;
         try {
                 _fld_pos = 0;

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/debian-med/aghermann.git



More information about the debian-med-commit mailing list