On Sat, 2012-06-23 at 09:06 +1000, Paul Colby wrote:
> Not sure I understand your question, but assuming it throws, I'd guess it
> throws an Exception object (exact class depends on the namespace)
> containing a message like:
>
> * when value is 0: "Not a valid syslog value: "
> * when value is 1: "ot a valid syslog value: "
> * when value is 100: whatever is in RAM starting 100 characters past the
> start of the literal, or a segfault if that location (through to the next
> NULL terminator) is not accessible to the program.
>
> As that what you were getting at? ;)
Exactly what I meant. And so soon after I posted the puzzle too!
I raised it because it is a a rather non intuitive bug, and at first
glance the code seems correct.
Incidentally this bug was picked up by compiling the code with clang
3.1. So a good argument for trying to compile with different compilers.
To be clear this bug is caused by the more C like features of C++:
The line in question is:
throw Exception("Not a valid syslog value: " + value);
"Not a valid syslog value: " has a const char* value (and returns a
pointer to the first character of the quoted string). This is subject to
pointer arithmetic so adding an integer will just advance the pointer.
The simplest fix (that I can think of) is
throw Exception("Not a valid syslog value: " +
boost::lexical_cast<std::string>(value))
This works because now the usual
std::string operator+(std::string&, std::string&);
defined in <string> which now matches by default converting the const
char* to the first string.
Andrew
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]