1

This function is a work in progress and is designed to read data from stdin for a simple integer calculator. The function works as intended when entering an operator and two operands: + 2 2. It also correctly reads from a file with similar formatting piped in on the command line. Despite this it will segfualt if the user hits enter on the command line or it reaches the end of a file. I'm not sure where I've gone wrong? What could be causing this behavior?

void process_input ()
{

while(!std::cin.eof())
{
    std::string line;
    std::string n;


    getline(std::cin, line);


    if (std::cin.fail())
        break;
    else if (line == "quit")
        return;

    std::istringstream stream(line);
    std::vector<std::string> input_cpy(0);
    while (stream >> n)
        input_cpy.push_back(n);

    //Clear global_input_cpy on each iteration and load the new line into it
    global_input_cpy.clear();
    for (int i = 0; i < input_cpy.size(); ++i)
        global_input_cpy.push_back(input_cpy[i]);



    if(input_cpy[0] == "+")
        sum(std::stol(input_cpy[1]), std::stol(input_cpy[2]));
    else if (input_cpy[0] == "*")
        multiply(std::stol(input_cpy[1]), std::stol(input_cpy[2]));
    else if (input_cpy[0] == "^")
        exponent(std::stol(input_cpy[1]), std::stol(input_cpy[2]));
    else
        std::cout << "input '" << input_cpy[0] << "'" << " unrecognized. skipping." << "\n";

    input_cpy.clear();
}
}
  • Please provide a complete example. `global_input_cpy` is not declared, `sum`, `multiply`, `exponent` are not defined. – Brian Cain Sep 25 '17 at 21:22
  • `while(!std::cin.eof())` probably won't bite you here, but know that it can lead to interesting bugs and much hair-pulling. More here: [Why is iostream::eof inside a loop condition considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) – user4581301 Sep 25 '17 at 21:33

1 Answers1

4

If no strings are read and therefore input_cpy is empty, you still try to access input_cpy[0] and this produces undefined behavior.

You need an assertion somewhere that input_cpy.size() >= 2 in order for these statements to have defined behavior:

if(input_cpy[0] == "+")
    sum(std::stol(input_cpy[1]), std::stol(input_cpy[2]));
else if (input_cpy[0] == "*")
    multiply(std::stol(input_cpy[1]), std::stol(input_cpy[2]));
else if (input_cpy[0] == "^")
    exponent(std::stol(input_cpy[1]), std::stol(input_cpy[2]));
cdhowie
  • 158,093
  • 24
  • 286
  • 300