When calling Render with a layout and a nil binding: panic: assignment to entry in nil map

Open
#134 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
48/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Stale
Tech stack
go
Domain
backend

Research direction

Start in jet/jet.go at jetVarMap and Render, especially the layout branch that calls bind.Set. Reproduce Render with a layout and a nil binding, then verify that the nil-binding path no longer panics while the existing layout behavior remains intact.

Written by the indexing model from the issue text.

Description

The issue is that in jetVarMap it assumes that a nil binding should result in a nil jet.VarMap.

https://github.com/gofiber/template/blob/c4b2c7ea24bfc975a0c0a438b15d3fb6a244db41/jet/jet.go#L233-L237

But if a layout is specified, it is then used to set a function and panics:

https://github.com/gofiber/template/blob/c4b2c7ea24bfc975a0c0a438b15d3fb6a244db41/jet/jet.go#L219-L229

This could be solved either by replacing var bind jet.VarMap with bind := make(jet.VarMap) or by checking if bind is nil (and makeing it if not) in Render right after if len(layout) > 0 {.

// var bind jet.VarMap
bind := make(jet.VarMap)

Or in Render

bind := jetVarMap(binding) 
 if len(layout) > 0 { 
        if bind == nil {
                bind = make(jet.VarMap)
        }
 	lay, err := e.Templates.GetTemplate(layout[0]) 
 	if err != nil { 
 		return err 
 	} 
 	bind.Set(e.layout, func() { 
 		_ = tmpl.Execute(out, bind, nil) 
 	}) 
 	return lay.Execute(out, bind, nil) 
 } 

I would send a pull request, but I'm not sure if it matters to you which way it is fixed. I would think it would be much clearer to put it in jetVarMap and get rid of the other calls to make there, but I guess there was a reason to not do that in the first place.


The work around is for the caller of Render to just specify an empty jet.VarMap or fiber.Map but that is not obvious and panicing from forgetting is not great.

Dominant language
Go
Stars
314
Forks
61
Avg merge
4h 35m
Merged PRs (30d)
12

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from gofiber/template

All issues in gofiber/template

Similar issues

More Go issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.