diff --git a/reftable/stack_test.c b/reftable/stack_test.c
index cf2e32a25c..c9beaf4dbf 100644
--- a/reftable/stack_test.c
+++ b/reftable/stack_test.c
@@ -28,9 +28,19 @@ static void clear_dir(const char *dirname)
 	strbuf_release(&path);
 }
 
+static char *get_tmp_template(const char *prefix)
+{
+	static struct strbuf path = STRBUF_INIT;
+	const char *tmp = getenv("TMPDIR");
+
+	strbuf_reset(&path);
+	strbuf_addf(&path, "%s/%s.XXXXXX", tmp ? tmp : "/tmp", prefix);
+	return path.buf;
+}
+
 static void test_read_file(void)
 {
-	char fn[256] = "/tmp/stack.test_read_file.XXXXXX";
+	char *fn = get_tmp_template("stack.test_read_file");
 	int fd = mkstemp(fn);
 	char out[1024] = "line1\n\nline2\nline3";
 	int n, err;
@@ -99,7 +109,7 @@ static int write_test_log(struct reftable_writer *wr, void *arg)
 
 static void test_reftable_stack_add_one(void)
 {
-	char dir[256] = "/tmp/stack_test.XXXXXX";
+	char *dir = get_tmp_template("stack_test");
 	struct reftable_write_options cfg = { 0 };
 	struct reftable_stack *st = NULL;
 	int err;
@@ -132,7 +142,7 @@ static void test_reftable_stack_uptodate(void)
 	struct reftable_write_options cfg = { 0 };
 	struct reftable_stack *st1 = NULL;
 	struct reftable_stack *st2 = NULL;
-	char dir[256] = "/tmp/stack_test.XXXXXX";
+	char *dir = get_tmp_template("stack_test");
 	int err;
 	struct reftable_ref_record ref1 = {
 		.refname = "HEAD",
@@ -171,7 +181,7 @@ static void test_reftable_stack_uptodate(void)
 
 static void test_reftable_stack_transaction_api(void)
 {
-	char dir[256] = "/tmp/stack_test.XXXXXX";
+	char *dir = get_tmp_template("stack_test");
 	struct reftable_write_options cfg = { 0 };
 	struct reftable_stack *st = NULL;
 	int err;
@@ -216,7 +226,7 @@ static void test_reftable_stack_validate_refname(void)
 	struct reftable_write_options cfg = { 0 };
 	struct reftable_stack *st = NULL;
 	int err;
-	char dir[256] = "/tmp/stack_test.XXXXXX";
+	char *dir = get_tmp_template("stack_test");
 	int i;
 	struct reftable_ref_record ref = {
 		.refname = "a/b",
@@ -254,7 +264,7 @@ static int write_error(struct reftable_writer *wr, void *arg)
 
 static void test_reftable_stack_update_index_check(void)
 {
-	char dir[256] = "/tmp/stack_test.XXXXXX";
+	char *dir = get_tmp_template("stack_test");
 	struct reftable_write_options cfg = { 0 };
 	struct reftable_stack *st = NULL;
 	int err;
@@ -284,7 +294,7 @@ static void test_reftable_stack_update_index_check(void)
 
 static void test_reftable_stack_lock_failure(void)
 {
-	char dir[256] = "/tmp/stack_test.XXXXXX";
+	char *dir = get_tmp_template("stack_test");
 	struct reftable_write_options cfg = { 0 };
 	struct reftable_stack *st = NULL;
 	int err, i;
@@ -309,7 +319,7 @@ static void test_reftable_stack_add(void)
 		.exact_log_message = 1,
 	};
 	struct reftable_stack *st = NULL;
-	char dir[256] = "/tmp/stack_test.XXXXXX";
+	char *dir = get_tmp_template("stack_test");
 	struct reftable_ref_record refs[2] = { { NULL } };
 	struct reftable_log_record logs[2] = { { NULL } };
 	int N = ARRAY_SIZE(refs);
@@ -385,7 +395,7 @@ static void test_reftable_stack_log_normalize(void)
 		0,
 	};
 	struct reftable_stack *st = NULL;
-	char dir[256] = "/tmp/stack_test.XXXXXX";
+	char *dir = get_tmp_template("stack_test");
 
 	uint8_t h1[SHA1_SIZE] = { 0x01 }, h2[SHA1_SIZE] = { 0x02 };
 
@@ -436,7 +446,7 @@ static void test_reftable_stack_log_normalize(void)
 static void test_reftable_stack_tombstone(void)
 {
 	int i = 0;
-	char dir[256] = "/tmp/stack_test.XXXXXX";
+	char *dir = get_tmp_template("stack_test");
 	struct reftable_write_options cfg = { 0 };
 	struct reftable_stack *st = NULL;
 	int err;
@@ -511,7 +521,7 @@ static void test_reftable_stack_tombstone(void)
 
 static void test_reftable_stack_hash_id(void)
 {
-	char dir[256] = "/tmp/stack_test.XXXXXX";
+	char *dir = get_tmp_template("stack_test");
 	struct reftable_write_options cfg = { 0 };
 	struct reftable_stack *st = NULL;
 	int err;
@@ -621,7 +631,7 @@ static void test_suggest_compaction_segment_nothing(void)
 
 static void test_reflog_expire(void)
 {
-	char dir[256] = "/tmp/stack.test_reflog_expire.XXXXXX";
+	char *dir = get_tmp_template("stack.test_reflog_expire");
 	struct reftable_write_options cfg = { 0 };
 	struct reftable_stack *st = NULL;
 	struct reftable_log_record logs[20] = { { NULL } };
@@ -701,7 +711,7 @@ static void test_empty_add(void)
 	struct reftable_write_options cfg = { 0 };
 	struct reftable_stack *st = NULL;
 	int err;
-	char dir[256] = "/tmp/stack_test.XXXXXX";
+	char *dir = get_tmp_template("stack_test");
 	struct reftable_stack *st2 = NULL;
 
 	EXPECT(mkdtemp(dir));
@@ -723,7 +733,7 @@ static void test_reftable_stack_auto_compaction(void)
 {
 	struct reftable_write_options cfg = { 0 };
 	struct reftable_stack *st = NULL;
-	char dir[256] = "/tmp/stack_test.XXXXXX";
+	char *dir = get_tmp_template("stack_test");
 	int err, i;
 	int N = 100;
 	EXPECT(mkdtemp(dir));
