diff --git a/Sources/Testing/Support/Additions/CommandLineAdditions.swift b/Sources/Testing/Support/Additions/CommandLineAdditions.swift index 63decd1ad..895082e05 100644 --- a/Sources/Testing/Support/Additions/CommandLineAdditions.swift +++ b/Sources/Testing/Support/Additions/CommandLineAdditions.swift @@ -33,14 +33,27 @@ extension CommandLine { } return result! #elseif os(Linux) || os(Android) - return try withUnsafeTemporaryAllocation(of: CChar.self, capacity: Int(PATH_MAX) * 2) { buffer in - let readCount = readlink("/proc/self/exe", buffer.baseAddress!, buffer.count - 1) - guard readCount >= 0 else { - throw CError(rawValue: swt_errno()) + var result: String? +#if DEBUG + var bufferCount = Int(1) // force looping +#else + var bufferCount = Int(PATH_MAX) +#endif + while result == nil { + try withUnsafeTemporaryAllocation(of: CChar.self, capacity: bufferCount) { buffer in + let readCount = readlink("/proc/self/exe", buffer.baseAddress!, buffer.count) + guard readCount >= 0 else { + throw CError(rawValue: swt_errno()) + } + if readCount < buffer.count { + buffer[readCount] = 0 // NUL-terminate the string. + result = String(cString: buffer.baseAddress!) + } else { + bufferCount += Int(PATH_MAX) // add more space and try again + } } - buffer[readCount] = 0 // NUL-terminate the string. - return String(cString: buffer.baseAddress!) } + return result! #elseif os(FreeBSD) var mib = [CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1] return try mib.withUnsafeMutableBufferPointer { mib in diff --git a/Sources/_TestingInternals/ExecutablePath.cpp b/Sources/_TestingInternals/ExecutablePath.cpp index 82297b452..f1a9b6656 100644 --- a/Sources/_TestingInternals/ExecutablePath.cpp +++ b/Sources/_TestingInternals/ExecutablePath.cpp @@ -24,9 +24,8 @@ static constinit std::atomic earlyCWD { nullptr }; /// path (which can occur when manually invoking the test executable.) __attribute__((__constructor__(101), __used__)) static void captureEarlyCWD(void) { - static char buffer[PATH_MAX * 2]; - if (getcwd(buffer, sizeof(buffer))) { - earlyCWD.store(buffer); + if (auto cwd = getcwd(nil, 0)) { + earlyCWD.store(cwd); } } #endif