Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/ansi-c/cprover_library.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ std::string get_cprover_library_text(
const struct cprover_library_entryt cprover_library[],
const std::string &prologue)
{
std::ostringstream library_text(prologue);
// the default mode is ios_base::out which means subsequent write to the
// stream will overwrite the original content
std::ostringstream library_text(prologue, std::ios_base::ate);
Copy link
Contributor

@hannes-steffenhagen-diffblue hannes-steffenhagen-diffblue Feb 25, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#include <iostream>
#include <sstream>
int main(void) {
  std::ostringstream send_help{"SOS"};
  send_help << "WAT";
  std::cout << send_help.str() << '\n';
}

Try it online!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WAT is the point of this constructor...
A potentially more obvious fix might be

std::ostringstream library_text;
library_text << prologue

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@thk123 In a slightly less humorous note, I can see uses for this (for example, if you need to guarantee some minimum width for the output so you add in a bunch of filler characters to override first), the “wtf” here is that this is the default, which I think few sane people would expect.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

std::ostringstream library_text(prologue);
library_text.seekp(0, library_text.end);

would also work.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd personally lean either towards the solution @thk123 suggested, or to the original solution @xbauch had in the PR. I think the alternative using seekp just looks even more odd :-) Though I appreciate the oddness comes from the C++ spec here.


std::size_t count=0;

Expand Down