Search code examples
c++botan

Botan pipe failbit exception


When I followed the instructions in Botan document Pipe/Filter Message Processing, I was faced with an unexpected failbit error.

My code is very simple:

ifstream in("2.txt", ios::binary);
ofstream out("2.enc", ios::binary); 
AutoSeeded_RNG rng;
SymmetricKey key(rng, 16); // a random 128-bit key
InitializationVector iv(rng, 16); // a random 128-bit IV

Pipe pipe(get_cipher("AES-128/CBC/CTS", key, iv, ENCRYPTION), new DataSink_Stream(out));
pipe.start_msg();
in >> pipe;
pipe.end_msg();
cout << in.fail() << endl;

It would always output 1, since the failbit is always set to 1.

Is this a bug or is there something wrong with my code?


Solution

  • This is how C++ input streams work; when a read is attempted past EOF, failbit and eofbit are set.

    We can see from the documented Botan code that operator>> will throw an exception if only failbit is set after extraction is attempted; in short, you have nothing to worry about here. The computer is just telling you that you consumed all the input data1.


    1 This is not solely what eofbit means in the strictest sense, but the way that Botan's operator>> works (reading as much as it can) requires an attempted read past EOF; otherwise, it wouldn't know when to stop!