Skip to content
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

binder: allow binding to a nil map #2574

Merged
merged 1 commit into from Feb 13, 2024
Merged

Conversation

georgmu
Copy link
Contributor

@georgmu georgmu commented Jan 12, 2024

Given the following code:

var params map[string]interface{} // <- this is nil
c.Bind(&params)

Currently, if the context does not define any path or query params, this might work when parsing a JSON body.
If there are path parameters involved, this will panic with:

echo: http: panic serving 127.0.0.1:41198: assignment to entry in nil map
goroutine 6 [running]:
net/http.(*conn).serve.func1()
	/usr/lib/golang/src/net/http/server.go:1868 +0x13d
panic({0x845340?, 0x8deaf0?})
	/usr/lib/golang/src/runtime/panic.go:920 +0x290
reflect.mapassign_faststr0(0x842580, 0x100c0000f8b18?, {0x8dc8c0?, 0x0?}, 0x842580?)
	/usr/lib/golang/src/runtime/map.go:1376 +0x25
reflect.mapassign_faststr(0x842580, 0x0, {0x8dc8c0, 0x1}, 0xc000017520)
	/usr/lib/golang/src/reflect/value.go:3837 +0x65
reflect.Value.SetMapIndex({0x842580, 0xc00006a088, 0x195}, {0x8327e0, 0xc000017510, 0x98}, {0x83d3a0, 0xc000017520, 0x94})
	/usr/lib/golang/src/reflect/value.go:2402 +0x2e5
github.com/labstack/echo/v4.(*DefaultBinder).bindData(0xad1780, {0x82ed80, 0xc00006a088}, 0xc0000f91b8, {0x89016d, 0x5})
	/home/georg/go/pkg/mod/github.com/labstack/echo/v4@v4.11.5-0.20231220133251-60fc2fb1b76f/bind.go:152 +0x150d
github.com/labstack/echo/v4.(*DefaultBinder).BindPathParams(0xad1780, {0x8e42b8, 0xc0000aaa00}, {0x82ed80, 0xc00006a088})
	/home/georg/go/pkg/mod/github.com/labstack/echo/v4@v4.11.5-0.20231220133251-60fc2fb1b76f/bind.go:40 +0x33d
github.com/labstack/echo/v4.(*DefaultBinder).Bind(0xad1780, {0x82ed80, 0xc00006a088}, {0x8e42b8, 0xc0000aaa00})
	/home/georg/go/pkg/mod/github.com/labstack/echo/v4@v4.11.5-0.20231220133251-60fc2fb1b76f/bind.go:111 +0x6d
github.com/labstack/echo/v4.(*context).Bind(0xc0000aaa00, {0x82ed80, 0xc00006a088})
	/home/georg/go/pkg/mod/github.com/labstack/echo/v4@v4.11.5-0.20231220133251-60fc2fb1b76f/context.go:439 +0x5d

With this patch applied, there are no panics anymore.

Copy link

codecov bot commented Feb 6, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (60fc2fb) 93.02% compared to head (b821001) 93.02%.
Report is 6 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #2574   +/-   ##
=======================================
  Coverage   93.02%   93.02%           
=======================================
  Files          39       39           
  Lines        4673     4676    +3     
=======================================
+ Hits         4347     4350    +3     
  Misses        236      236           
  Partials       90       90           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Contributor

@aldas aldas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this PR is overall OK but that check could be moved. If you have time please change that and if not, I can do it myself this weekend and merge it.

bind.go Outdated Show resolved Hide resolved
@algorithmcardboard
Copy link

I am running into this issue as well. Thank you @georgmu for the fix. @aldas any idea when this will make it to the release?

@aldas
Copy link
Contributor

aldas commented Feb 13, 2024

@algorithmcardboard , I really can not say at the moment, but you can use dest := map[string]int{} syntax to create your maps instead of var dest map[string]int

@aldas aldas merged commit ea529bb into labstack:master Feb 13, 2024
16 checks passed
@aldas
Copy link
Contributor

aldas commented Feb 13, 2024

@georgmu thanks for the fix and @algorithmcardboard for pinging me. I forgot this PR again.

nono added a commit to cozy/cozy-stack that referenced this pull request Apr 22, 2024
[![Mend
Renovate](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com)

This PR contains the following updates:

| Package | Change | Age | Adoption | Passing | Confidence |
|---|---|---|---|---|---|
| [github.com/labstack/echo/v4](https://togithub.com/labstack/echo) |
`v4.11.4` -> `v4.12.0` |
[![age](https://developer.mend.io/api/mc/badges/age/go/github.com%2flabstack%2fecho%2fv4/v4.12.0?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![adoption](https://developer.mend.io/api/mc/badges/adoption/go/github.com%2flabstack%2fecho%2fv4/v4.12.0?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![passing](https://developer.mend.io/api/mc/badges/compatibility/go/github.com%2flabstack%2fecho%2fv4/v4.11.4/v4.12.0?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![confidence](https://developer.mend.io/api/mc/badges/confidence/go/github.com%2flabstack%2fecho%2fv4/v4.11.4/v4.12.0?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|

---

### Release Notes

<details>
<summary>labstack/echo (github.com/labstack/echo/v4)</summary>

###
[`v4.12.0`](https://togithub.com/labstack/echo/blob/HEAD/CHANGELOG.md#v4120---2024-04-15)

[Compare
Source](https://togithub.com/labstack/echo/compare/v4.11.4...v4.12.0)

**Security**

- Update golang.org/x/net dep because of
[GO-2024-2687](https://pkg.go.dev/vuln/GO-2024-2687) by
[@&#8203;aldas](https://togithub.com/aldas) in
[labstack/echo#2625

**Enhancements**

- binder: make binding to Map work better with string destinations by
[@&#8203;aldas](https://togithub.com/aldas) in
[labstack/echo#2554
- README.md: add Encore as sponsor by
[@&#8203;marcuskohlberg](https://togithub.com/marcuskohlberg) in
[labstack/echo#2579
- Reorder paragraphs in README.md by
[@&#8203;aldas](https://togithub.com/aldas) in
[labstack/echo#2581
- CI: upgrade actions/checkout to v4 by
[@&#8203;aldas](https://togithub.com/aldas) in
[labstack/echo#2584
- Remove default charset from 'application/json' Content-Type header by
[@&#8203;doortts](https://togithub.com/doortts) in
[labstack/echo#2568
- CI: Use Go 1.22 by [@&#8203;aldas](https://togithub.com/aldas) in
[labstack/echo#2588
- binder: allow binding to a nil map by
[@&#8203;georgmu](https://togithub.com/georgmu) in
[labstack/echo#2574
- Add Skipper Unit Test In BasicBasicAuthConfig and Add More Detail
Explanation regarding BasicAuthValidator by
[@&#8203;RyoKusnadi](https://togithub.com/RyoKusnadi) in
[labstack/echo#2461
- fix some typos by
[@&#8203;teslaedison](https://togithub.com/teslaedison) in
[labstack/echo#2603
- fix: some typos by [@&#8203;pomadev](https://togithub.com/pomadev) in
[labstack/echo#2596
- Allow ResponseWriters to unwrap writers when flushing/hijacking by
[@&#8203;aldas](https://togithub.com/aldas) in
[labstack/echo#2595
- Add SPDX licence comments to files. by
[@&#8203;aldas](https://togithub.com/aldas) in
[labstack/echo#2604
- Upgrade deps by [@&#8203;aldas](https://togithub.com/aldas) in
[labstack/echo#2605
- Change type definition blocks to single declarations. This helps copy…
by [@&#8203;aldas](https://togithub.com/aldas) in
[labstack/echo#2606
- Fix Real IP logic by [@&#8203;cl-bvl](https://togithub.com/cl-bvl) in
[labstack/echo#2550
- Default binder can use `UnmarshalParams(params []string) error` inter…
by [@&#8203;aldas](https://togithub.com/aldas) in
[labstack/echo#2607
- Default binder can bind pointer to slice as struct field. For example
`*[]string` by [@&#8203;aldas](https://togithub.com/aldas) in
[labstack/echo#2608
- Remove maxparam dependence from Context by
[@&#8203;aldas](https://togithub.com/aldas) in
[labstack/echo#2611
- When route is registered with empty path it is normalized to `/`. by
[@&#8203;aldas](https://togithub.com/aldas) in
[labstack/echo#2616
- proxy middleware should use httputil.ReverseProxy for SSE requests by
[@&#8203;aldas](https://togithub.com/aldas) in
[labstack/echo#2624

</details>

---

### Configuration

📅 **Schedule**: Branch creation - "before 6am on Monday" in timezone
Europe/Paris, Automerge - At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you
are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the
rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update
again.

---

- [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check
this box

---

This PR has been generated by [Mend
Renovate](https://www.mend.io/free-developer-tools/renovate/). View
repository job log
[here](https://developer.mend.io/github/cozy/cozy-stack).

<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNy4zMTMuMSIsInVwZGF0ZWRJblZlciI6IjM3LjMxMy4xIiwidGFyZ2V0QnJhbmNoIjoibWFzdGVyIiwibGFiZWxzIjpbXX0=-->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants