diff options
author | smitsohu <smitsohu@gmail.com> | 2021-05-03 00:52:08 +0200 |
---|---|---|
committer | smitsohu <smitsohu@gmail.com> | 2021-05-03 00:52:08 +0200 |
commit | 289d648004c78b19cd953b36db69df6958dfb0aa (patch) | |
tree | af254776e0d2d23e88c0835eee9634ebc70d923b /src/firejail/util.c | |
parent | Update Librewolf profile and Add Sway profile (#4164) (diff) | |
download | firejail-289d648004c78b19cd953b36db69df6958dfb0aa.tar.gz firejail-289d648004c78b19cd953b36db69df6958dfb0aa.tar.zst firejail-289d648004c78b19cd953b36db69df6958dfb0aa.zip |
enhance clean_pathname function
Diffstat (limited to 'src/firejail/util.c')
-rw-r--r-- | src/firejail/util.c | 24 |
1 files changed, 17 insertions, 7 deletions
diff --git a/src/firejail/util.c b/src/firejail/util.c index 2ad85acd6..8966521cb 100644 --- a/src/firejail/util.c +++ b/src/firejail/util.c | |||
@@ -544,11 +544,13 @@ char *split_comma(char *str) { | |||
544 | } | 544 | } |
545 | 545 | ||
546 | 546 | ||
547 | // remove consecutive and trailing slashes | 547 | // simplify absolute path by removing |
548 | // and return allocated memory | 548 | // 1) consecutive and trailing slashes, and |
549 | // e.g. /home//user/ -> /home/user | 549 | // 2) segments with a single dot |
550 | // for example /foo//./bar/ -> /foo/bar | ||
550 | char *clean_pathname(const char *path) { | 551 | char *clean_pathname(const char *path) { |
551 | assert(path); | 552 | assert(path && path[0] == '/'); |
553 | |||
552 | size_t len = strlen(path); | 554 | size_t len = strlen(path); |
553 | char *rv = malloc(len + 1); | 555 | char *rv = malloc(len + 1); |
554 | if (!rv) | 556 | if (!rv) |
@@ -557,15 +559,23 @@ char *clean_pathname(const char *path) { | |||
557 | size_t i = 0; | 559 | size_t i = 0; |
558 | size_t j = 0; | 560 | size_t j = 0; |
559 | while (path[i]) { | 561 | while (path[i]) { |
560 | while (path[i] == '/' && path[i+1] == '/') | 562 | if (path[i] == '/') { |
561 | i++; | 563 | while (path[i+1] == '/' || |
564 | (path[i+1] == '.' && path[i+2] == '/')) | ||
565 | i++; | ||
566 | } | ||
567 | |||
562 | rv[j++] = path[i++]; | 568 | rv[j++] = path[i++]; |
563 | } | 569 | } |
564 | rv[j] = '\0'; | 570 | rv[j] = '\0'; |
565 | 571 | ||
572 | // remove a trailing dot | ||
573 | if (j > 1 && rv[j - 1] == '.' && rv[j - 2] == '/') | ||
574 | rv[--j] = '\0'; | ||
575 | |||
566 | // remove a trailing slash | 576 | // remove a trailing slash |
567 | if (j > 1 && rv[j - 1] == '/') | 577 | if (j > 1 && rv[j - 1] == '/') |
568 | rv[j - 1] = '\0'; | 578 | rv[--j] = '\0'; |
569 | 579 | ||
570 | return rv; | 580 | return rv; |
571 | } | 581 | } |