aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorLibravatar smitsohu <smitsohu@gmail.com>2018-10-17 18:18:09 +0200
committerLibravatar smitsohu <smitsohu@gmail.com>2018-10-17 18:18:09 +0200
commit520c94af9a0482c573733bcd5c3c8826fc63430d (patch)
treedb7b3aaf3d3d309a66088f547b03f7e164f125ba /src
parentfs_whitelist: cache length of home directory string (diff)
downloadfirejail-520c94af9a0482c573733bcd5c3c8826fc63430d.tar.gz
firejail-520c94af9a0482c573733bcd5c3c8826fc63430d.tar.zst
firejail-520c94af9a0482c573733bcd5c3c8826fc63430d.zip
improve clean_pathname function
Diffstat (limited to 'src')
-rw-r--r--src/firejail/util.c17
1 files changed, 12 insertions, 5 deletions
diff --git a/src/firejail/util.c b/src/firejail/util.c
index 61330a87e..866ef4653 100644
--- a/src/firejail/util.c
+++ b/src/firejail/util.c
@@ -471,11 +471,13 @@ void trim_trailing_slash_or_dot(char *path) {
471char *line_remove_spaces(const char *buf) { 471char *line_remove_spaces(const char *buf) {
472 EUID_ASSERT(); 472 EUID_ASSERT();
473 assert(buf); 473 assert(buf);
474 if (strlen(buf) == 0) 474 size_t len = strlen(buf);
475 if (len == 0)
475 return NULL; 476 return NULL;
477 assert(len + 1 != 0 && buf[len] == '\0');
476 478
477 // allocate memory for the new string 479 // allocate memory for the new string
478 char *rv = malloc(strlen(buf) + 1); 480 char *rv = malloc(len + 1);
479 if (rv == NULL) 481 if (rv == NULL)
480 errExit("malloc"); 482 errExit("malloc");
481 483
@@ -539,12 +541,14 @@ char *split_comma(char *str) {
539char *clean_pathname(const char *path) { 541char *clean_pathname(const char *path) {
540 assert(path); 542 assert(path);
541 size_t len = strlen(path); 543 size_t len = strlen(path);
542 char *rv = calloc(len + 1, 1); 544 assert(len + 1 != 0 && path[len] == '\0');
545
546 char *rv = malloc(len + 1);
543 if (!rv) 547 if (!rv)
544 errExit("calloc"); 548 errExit("malloc");
545 549
546 if (len > 0) { 550 if (len > 0) {
547 int i, j, cnt; 551 size_t i, j, cnt;
548 for (i = 0, j = 0, cnt = 0; i < len; i++) { 552 for (i = 0, j = 0, cnt = 0; i < len; i++) {
549 if (path[i] == '/') 553 if (path[i] == '/')
550 cnt++; 554 cnt++;
@@ -556,11 +560,14 @@ char *clean_pathname(const char *path) {
556 j++; 560 j++;
557 } 561 }
558 } 562 }
563 rv[j] = '\0';
559 564
560 // remove a trailing slash 565 // remove a trailing slash
561 if (j > 1 && rv[j - 1] == '/') 566 if (j > 1 && rv[j - 1] == '/')
562 rv[j - 1] = '\0'; 567 rv[j - 1] = '\0';
563 } 568 }
569 else
570 *rv = '\0';
564 571
565 return rv; 572 return rv;
566} 573}