Carcigenicate
Carcigenicate

Reputation: 45691

I'm getting a Segfault with certain compilers only

I wrote the following code to encrypt text (it's far from done):

#include <iostream>
#include <string>

using namespace std;

class Device {
    string input;
    string encrypted;
    string decrypted;
    string key;
    int keysize;

public:
    Device(string inp) {
        input = inp;
        keysize = 0;
    }

    int give_key(string ikey) {
        key = ikey;
        keysize = key.size();
    }

    string encrypt() {
        if(key != "") {
            for(int enc = 0, keyiter = 0; enc < input.size(); ++enc, ++keyiter) {
                cout << "Starting loop #" << enc << endl;
                if(keyiter == keysize) keyiter = 0; //Reset key iter

                int coffset = key[keyiter] - 48;
                cout << "Setting offset to " << coffset << endl;

                int oldspot = input[enc];
                cout << "Setting oldspot to " << oldspot << endl;

                cout << "Char " << input[enc] << " changed to ";
                //Skip non-letters between upper and lowercase letters
                if((input[enc] + coffset) > 90 && (input[enc] + coffset) < 97) {
                    int holder = 90 - oldspot;
                    input[enc] = (coffset - holder) + 97;

                }
                else if((input[enc] + coffset) > 122) {
                    int holder = 122 - oldspot;
                    input[enc] = (coffset - holder) + 65;
                }
                else {
                    input[enc] += coffset;
                }
                cout << input[enc] << endl;
                cout << endl;
            }
        }
        else {
            cout << "Key not set!" << endl;
        }

        cout << "Setting Encrypted" << endl;
        encrypted = input;

        cout << "Finishing encryption function" << endl;
    }

    string get_encrypt() {
        return encrypted;
    }

};


int main()
{
    Device device("AbCdEfG");
    device.give_key("12345");
    device.encrypt();
    cout << device.get_encrypt() << endl;

    cout << "Done" << endl;
    int wait; cin >> wait;
}

On C4Droid (an Android IDE), immediately after displaying "Finishing encryption function", it gives a Segmentation Fault. C4Droid doesn't have a debugger, so I loaded it into Code::Blocks on my PC and ran the debugger, but it runs without an error. I tried different combos of inputs and keys, but it always runs fine. Does anyone know what the issue might be? My question is solely about the odd segfault.

Upvotes: 2

Views: 361

Answers (1)

user3072164
user3072164

Reputation:

Add a return statement to your encrypt method. The current code throws an segmentation fault for me too, because the return type string is a class and after executing encrypt the returned object is tried to be destructed, however the return value was never initialized properly.

Also notice all the warnings:

../src/Test.cpp: In member function ‘int Device::give_key(std::string)’:
../src/Test.cpp:26:1: warning: no return statement in function returning non-void [-Wreturn-type]
../src/Test.cpp: In member function ‘std::string Device::encrypt()’:
../src/Test.cpp:30:49: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
../src/Test.cpp:63:1: warning: no return statement in function returning non-void [-Wreturn-type]
../src/Test.cpp: In member function ‘std::string Device::decrypt()’:
../src/Test.cpp:70:1: warning: no return statement in function returning non-void [-Wreturn-type]

Upvotes: 6

Related Questions