1

I was dealing with a platform specific bug today in which on a Windows machine a certain string would be quite garbled, but not on a Mac. The bug had to do with several lines that did both explicit and implicit conversions between std::string and const char *. Basically, I had a function with the signature

void foo(const std::string &id);

where foo at some point prints the string. On Windows, if called like below it would print the id string with various levels of corruption (garbling the first few characters or as much as the whole string)

std::string mystring = bar();
const char *id = mystring.c_str();
foo(id); // pass the C style string in because I thought that's what it took

I corrected the error by calling foo correctly:

std::string mystring = bar();
foo(mystring);

I can't figure out a few things though, like

  • What was the source of the bug?
  • Why was it platform specific?
  • Is implicit conversion between const char * and std::string ever safe?
1
  • The third point's answer is not when it's a null string or not pointing to a valid string. Commented Jul 18, 2013 at 21:37

3 Answers 3

4

I'm going to take a wild guess and say that the code in your question isn't representative of your real code. The symptom sounds like memory being reused, which means the string isn't valid anymore at the time of the foo call. You could get that by using c_str on a temporary, for example const char *id = bar().c_str();.

Sign up to request clarification or add additional context in comments.

1 Comment

Totally right. In writing the question I changed the code removing bar().c_str() because I didn't think through the fact that the return from bar would be temporary. Thanks!!!
2

There are two likely possibilities, and from just what you've said, we can't tell which:

  1. foo mishandles the case where it gets a reference to a temporary. For example, perhaps it stashes a pointer to its parameter somewhere and other code uses it after foo returns.

  2. The data in mystring contains embedded zero bytes and the length is lost when you call c_str.

3 Comments

Hadn't thought of #2.
Embedded zero bytes would cause truncation, not garbling.
@MarkRansom: Truncation can cause garbling. For example, consider if the last byte in the string indicates its encoding format. Truncation would mean the last byte would be changed, causing the encoding format to be misunderstood, causing garbling. Or consider if multibyte is the default unless the string is an odd number of characters ... again, truncation would cause garbling.
0

I don't know what is going on, but is there any reason not to copy the result of c_str() into a buffer you own, call foo() on it, and destroy the buffer you created? If that doesn't fix it, it is a problem with foo() only rather than the implementation or some conflict between the implementation and foo().

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.