From 831b724317b7499c7f3d3b287d73099be10e1e15 Mon Sep 17 00:00:00 2001 From: Kyle Jessup Date: Mon, 28 Nov 2016 11:16:23 -0500 Subject: [PATCH] Better safeguards in File.resolveTilde Former-commit-id: 2552d79e8bced1d4a9817291f5e2fc61690597d8 --- Sources/PerfectLib/File.swift | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/Sources/PerfectLib/File.swift b/Sources/PerfectLib/File.swift index 79b0aa19..926e010f 100644 --- a/Sources/PerfectLib/File.swift +++ b/Sources/PerfectLib/File.swift @@ -115,8 +115,14 @@ public class File { static func resolveTilde(inPath: String) -> String { if !inPath.isEmpty && inPath[inPath.startIndex] == "~" { var wexp = wordexp_t() - wordexp(inPath, &wexp, 0) - if let resolved = wexp.we_wordv[0], let pth = String(validatingUTF8: resolved) { + guard 0 == wordexp(inPath, &wexp, 0), + let we_wordv = wexp.we_wordv else { + return inPath + } + defer { + wordfree(&wexp) + } + if let resolved = we_wordv[0], let pth = String(validatingUTF8: resolved) { return pth } }