Skip to content

Commit

Permalink
libponyc: resolve relative paths in use "path:..." statements
Browse files Browse the repository at this point in the history
The use of `pony_realpath` (which calls the POSIX `realpath` underneath)
is convenient, but brings a change in behaviour: before, we have passed
just about anything into `prog->libpaths`, regardless of the directory's
existence. With `realpath`, nonexistent directories will make the call
return an error.

To remediate the impact a bit, I've added an `errorf()` statement.

However, to make this round, I think we might have to either check for
existence in both cases, or don't do that in either one.

Signed-off-by: Stephan Renatus <srenatus@chef.io>
  • Loading branch information
srenatus committed Dec 18, 2018
1 parent 8a0a7d4 commit 6baf622
Showing 1 changed file with 29 additions and 3 deletions.
32 changes: 29 additions & 3 deletions src/libponyc/pkg/program.c
Original file line number Diff line number Diff line change
Expand Up @@ -148,14 +148,40 @@ bool use_path(ast_t* use, const char* locator, ast_t* name,
pass_opt_t* options)
{
(void)name;
char resolved[FILENAME_MAX];
char buf[FILENAME_MAX];

if(is_path_absolute(locator)) {
if(strlen(locator) > FILENAME_MAX)
return false;

strcpy(resolved, locator);
} else {
// resolve path relative to pkg path
ast_t* pkg_ast = ast_nearest(use, TK_PACKAGE);
const char* pkg_path = package_path(pkg_ast);

if(strlen(pkg_path) + strlen(locator) >= FILENAME_MAX)
return false;

strcpy(buf, pkg_path);
strcat(buf, "/");
strcat(buf, locator);

if(pony_realpath(buf, resolved) != resolved) {
errorf(options->check.errors, package_filename(pkg_ast),
"resolve \"path:%s\": %s", locator, strerror(errno));
return false;
}
}

const char* libpath = quoted_locator(options, use, locator);
const char* libpath = quoted_locator(options, use, resolved);

if(libpath == NULL)
return false;

ast_t* p = ast_nearest(use, TK_PROGRAM);
program_t* prog = (program_t*)ast_data(p);
ast_t* prog_ast = ast_nearest(use, TK_PROGRAM);
program_t* prog = (program_t*)ast_data(prog_ast);
pony_assert(prog->lib_args == NULL); // Not yet built args

if(strlist_find(prog->libpaths, libpath) != NULL) // Ignore duplicate
Expand Down

0 comments on commit 6baf622

Please sign in to comment.