nagyistoce / stringencoders

Automatically exported from code.google.com/p/stringencoders
Other
0 stars 0 forks source link

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

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
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 adisha...@gmail.com on 1 Nov 2012 at 8:37