Skip to content

Commit a227cd4

Browse files
gh-31: Canonicalize the program path on startup.
1 parent 3600856 commit a227cd4

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

src/main.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,16 @@ int main(int argc, char** argv) {
425425
(void)verbose_flag;
426426

427427
char* src = NULL;
428-
const char* source_label = path;
428+
char* source_label = NULL;
429+
/* Canonicalize the provided program path now so it's correct even if
430+
the process changes cwd below. This prevents relative paths like
431+
"./tests/test2.pre" from resolving incorrectly after chdir. */
432+
#if defined(_MSC_VER)
433+
if (path) source_label = _fullpath(NULL, path, 0);
434+
#else
435+
if (path) source_label = realpath(path, NULL);
436+
#endif
437+
if (!source_label && path) source_label = strdup(path);
429438

430439
FILE* f = fopen(path, "rb");
431440
if (!f) {
@@ -498,6 +507,7 @@ int main(int argc, char** argv) {
498507
}
499508

500509
free(src);
510+
if (source_label) free(source_label);
501511
extensions_shutdown();
502512
builtins_reset_dynamic();
503513
return PREFIX_SUCCESS;

0 commit comments

Comments
 (0)