Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Corruption of const input string that use C.O.W. #31

Open
GoogleCodeExporter opened this issue Apr 30, 2015 · 0 comments
Open

Corruption of const input string that use C.O.W. #31

GoogleCodeExporter opened this issue Apr 30, 2015 · 0 comments

Comments

@GoogleCodeExporter
Copy link

GCC-4.6 seems to use C.O.W. (Copy-On-Write) for std::strings.
When calling std::string modp::b85_decode(const std::string& s), the input 
string s is corrupted.
This is due to the fact that although 's' is "copied" into an internal copy 
'x', the copy is not really done. A further call to the non-const b85_decode() 
corrupts s, since it uses const_cast to strip the const from the .data() 
returned results (presumably to avoid a copy at all costs on all 
implementations).

What steps will reproduce the problem?
1. On gcc (with C.O.W.), pass an std::string 's' to modp::b85_decode(const 
std::string& s);
2. 's' is now corrupted.

What is the expected output? What do you see instead?
A call to a function that accepts const should not alter the const argument.

What version of the product are you using? On what operating system?
gcc 4.6 on Linux.

Please provide any additional information below.

One possible fix is to change the implementation to:
     inline std::string b85_decode(const std::string& s)
     {
         // std::string x(s); // original impl.
         std::string x(s.data(), s.size()); // new impl. - force copy even with COW
         b85_decode(x);
         return x;
     }

Original issue reported on code.google.com by [email protected] on 1 Nov 2012 at 8:37

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant