aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNick Mathewson <nickm@torproject.org>2016-06-20 10:46:53 -0400
committerNick Mathewson <nickm@torproject.org>2016-06-20 10:47:31 -0400
commitba28da8de55a4df8b958c06e5a589062171bb437 (patch)
tree0b3ceed7d877e790644012b5dad0131a23b0d36f
parent2b74e13a7cd965929dc2965b3c57a48a5b395f1a (diff)
downloadtor-ba28da8de55a4df8b958c06e5a589062171bb437.tar.gz
tor-ba28da8de55a4df8b958c06e5a589062171bb437.zip
compat.c coverage: simplify under-tested alloc_getcwd.
Yes, HURD lacks PATH_MAX. But we already limited the maximum buffer to 4096, so why not just use that?
-rw-r--r--src/common/compat.c33
1 files changed, 11 insertions, 22 deletions
diff --git a/src/common/compat.c b/src/common/compat.c
index 72dffe2a6e..24543cd89a 100644
--- a/src/common/compat.c
+++ b/src/common/compat.c
@@ -2341,28 +2341,15 @@ get_parent_directory(char *fname)
static char *
alloc_getcwd(void)
{
- int saved_errno = errno;
-/* We use this as a starting path length. Not too large seems sane. */
-#define START_PATH_LENGTH 128
-/* Nobody has a maxpath longer than this, as far as I know. And if they
- * do, they shouldn't. */
-#define MAX_SANE_PATH_LENGTH 4096
- size_t path_length = START_PATH_LENGTH;
- char *path = tor_malloc(path_length);
-
- errno = 0;
- while (getcwd(path, path_length) == NULL) {
- if (errno == ERANGE && path_length < MAX_SANE_PATH_LENGTH) {
- path_length*=2;
- path = tor_realloc(path, path_length);
- } else {
- tor_free(path);
- path = NULL;
- break;
- }
- }
- errno = saved_errno;
- return path;
+#ifdef PATH_MAX
+#define MAX_CWD PATH_MAX
+#else
+#define MAX_CWD 4096
+#endif
+
+ char path_buf[MAX_CWD];
+ char *path = getcwd(path_buf, sizeof(path_buf));
+ return path ? tor_strdup(path) : NULL;
}
#endif
@@ -2393,11 +2380,13 @@ make_path_absolute(char *fname)
tor_asprintf(&absfname, "%s/%s", path, fname);
tor_free(path);
} else {
+ /* LCOV_EXCL_START Can't make getcwd fail. */
/* If getcwd failed, the best we can do here is keep using the
* relative path. (Perhaps / isn't readable by this UID/GID.) */
log_warn(LD_GENERAL, "Unable to find current working directory: %s",
strerror(errno));
absfname = tor_strdup(fname);
+ /* LCOV_EXCL_STOP */
}
}
return absfname;