Skip to content

net/http: Server panic if listener returns connection with RemoteAddr() == nil #23022

Closed
@azavorotnii

Description

@azavorotnii

What version of Go are you using (go version)?

1.9.2

Does this issue reproduce with the latest release?

Yes.

What operating system and processor architecture are you using (go env)?

Any.

What did you do?

In very specific situation listener returns net.Conn object with "RemoteAddr" that is "nil".

	listener, err := net.ListenTCP("tcp", &net.TCPAddr{Port: 8080})
	if err != nil {
		log.Fatal(err)
	}
	
	server := &http.Server{}
	for {
		if err := server.Serve(listener); err != nil {
			log.Println(err)
		}
	}

What did you expect to see?

Server returns error from server.Serve() or ignore issue.

What did you see instead?

Panic:

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x18 pc=0x2fcaf4]

goroutine 207644 [running]:
net/http.(*conn).serve(0x15ee4120, 0x31fbbc0, 0x154af2c0)
        /usr/local/go/src/net/http/server.go:1691 +0x34
created by net/http.(*Server).Serve
        /usr/local/go/src/net/http/server.go:2720 +0x208

Related code lines:

func (c *conn) serve(ctx context.Context) {
	c.remoteAddr = c.rwc.RemoteAddr().String()
	...

Field "c.remoteAddr" is used only in couple places, so should not be harmful to do instead:

func (c *conn) serve(ctx context.Context) {
	if raddr := c.rwc.RemoteAddr(); raddr != nil {
		c.remoteAddr = raddr.String()
	}
	...

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeWaitingForInfoIssue is not actionable because of missing required information, which needs to be provided.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions