Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix exception when file type contains colon #817

Merged
merged 2 commits into from
Dec 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 15 additions & 9 deletions internal/driver/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -492,17 +492,23 @@ mapping:
func fetch(source string, duration, timeout time.Duration, ui plugin.UI, tr http.RoundTripper) (p *profile.Profile, src string, err error) {
var f io.ReadCloser

if sourceURL, timeout := adjustURL(source, duration, timeout); sourceURL != "" {
ui.Print("Fetching profile over HTTP from " + sourceURL)
if duration > 0 {
ui.Print(fmt.Sprintf("Please wait... (%v)", duration))
// First determine whether the source is a file, if not, it will be treated as a URL.
if _, openErr := os.Stat(source); openErr == nil {
if isPerfFile(source) {
f, err = convertPerfData(source, ui)
} else {
f, err = os.Open(source)
}
f, err = fetchURL(sourceURL, timeout, tr)
src = sourceURL
} else if isPerfFile(source) {
f, err = convertPerfData(source, ui)
} else {
f, err = os.Open(source)
sourceURL, timeout := adjustURL(source, duration, timeout)
if sourceURL != "" {
ui.Print("Fetching profile over HTTP from " + sourceURL)
if duration > 0 {
ui.Print(fmt.Sprintf("Please wait... (%v)", duration))
}
f, err = fetchURL(sourceURL, timeout, tr)
src = sourceURL
}
}
if err == nil {
defer f.Close()
Expand Down
22 changes: 19 additions & 3 deletions internal/driver/fetch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,12 +192,28 @@ func TestFetch(t *testing.T) {
type testcase struct {
source, execName string
}

for _, tc := range []testcase{
ts := []testcase{
{path + "go.crc32.cpu", ""},
{path + "go.nomappings.crash", "/bin/gotest.exe"},
{"http://localhost/profile?file=cppbench.cpu", ""},
} {
}
// Test that paths with a colon character are recognized as file paths
// if the file exists, rather than as a URL. We have to skip this test
// on Windows since the colon char is not allowed in Windows paths.
if runtime.GOOS != "windows" {
cuishuang marked this conversation as resolved.
Show resolved Hide resolved
cuishuang marked this conversation as resolved.
Show resolved Hide resolved
src := filepath.Join(path, "go.crc32.cpu")
dst := filepath.Join(t.TempDir(), "go.crc32.cpu_2023-11-11_01:02:03")
data, err := os.ReadFile(src)
if err != nil {
t.Fatalf("read src file %s failed: %#v", src, err)
}
err = os.WriteFile(dst, data, 0644)
if err != nil {
t.Fatalf("create dst file %s failed: %#v", dst, err)
}
ts = append(ts, testcase{dst, ""})
}
for _, tc := range ts {
p, _, _, err := grabProfile(&source{ExecName: tc.execName}, tc.source, nil, testObj{}, &proftest.TestUI{T: t}, &httpTransport{})
if err != nil {
t.Fatalf("%s: %s", tc.source, err)
Expand Down
Loading