diff options
author | startx2017 <vradu.startx@yandex.com> | 2018-10-17 19:23:21 -0400 |
---|---|---|
committer | startx2017 <vradu.startx@yandex.com> | 2018-10-17 19:23:21 -0400 |
commit | 9254a0e68b122f1117afe7832bee578f8bfa3d72 (patch) | |
tree | ecad8a468d7a39e41710ec9f73367fc7431bf5c7 /src | |
parent | mainline merge: fs_whitelist: no warning if macro resolution fails because of... (diff) | |
download | firejail-9254a0e68b122f1117afe7832bee578f8bfa3d72.tar.gz firejail-9254a0e68b122f1117afe7832bee578f8bfa3d72.tar.zst firejail-9254a0e68b122f1117afe7832bee578f8bfa3d72.zip |
mainline merge: improve clean_pathname function
Diffstat (limited to 'src')
-rw-r--r-- | src/firejail/util.c | 21 |
1 files changed, 12 insertions, 9 deletions
diff --git a/src/firejail/util.c b/src/firejail/util.c index c1a680dd4..485ddb3af 100644 --- a/src/firejail/util.c +++ b/src/firejail/util.c | |||
@@ -538,31 +538,34 @@ char *split_comma(char *str) { | |||
538 | char *clean_pathname(const char *path) { | 538 | char *clean_pathname(const char *path) { |
539 | assert(path); | 539 | assert(path); |
540 | size_t len = strlen(path); | 540 | size_t len = strlen(path); |
541 | char *rv = calloc(len + 1, 1); | 541 | assert(len + 1 != 0 && path[len] == '\0'); |
542 | |||
543 | char *rv = malloc(len + 1); | ||
542 | if (!rv) | 544 | if (!rv) |
543 | errExit("calloc"); | 545 | errExit("malloc"); |
546 | |||
544 | if (len > 0) { | 547 | if (len > 0) { |
545 | int i, j, cnt; | 548 | size_t i, j, cnt; |
546 | for (i = 0, j = 0, cnt = 0; i < len; i++) { | 549 | for (i = 0, j = 0, cnt = 0; i < len; i++) { |
547 | if (path[i] == '/') | 550 | if (path[i] == '/') |
548 | cnt++; | 551 | cnt++; |
549 | else | 552 | else |
550 | cnt = 0; | 553 | cnt = 0; |
554 | |||
551 | if (cnt < 2) { | 555 | if (cnt < 2) { |
552 | rv[j] = path[i]; | 556 | rv[j] = path[i]; |
553 | j++; | 557 | j++; |
554 | } | 558 | } |
555 | } | 559 | } |
560 | rv[j] = '\0'; | ||
561 | |||
556 | // remove a trailing slash | 562 | // remove a trailing slash |
557 | if (j > 1 && rv[j - 1] == '/') | 563 | if (j > 1 && rv[j - 1] == '/') |
558 | rv[j - 1] = '\0'; | 564 | rv[j - 1] = '\0'; |
559 | size_t new_len = strlen(rv); | ||
560 | if (new_len < len) { | ||
561 | rv = realloc(rv, new_len + 1); | ||
562 | if (!rv) | ||
563 | errExit("realloc"); | ||
564 | } | ||
565 | } | 565 | } |
566 | else | ||
567 | *rv = '\0'; | ||
568 | |||
566 | return rv; | 569 | return rv; |
567 | } | 570 | } |
568 | 571 | ||