std::ofstream invalid write of size 4



  • Hi Leute,
    ich hab hier etwas was ich mir nicht erklären kann. Ich hab mir eine Fehler Klasse geschrieben aber das klappt noch nicht so wie ich mir das vorstelle.

    Die .log wird noch beschrieben danach geht nichts mehr.

    Hier mein Code:

    try {
          foo();
        }
        catch (Err& excep) {
          excep.say();
          return;
        }
    

    Error Klasse:

    #ifndef ERR_H
    #define ERR_H
    
    #include <string>
    #include <cstring>
    #include <ctime>
    #include <cstdio>
    #include <exception>
    #include <iostream>
    #include <ios>
    #include <fstream>
    
    namespace {
    	char msg[10000] = "Failed: ";
      size_t msglen = 0;
    }
    
    class Err : public std::exception {
    public:
      Err(const std::string& filename) {
        if (msglen == 0) {
          msglen = strlen(msg);
        }
        int maxRestlen = sizeof(msg) - msglen - 1;
        if (maxRestlen > 0) {
          strncpy(msg+msglen, filename.c_str(), maxRestlen);
        }
      }
    
      /**
       * @brief Function to log the errors into err.log.
       */
      void say() {
        std::ofstream error_log;
        error_log.open("/tmp/err.log", std::ios::app);
        if (!error_log.is_open()) {
          std::cerr << "Error log not open!" << '\n';
        }
    
        time(&Rawtime);
        Timeinfo = localtime(&Rawtime);
        error_log << msg << ' ' << asctime(Timeinfo);
      }
    
      /**
       * @brief Function to return the error into the shell.
       *
       * @return msg Returns the error message in msg.
       */
      const char *what() const throw() {
        return msg;
      }
    private:
      time_t      Rawtime;
      struct tm * Timeinfo;
    };
    
    #endif
    

    Und der Fehler aus valgrind:

    ==13996== Invalid write of size 4
    ==13996==    at 0x595DD02: std::__basic_file<char>::close() (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.16)
    ==13996==    by 0x59610D0: std::basic_filebuf<char, std::char_traits<char> >::close() (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.16)
    ==13996==    by 0x596167C: std::basic_ofstream<char, std::char_traits<char> >::~basic_ofstream() (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.16)
    ==13996==    by 0x41D7AF: Xqaerr::say() (xqaerr.h:43)
    ==13996==    by 0x41CC05: exdb::ExdbDaemon::Daemon() (exdbdaemon.cpp:109)
    ==13996==    by 0x4254B7: main (main.cpp:76)
    ==13996==  Address 0xffffffffffffffb4 is not stack'd, malloc'd or (recently) free'd
    


  • Oh sry hab den throw vergessen:

    throw Err("bla bla bla");
    


  • Wenn error_log nicht geöffnet werden kann stürzt er ab.



  • nwp3 schrieb:

    Wenn error_log nicht geöffnet werden kann stürzt er ab.

    Warum? Es wird doch einfach nichts ins Logfile geschrieben. Das ist kein Absturz.

    Schlimmer finde ich, wenn 2 Exception Instanzen existieren, überschreiben sie sich gegenseitig den globalen Puffer.



  • nwp3 schrieb:

    Wenn error_log nicht geöffnet werden kann stürzt er ab.

    Aber wird sie doch anscheint, sonst könnte ich den Fehler doch gar nicht in der .log nachlesen.

    tntnet schrieb:

    nwp3 schrieb:

    Wenn error_log nicht geöffnet werden kann stürzt er ab.

    Warum? Es wird doch einfach nichts ins Logfile geschrieben. Das ist kein Absturz.

    Schlimmer finde ich, wenn 2 Exception Instanzen existieren, überschreiben sie sich gegenseitig den globalen Puffer.

    Doch wird.

    Ja das mit den 2 Instanzen ist richtig. Aber ist das der Fehler hier? Ich hab allerdings nur eine.


  • Mod

    Ich hab mir eine Fehler Klasse geschrieben aber das klappt noch nicht so wie ich mir das vorstelle.

    Tu dir selbst einen riesigen Gefallen und lösch' die Scheiße den Code.

    Wie wäre es damit?

    class LogException : public std::runtime_error
    {
    public:
    	LogException( std::string const& str ) :
    		std::runtime_error(str)
    	{
    		std::ofstream stream("exception_log.txt", std::ios::app);
    		stream << "- LogException -\n";
    		stream << "time since start = " << float(clock()) / CLOCKS_PER_SEC << '\n';
    		// stream << "time()  = " << time(nullptr)  << '\n';
    		stream << "Message: " << str << '\n';
    	}
    };
    

    bad_alloc s kannst du dabei vorerst weglassen.



  • Mit deinem Code:

    ==16039== Invalid write of size 4
    ==16039==    at 0x595DD02: std::__basic_file<char>::close() (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.16)
    ==16039==    by 0x59610D0: std::basic_filebuf<char, std::char_traits<char> >::close() (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.16)
    ==16039==    by 0x596167C: std::basic_ofstream<char, std::char_traits<char> >::~basic_ofstream() (in /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.16)
    ==16039==    by 0x41CDDD: Xqaerr::Xqaerr(std::string const&) (xqaerr.h:21)
    ==16039==    by 0x41C6D5: exdb::ExdbDaemon::auth() (exdbdaemon.cpp:171)
    ==16039==    by 0x41BBDA: exdb::ExdbDaemon::Daemon() (exdbdaemon.cpp:100)
    ==16039==    by 0x42477B: main (main.cpp:76)
    ==16039==  Address 0xffffffffffffffbb is not stack'd, malloc'd or (recently) free'd
    

Anmelden zum Antworten