Skip to content

Commit

Permalink
driver/toobj.cpp: writeModule: don't use StringRef::data to get a c_s…
Browse files Browse the repository at this point in the history
…tr (#4554)

The .data() method on a StringRef, returns a const char* to the first
character but there is no guarantee that the string is null
terminated.

In order to get a proper null terminated string it is
necessary to use .str() which will return a C++ std::string which
provided the .c_str() method to get the null terminated C string.

The invalid behavior can be seen by running (where the user doesn't
have acces to the inaccessible folder):
$ ldc2 <<< '' - -od=inaccessible/impossible
Error: failed to create output directory: inaccessible/impossible/__stdin_4416.o

The error message should only include inaccessible/impossible.

Signed-off-by: Andrei Horodniceanu <[email protected]>
  • Loading branch information
the-horo authored Jan 7, 2024
1 parent 530d41b commit f192187
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion driver/toobj.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ void writeModule(llvm::Module *m, const char *filename) {
if (!directory.empty()) {
if (auto ec = llvm::sys::fs::create_directories(directory)) {
error(Loc(), "failed to create output directory: %s\n%s",
directory.data(), ec.message().c_str());
directory.str().c_str(), ec.message().c_str());
fatal();
}
}
Expand Down

0 comments on commit f192187

Please sign in to comment.