#Caught fatal signal 11

48 messages · Page 1 of 1 (latest)

undone ice
main pelicanBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

little onyx
#

Error 11 is a segmentation fault, usually it should print something along those lines, but maybe not in your case.

Anyways, try using a debugger to step through your code, though I have a feeling the problem is that you’re receiving an invalid character in the input, which you then try to access in the symbol table, which in turn doesn’t exist, and you try to dereference .end()

#

Or alternatively, someone is sending the program a kill -s 11

undone ice
#

So debugging on Windows doesn't give me anything as it run fine

#

And Linux is remote and I can't run debugger there

little onyx
#

You can use gdb on Linux (if you compile with -g) - gdb is fully command line

main pelicanBOT
#
Debugging with GDB

Compile your program with -g flag and run your program using gdb: gdb yourprogname. From there, you can debug using GDB commands. Use help to list commands and their options.

Break

Set a breakpoint to pause execution at a certain line or a function:

  • break main
  • b 42
Run

Run your program inside gdb after setting breakpoints:

  • run
  • r
Print

Print value of expression:

  • print my_var
  • p (char) ch
Walk & Step

Execute next line of code, where next stays in the function and step enters functions:

  • n
  • s
Continue

Continue execution until (Nth) next breakpoint

  • continue
  • c 3
Backtrace

Print backtrace of all or N stack frames:

  • backtrace -full
  • bt 3
undone ice
little onyx
#

In that case, you can try guarding line 76 with an .has to check if the key appears in your map

undone ice
# little onyx In that case, you can try guarding line 76 with an `.has` to check if the key ap...

Changed to:

if (morse_code_table.find(symbol) != morse_code_table.end()) {
    decoded += morse_code_table.at(symbol);
} else {
    std::cerr << "Unknown Morse code symbol: " << symbol << std::endl;
}

and still Caught fatal signal 11

Then I tried commenting out all the logic in decode_morse(const std::string &) leaving only returning at least empty string. And still getting the error! I guess it is somehow related to files/streams?

#

Oh no, not really. If I only return an empty string there's no problem. But if I comment some part of it, the problem is reproducible:

std::string decode_morse(const std::string &morse)
{
    std::string decoded;
    std::istringstream iss(morse);
    std::string token;

    /* Loop through each Morse code sequence separated by '|' characters */
    while (std::getline(iss, token, '|')) {
        std::istringstream iss2(token);
        std::string symbol;

        /* Loop through each Morse code symbol separated by ' ' characters */
        // while (std::getline(iss2, symbol, ' ')) {
        //     if (symbol.empty()) {
        //         decoded += '!';
        //     } else {
        //         if (morse_code_table.find(symbol) != morse_code_table.end()) {
        //             decoded += morse_code_table.at(symbol);
        //         } else {
        //             std::cerr << "Unknown Morse code symbol: " << symbol << std::endl;
        //         }
        //     }
        // }
    }

    return decoded;
}
#

So the problem is with:

std::string decode_morse(const std::string &morse)
{
    std::string decoded;
    std::istringstream iss(morse);
    std::string token;

    while (std::getline(iss, token, '|')) {
        std::istringstream iss2(token);
        std::string symbol;
#

Any ideas?

little onyx
#

Try making a copy of the Morse Input, and making the stream off of that, since it’s a const reference it can be placed in read only, and string stream can be constructed via move(from non-const, per spec, but maybe there’s a bug) *or there’s a problem accessing the file/with the file itself, which leads the kernel to segfault

undone ice
#

What I find weird is that on my Windows computer with MSVC it compiles and works just fine.

undone ice
little onyx
#

You can put it in a new std::string variable, that way you can also verify its contents afterwards, or remove the & from your parameter

undone ice
little onyx
#

What happens if you comment out your both whiles?

undone ice
#
std::string decode_morse(std::string morse)
{
    std::string decoded;
    std::istringstream iss(morse);
    std::string token;

    /* Loop through each Morse code sequence separated by '|' characters */
    // while (std::getline(iss, token, '|')) {
    //     std::istringstream iss2(token);
    //     std::string symbol;
    //
    //     /* Loop through each Morse code symbol separated by ' ' characters */
    //     while (std::getline(iss2, symbol, ' ')) {
    //         if (symbol.empty()) {
    //             decoded += '!';
    //         } else {
    //             if (morse_code_table.find(symbol) != morse_code_table.end()) {
    //                 decoded += morse_code_table.at(symbol);
    //             } else {
    //                 decoded += '!';
    //             }
    //         }
    //     }
    // }

    return decoded;
}
little onyx
#

Try commenting out the istringstream, maybe your server has a buggy libcpp or links/loads different ones

undone ice
#

WTF?

little onyx
#

It seems your libc++ is borked, either by pure chance, or intentionally (malicious or to stop students in case of this being hw)

#

If it’s not academia, I would suggest cordoning the server in case it’s infected

little onyx
#

Also, I just realized I forgot to ask, are you compiling on the same server as running the code, maybe there’s a subtle linker issue that istringstream is at a different location than what the linker may have put in, shouldn’t, linkers are pretty robust, but in this case it’s worth a shot

undone ice
undone ice
charred sierra
#

??? a string stream doesn't open a file, though

undone ice
undone ice
charred sierra
#

it's a string stream

#

it holds a string buffer

undone ice
#

Would it be difficult to convert stringstream to something else?

#

I don't want to do this since my code works, but apparently I have to....

charred sierra
#

you could use a string and just string.find in a loop or something

undone ice
#

And now getting

  what():  std::bad_alloc```
![sadcat](https://cdn.discordapp.com/emojis/850519552995622913.webp?size=128 "sadcat")
#

It doesn't even give me a line number

#
std::string
decode_morse(std::string morse)
{
    std::string decoded;
    size_t pos = 0;

    /* Loop through each Morse code sequence separated by '|' chars */
    while ((pos = morse.find('|')) != std::string::npos) {
        std::string token = morse.substr(0, pos);
        morse.erase(0, pos + 1);

        size_t pos2 = 0;
        /* Loop through each Morse code symbol separated by ' ' chars */
        while ((pos2 = token.find(' ')) != std::string::npos) {
            std::string symbol = token.substr(0, pos2);
            token.erase(0, pos2 + 1);

            if (symbol.empty()) {
                decoded += '!';
            } else {
                if (morse_code_table.find(symbol) != morse_code_table.end()) {
                    decoded += morse_code_table.at(symbol);
                } else {
                    decoded += '!';
                }
            }
        }
    }

    /* Check if the last token is not empty, indicating no end pause symbol */
    if (!morse.empty()) {
        decoded.pop_back();
        decoded += '!';
    }

    return decoded;
}
near field
#

Does it happen on your machine, or does it only happen on the remote server?

#

because this could mean that no more memory was available for allocation

little onyx
#

I have a feeling this isn't the standard standard library but rather a self-made one. You can try iterating directly, I'm using std::string_view to keep the copy operations to a minimum:

#include <iostream>
#include <map>
#include <string>

std::map<std::string_view, char> morse_code_table = {
    {".-", 'A'},     {"-...", 'B'},   {"-.-.", 'C'},   {"-..", 'D'},
    {".", 'E'},      {"..-.", 'F'},   {"--.", 'G'},    {"....", 'H'},
    {"..", 'I'},     {".---", 'J'},   {"-.-", 'K'},    {".-..", 'L'},
    {"--", 'M'},     {"-.", 'N'},     {"---", 'O'},    {".--.", 'P'},
    {"--.-", 'Q'},   {".-.", 'R'},    {"...", 'S'},    {"-", 'T'},
    {"..-", 'U'},    {"...-", 'V'},   {".--", 'W'},    {"-..-", 'X'},
    {"-.--", 'Y'},   {"--..", 'Z'},   {"-----", '0'},  {".----", '1'},
    {"..---", '2'},  {"...--", '3'},  {"....-", '4'},  {".....", '5'},
    {"-....", '6'},  {"--...", '7'},  {"---..", '8'},  {"----.", '9'},
    {".-.-.-", '.'}, {"--..--", ','}, {"..--..", '?'}, {"-....-", '-'},
    {"-.--.", '('},  {"-.--.-", ')'}, {"---...", ':'}, {"-.-.-.", ';'},
    {"-..-.", '/'},  {"-...-", '='},  {".-.-.", '+'},  {"..--.-", '_'},
    {".----.", ' '}, {"-.-.--", '!'}, {"-.-.-.", '&'}, {".-...", '&'},
    {"-..-.", '\"'}, {"..--.-", '@'}, {"|", ' '}};

char decode_symbol(const std::string_view &symbol) {
    if (morse_code_table.find(symbol) != morse_code_table.end()) {
        return morse_code_table.at(symbol);
    } else {
        return '!';
    }
}

std::string decode_morse(const std::string &morse) {
    int current_start = 0;
    std::string decoded = "";
    for (int i = 0; i <= morse.size(); i++) {
        if (i == morse.size() || morse.at(i) == '|') {
            decoded += decode_symbol(morse.substr(current_start, i - current_start));
            current_start = i+1;
        }
    }
    return decoded;
}

int main() {
    std::string a;
    std::cin >> a;
    std::cout << decode_morse(a);
}
little onyx
vital pivotBOT
#
Program Output
THE!MEANING!OF!LIFE:!42!