X-Git-Url: http://gitweb.michael.orlitzky.com/?p=apply-default-acl.git;a=blobdiff_plain;f=src%2Flibadacl.c;fp=src%2Flibadacl.c;h=62ab3cc0047dcb2e92dcf3d9b21d86b21fe518b8;hp=ce125db12dc77397574a98e11c683e12d7ed5911;hb=d8b55a1ea987e0ac8915bd5b2597d85f2d81c85d;hpb=4e8d531d9561674cfa5fc4853bafcd3a2b949911 diff --git a/src/libadacl.c b/src/libadacl.c index ce125db..62ab3cc 100644 --- a/src/libadacl.c +++ b/src/libadacl.c @@ -5,16 +5,17 @@ * */ -/* Enables get_current_dir_name() in unistd.h and the O_PATH flag. */ +/* Enables get_current_dir_name() in unistd.h, the O_PATH flag, and + * the asprintf() function. +*/ #define _GNU_SOURCE #include /* readdir(), etc. */ #include /* EINVAL, ELOOP, ENOTDIR, etc. */ #include /* openat() */ #include /* basename(), dirname() */ -#include /* PATH_MAX */ #include /* the "bool" type */ -#include /* perror(), snprintf() */ +#include /* perror(), asprintf() */ #include /* free() */ #include /* strdup() */ #include /* fstat() */ @@ -36,7 +37,7 @@ */ #define CLOSE_ERROR -1 #define OPEN_ERROR -1 -#define SNPRINTF_ERROR -1 +#define ASPRINTF_ERROR -1 #define STAT_ERROR -1 #define XATTR_ERROR -1 @@ -134,11 +135,11 @@ int safe_open(const char* pathname, int flags) { return OPEN_ERROR; } - char abspath[PATH_MAX]; - int snprintf_result = 0; + char* abspath = NULL; + int asprintf_result = 0; if (strchr(pathname, '/') == pathname) { /* pathname is already absolute; just copy it. */ - snprintf_result = snprintf(abspath, PATH_MAX, "%s", pathname); + asprintf_result = asprintf(&abspath, "%s", pathname); } else { /* Concatenate the current working directory and pathname into an @@ -163,14 +164,17 @@ int safe_open(const char* pathname, int flags) { free(cwd); return OPEN_ERROR; } - snprintf_result = snprintf(abspath, PATH_MAX, "%s/%s", abs_cwd, pathname); + asprintf_result = asprintf(&abspath, "%s/%s", abs_cwd, pathname); free(cwd); } - if (snprintf_result == SNPRINTF_ERROR || snprintf_result > PATH_MAX) { - perror("safe_open (snprintf)"); + if (asprintf_result == ASPRINTF_ERROR) { + perror("safe_open (asprintf)"); return OPEN_ERROR; } + /* Beyond here, asprintf() worked, and we need to free abspath. */ + int result = OPEN_ERROR; + bool abspath_is_root = (strcmp(abspath, "/") == 0); int rootflags = flags | O_DIRECTORY; if (!abspath_is_root) { @@ -180,18 +184,24 @@ int safe_open(const char* pathname, int flags) { int rootfd = open("/", rootflags); if (rootfd == OPEN_ERROR) { perror("safe_open (open)"); - return OPEN_ERROR; + result = OPEN_ERROR; + goto cleanup; } if (abspath_is_root) { - return rootfd; + result = rootfd; + goto cleanup; } - int result = safe_open_ex(rootfd, abspath+1, flags); + result = safe_open_ex(rootfd, abspath+1, flags); if (close(rootfd) == CLOSE_ERROR) { perror("safe_open (close)"); - return OPEN_ERROR; + result = OPEN_ERROR; + goto cleanup; } + + cleanup: + free(abspath); return result; }