Skip to content

Server panics when tring to access a non-existing form file #1435

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

Closed
fvln opened this issue Oct 31, 2019 · 3 comments
Closed

Server panics when tring to access a non-existing form file #1435

fvln opened this issue Oct 31, 2019 · 3 comments
Labels

Comments

@fvln
Copy link

fvln commented Oct 31, 2019

Issue Description

Function FormFile tries to close a file handle, even if the file could not be opened. Bug introduced by #1411

Checklist

  • [ X ] Dependencies installed
  • [ X ] No typos
  • [ X ] Searched existing issues and docs

Expected behaviour

Function should return a non-nil error.

Actual behaviour

Panic

Steps to reproduce

package main

func main() {
	e := echo.New()
	e.POST("/uploads", uploadHandler)
}

func uploadHandler(context echo.Context) error {

	file, err := context.FormFile("file")
	if err != nil {
		log.Println("No file uploaded", err)
		return context.String(http.StatusBadRequest, "File is missing")
	}

// Do stuff with returned file

	return context.NoContent(http.StatusCreated)
}

Then run: curl -X POST "http://127.0.0.1:1323/api/v1/uploads" to trigger a panic

server | echo: http: panic serving 192.168.101.1:58638: runtime error: invalid memory address or nil pointer dereference
server | goroutine 54 [running]:
server | net/http.(*conn).serve.func1(0xc420d620a0)
server | 	/usr/lib/go-1.10/src/net/http/server.go:1726 +0xd0
server | panic(0x7c45e0, 0xc34ea0)
server | 	/usr/lib/go-1.10/src/runtime/panic.go:502 +0x229
server | github.com/labstack/echo.(*context).FormFile(0xc4206a0120, 0x83c1d5, 0x4, 0x0, 0x0, 0x0)
server | 	/home/user/go/src/github.com/labstack/echo/context.go:351 +0x62

Version/commit

754b449

Suggested fix

f, fh, err := c.request.FormFile(name)
if f != nil {
	defer f.Close()
}
@vishr
Copy link
Member

vishr commented Oct 31, 2019

@fvln Do you mind sending a PR?

@stale
Copy link

stale bot commented Dec 30, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@cperkkk
Copy link

cperkkk commented Feb 28, 2020

please re-open.

tcgriffith added a commit to tcgriffith/echo that referenced this issue Mar 3, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants