From ee4b602d26225bb5a548eaeb7bd570adaa48721a Mon Sep 17 00:00:00 2001 From: Michael Orlitzky Date: Thu, 1 Mar 2018 20:57:50 -0500 Subject: [PATCH] src/libadacl.c: minor code simplification in safe_open(). --- src/libadacl.c | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/src/libadacl.c b/src/libadacl.c index 1b31938..36812e8 100644 --- a/src/libadacl.c +++ b/src/libadacl.c @@ -170,25 +170,24 @@ int safe_open(const char* pathname, int flags) { return OPEN_ERROR; } - int fd = 0; - if (strcmp(abspath, "/") == 0) { - fd = open("/", flags | O_DIRECTORY); - } - else { + bool abspath_is_root = (strcmp(abspath, "/") == 0); + int rootflags = flags | O_DIRECTORY; + if (!abspath_is_root) { /* Use O_PATH for some added safety if "/" is not our target */ - fd = open("/", flags | O_DIRECTORY | O_PATH); + rootflags |= O_PATH; } - if (fd == OPEN_ERROR) { + int rootfd = open("/", rootflags); + if (rootfd == OPEN_ERROR) { perror("safe_open (open)"); return OPEN_ERROR; } - if (strcmp(abspath, "/") == 0) { - return fd; + if (abspath_is_root) { + return rootfd; } - int result = safe_open_ex(fd, abspath+1, flags); - if (close(fd) == CLOSE_ERROR) { + int result = safe_open_ex(rootfd, abspath+1, flags); + if (close(rootfd) == CLOSE_ERROR) { perror("safe_open (close)"); return OPEN_ERROR; } -- 2.44.2