瀏覽代碼

Avoid using $HOME when it's empty. (#169)

Without the check, pprof may try using a path like "/pprof" when no
home environment variable is defined.

Fixes #148.
Alexey Alexandrov 8 年之前
父節點
當前提交
4ab1a368c5
共有 2 個檔案被更改,包括 14 行新增4 行删除
  1. 9
    3
      internal/driver/fetch.go
  2. 5
    1
      internal/proftest/proftest.go

+ 9
- 3
internal/driver/fetch.go 查看文件

@@ -277,12 +277,18 @@ func homeEnv() string {
277 277
 }
278 278
 
279 279
 // setTmpDir prepares the directory to use to save profiles retrieved
280
-// remotely. It is selected from PPROF_TMPDIR, defaults to $HOME/pprof.
280
+// remotely. It is selected from PPROF_TMPDIR, defaults to $HOME/pprof, and, if
281
+// $HOME is not set, falls back to os.TempDir().
281 282
 func setTmpDir(ui plugin.UI) (string, error) {
283
+	var dirs []string
282 284
 	if profileDir := os.Getenv("PPROF_TMPDIR"); profileDir != "" {
283
-		return profileDir, nil
285
+		dirs = append(dirs, profileDir)
284 286
 	}
285
-	for _, tmpDir := range []string{os.Getenv(homeEnv()) + "/pprof", os.TempDir()} {
287
+	if homeDir := os.Getenv(homeEnv()); homeDir != "" {
288
+		dirs = append(dirs, filepath.Join(homeDir, "pprof"))
289
+	}
290
+	dirs = append(dirs, os.TempDir())
291
+	for _, tmpDir := range dirs {
286 292
 		if err := os.MkdirAll(tmpDir, 0755); err != nil {
287 293
 			ui.PrintErr("Could not use temp dir ", tmpDir, ": ", err.Error())
288 294
 			continue

+ 5
- 1
internal/proftest/proftest.go 查看文件

@@ -103,7 +103,11 @@ func (ui *TestUI) PrintErr(args ...interface{}) {
103 103
 		ui.Ignore--
104 104
 		return
105 105
 	}
106
-	ui.T.Error(args)
106
+	// Stringify arguments with fmt.Sprint() to match what default UI
107
+	// implementation does. Without this Error() calls fmt.Sprintln() which
108
+	// _always_ adds spaces between arguments, unlike fmt.Sprint() which only
109
+	// adds them between arguments if neither is string.
110
+	ui.T.Error(fmt.Sprint(args...))
107 111
 }
108 112
 
109 113
 // IsTerminal indicates if the UI is an interactive terminal.