Skip to content

Middleware Chains - Nil pointer dereference #83

@inzanez

Description

@inzanez

Hi

I was playing around building different middleware chains on the fly. I was using a slice:
middleware vestigo.Middleware[]
for that purpose. This sometimes resulted in a crash with nil pointer dereference / invalid memory access. I traced it back to a function in 'router.go' in the vestigo package:

func buildChain(f http.HandlerFunc, m ...Middleware) http.HandlerFunc {
    // if our chain is done, use the original handlerfunc
    if len(m) == 0 {
        return f
    }
    // otherwise nest the handlerfuncs
    return m[0](buildChain(f, m[1:cap(m)]...))
}

I replaced the line
return m[0](buildChain(f, m[1:cap(m)]...))
with
return m[0](buildChain(f, m[1:len(m)]...))

on my local sources, now everything is fine. I was wondering why you were using cap here at any rate? Wouldn't using 'len' be safer?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions