<div dir="ltr">&result[0] is const, you're going to have to const cast it to do that unfortunately. Regardless though, manual new / delete is almost never the right solution. For example, the original code could be replaced with <div><br></div><div>```</div><div>std::vector<char> Buffer(FileName.begin(), FileName.end());</div><div>Buffer.push_back(0);</div><div>memcpy(&Buffer[0], FileName.data(), FileName.size());</div><div>PathRemoveFileSpecA(&Buffer[0]);</div><div>return std::string(&Buffer[0]);</div><div>```</div><div><br></div><div>and it would be better than manually new'ing and deleting. Unless you can accept the const_cast (which I'm not convinced we should in this case), the only alternative is stack allocating the buffer.</div><div><br></div><div>BTW, if we could use llvm then this would be a perfect use case for StringRef :) Oh well</div></div><br><div class="gmail_quote"><div dir="ltr">On Tue, Dec 6, 2016 at 2:03 PM Adrian McCarthy via Phabricator <<a href="mailto:reviews@reviews.llvm.org">reviews@reviews.llvm.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">amccarth added inline comments.<br class="gmail_msg">
<br class="gmail_msg">
<br class="gmail_msg">
================<br class="gmail_msg">
Comment at: lib/Fuzzer/FuzzerIOWindows.cpp:144<br class="gmail_msg">
std::string DirName(const std::string &FileName) {<br class="gmail_msg">
- assert(0 && "Unimplemented");<br class="gmail_msg">
+ char *Tmp = new char[FileName.size() + 1];<br class="gmail_msg">
+ memcpy(Tmp, FileName.c_str(), FileName.size() + 1);<br class="gmail_msg">
----------------<br class="gmail_msg">
Instead of managing memory yourself, I would just use another string as the non-const buffer. PathRemoveFileSpace may replace one of the characters with '\0', so all you have to do is resize the result.<br class="gmail_msg">
<br class="gmail_msg">
std::string result = FileName;<br class="gmail_msg">
PathRemoveFileSpecA(&result[0]);<br class="gmail_msg">
result.resize(std::strlen(result.c_str());<br class="gmail_msg">
return result;<br class="gmail_msg">
<br class="gmail_msg">
<br class="gmail_msg">
<br class="gmail_msg">
Repository:<br class="gmail_msg">
rL LLVM<br class="gmail_msg">
<br class="gmail_msg">
<a href="https://reviews.llvm.org/D27475" rel="noreferrer" class="gmail_msg" target="_blank">https://reviews.llvm.org/D27475</a><br class="gmail_msg">
<br class="gmail_msg">
<br class="gmail_msg">
<br class="gmail_msg">
</blockquote></div>