Improve detail::script_specific_filename()

- To handle relative paths that don't start with a '.'
- To handle getcwd() failures
This commit is contained in:
Jon Siwek 2021-03-31 14:18:36 -07:00
parent d3261238dd
commit ab1f1f8360

View file

@ -29,16 +29,19 @@ std::string script_specific_filename(const StmtPtr& body)
auto bl_f = body_loc->filename; auto bl_f = body_loc->filename;
ASSERT(bl_f != nullptr); ASSERT(bl_f != nullptr);
if ( bl_f[0] == '.' && if ( (bl_f[0] != '.' && bl_f[0] != '/') ||
(bl_f[1] == '/' || (bl_f[1] == '.' && bl_f[2] == '/')) ) (bl_f[0] == '.' && (bl_f[1] == '/' ||
(bl_f[1] == '.' && bl_f[2] == '/'))) )
{ {
// Add working directory to avoid collisions over the // Add working directory to avoid collisions over the
// same relative name. // same relative name.
static std::string working_dir; static std::string working_dir;
if ( working_dir.size() == 0 ) if ( working_dir.empty() )
{ {
char buf[8192]; char buf[8192];
getcwd(buf, sizeof buf); if ( ! getcwd(buf, sizeof buf) )
reporter->InternalError("getcwd failed: %s", strerror(errno));
working_dir = buf; working_dir = buf;
} }