Browse Source

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 years ago
parent
commit
4ab1a368c5
2 changed files with 14 additions and 4 deletions
  1. 9
    3
      internal/driver/fetch.go
  2. 5
    1
      internal/proftest/proftest.go

+ 9
- 3
internal/driver/fetch.go View File

277
 }
277
 }
278
 
278
 
279
 // setTmpDir prepares the directory to use to save profiles retrieved
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
 func setTmpDir(ui plugin.UI) (string, error) {
282
 func setTmpDir(ui plugin.UI) (string, error) {
283
+	var dirs []string
282
 	if profileDir := os.Getenv("PPROF_TMPDIR"); profileDir != "" {
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
 		if err := os.MkdirAll(tmpDir, 0755); err != nil {
292
 		if err := os.MkdirAll(tmpDir, 0755); err != nil {
287
 			ui.PrintErr("Could not use temp dir ", tmpDir, ": ", err.Error())
293
 			ui.PrintErr("Could not use temp dir ", tmpDir, ": ", err.Error())
288
 			continue
294
 			continue

+ 5
- 1
internal/proftest/proftest.go View File

103
 		ui.Ignore--
103
 		ui.Ignore--
104
 		return
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
 // IsTerminal indicates if the UI is an interactive terminal.
113
 // IsTerminal indicates if the UI is an interactive terminal.