Skip to content

🐛 [Bug]: Next() and OverrideParam() dereference c.route without nil check #4354

@pageton

Description

@pageton

Bug Description

Next() and OverrideParam() access c.route.Handlers and c.route.Params without checking if c.route is nil. Other methods like Route() and IsMiddleware() properly guard against nil c.route.

How to Reproduce

Steps to reproduce the behavior:

  1. Call Next() from a custom error handler or middleware that runs before routing
  2. c.route is nil (router hasn't set it yet)
  3. c.route.Handlers panics with nil pointer dereference

Affected Code

ctx.go:249-259Next():

func (c *DefaultCtx) Next() error {
    c.indexHandler++
    if c.indexHandler < len(c.route.Handlers) {  // PANIC if c.route is nil

ctx.go:419-442OverrideParam():

func (c *DefaultCtx) OverrideParam(name, value string) {
    if !c.Matched() {    // checks c.isMatched, NOT c.route != nil
        return
    }
    for i, param := range c.route.Params {  // PANIC if c.route is nil

Contrast with correct patterns

Route() at line 365 and IsMiddleware() at line 387 properly check:

if c.route == nil {  // correct nil guard
    return &Route{...}
}

Expected Behavior

func (c *DefaultCtx) Next() error {
    c.indexHandler++
    if c.route != nil && c.indexHandler < len(c.route.Handlers) {
        // ...
    }
}

func (c *DefaultCtx) OverrideParam(name, value string) {
    if c.route == nil {
        return
    }
    // ...
}

Fiber Version

v3 (latest main branch)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    Status
    Done

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions