Skip to content

Commit 8785d3c

Browse files
author
Dean Karn
authored
Handle invalid Slice/Array indexes better (#61)
1 parent ff1560b commit 8785d3c

File tree

6 files changed

+78
-70
lines changed

6 files changed

+78
-70
lines changed

.github/workflows/workflow.yml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,20 @@ jobs:
88
test:
99
strategy:
1010
matrix:
11-
go-version: [1.15.x, 1.16.x]
11+
go-version: [1.17.x, 1.20.x]
1212
os: [ubuntu-latest, macos-latest, windows-latest]
1313
runs-on: ${{ matrix.os }}
1414
steps:
1515
- name: Install Go
16-
uses: actions/setup-go@v2
16+
uses: actions/setup-go@v3
1717
with:
1818
go-version: ${{ matrix.go-version }}
1919

2020
- name: Checkout code
21-
uses: actions/checkout@v2
21+
uses: actions/checkout@v3
2222

2323
- name: Restore Cache
24-
uses: actions/cache@v2
24+
uses: actions/cache@v3
2525
with:
2626
path: ~/go/pkg/mod
2727
key: ${{ runner.os }}-v1-go-${{ hashFiles('**/go.sum') }}
@@ -32,7 +32,7 @@ jobs:
3232
run: go test -race -covermode=atomic -coverprofile="profile.cov" ./...
3333

3434
- name: Send Coverage
35-
if: matrix.os == 'ubuntu-latest' && matrix.go-version == '1.16.x'
35+
if: matrix.os == 'ubuntu-latest' && matrix.go-version == '1.20.x'
3636
uses: shogo82148/actions-goveralls@v1
3737
with:
3838
path-to-profile: profile.cov
@@ -41,8 +41,8 @@ jobs:
4141
name: lint
4242
runs-on: ubuntu-latest
4343
steps:
44-
- uses: actions/checkout@v2
44+
- uses: actions/checkout@v3
4545
- name: golangci-lint
46-
uses: golangci/golangci-lint-action@v2
46+
uses: golangci/golangci-lint-action@v3
4747
with:
48-
version: v1.37.1
48+
version: latest

.travis.yml

Lines changed: 0 additions & 26 deletions
This file was deleted.

Makefile

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
all: lint test bench
2+
3+
lint:
4+
golangci-lint run --timeout 5m
5+
6+
test:
7+
go test -covermode=atomic -race ./...
8+
9+
bench:
10+
go test -bench=. -benchmem ./...
11+
12+
.PHONY: test lint bench
13+
.DEFAULT_GOAL := all

README.md

Lines changed: 25 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package form
22
============
3-
<img align="right" src="https://raw.githubusercontent.com/go-playground/form/master/logo.jpg">![Project status](https://img.shields.io/badge/version-4.2.0-green.svg)
3+
<img align="right" src="https://raw.githubusercontent.com/go-playground/form/master/logo.jpg">![Project status](https://img.shields.io/badge/version-4.2.1-green.svg)
44
[![Build Status](https://github.com/go-playground/form/actions/workflows/workflow.yml/badge.svg)](https://github.com/go-playground/form/actions/workflows/workflow.yml)
55
[![Coverage Status](https://coveralls.io/repos/github/go-playground/form/badge.svg?branch=master)](https://coveralls.io/github/go-playground/form?branch=master)
66
[![Go Report Card](https://goreportcard.com/badge/github.com/go-playground/form)](https://goreportcard.com/report/github.com/go-playground/form)
@@ -277,36 +277,35 @@ Field []*string{nil, nil, &i}
277277

278278
Benchmarks
279279
------
280-
###### Run on MacBook Pro (15-inch, 2017) using go version go1.10.1 darwin/amd64
280+
###### Run on M1 MacBook Pro using go version go1.20.6 darwin/amd64
281281

282282
NOTE: the 1 allocation and B/op in the first 4 decodes is actually the struct allocating when passing it in, so primitives are actually zero allocation.
283283

284284
```go
285-
go test -run=NONE -bench=. -benchmem=true
285+
go test -run=NONE -bench=. -benchmem=true ./...
286286
goos: darwin
287-
goarch: amd64
288-
pkg: github.com/go-playground/form/benchmarks
289-
290-
BenchmarkSimpleUserDecodeStruct-8 5000000 236 ns/op 64 B/op 1 allocs/op
291-
BenchmarkSimpleUserDecodeStructParallel-8 20000000 82.1 ns/op 64 B/op 1 allocs/op
292-
BenchmarkSimpleUserEncodeStruct-8 2000000 627 ns/op 485 B/op 10 allocs/op
293-
BenchmarkSimpleUserEncodeStructParallel-8 10000000 223 ns/op 485 B/op 10 allocs/op
294-
BenchmarkPrimitivesDecodeStructAllPrimitivesTypes-8 2000000 724 ns/op 96 B/op 1 allocs/op
295-
BenchmarkPrimitivesDecodeStructAllPrimitivesTypesParallel-8 10000000 246 ns/op 96 B/op 1 allocs/op
296-
BenchmarkPrimitivesEncodeStructAllPrimitivesTypes-8 500000 3187 ns/op 2977 B/op 36 allocs/op
297-
BenchmarkPrimitivesEncodeStructAllPrimitivesTypesParallel-8 1000000 1106 ns/op 2977 B/op 36 allocs/op
298-
BenchmarkComplexArrayDecodeStructAllTypes-8 100000 13748 ns/op 2248 B/op 121 allocs/op
299-
BenchmarkComplexArrayDecodeStructAllTypesParallel-8 500000 4313 ns/op 2249 B/op 121 allocs/op
300-
BenchmarkComplexArrayEncodeStructAllTypes-8 200000 10758 ns/op 7113 B/op 104 allocs/op
301-
BenchmarkComplexArrayEncodeStructAllTypesParallel-8 500000 3532 ns/op 7113 B/op 104 allocs/op
302-
BenchmarkComplexMapDecodeStructAllTypes-8 100000 17644 ns/op 5305 B/op 130 allocs/op
303-
BenchmarkComplexMapDecodeStructAllTypesParallel-8 300000 5470 ns/op 5308 B/op 130 allocs/op
304-
BenchmarkComplexMapEncodeStructAllTypes-8 200000 11155 ns/op 6971 B/op 129 allocs/op
305-
BenchmarkComplexMapEncodeStructAllTypesParallel-8 500000 3768 ns/op 6971 B/op 129 allocs/op
306-
BenchmarkDecodeNestedStruct-8 500000 2462 ns/op 384 B/op 14 allocs/op
307-
BenchmarkDecodeNestedStructParallel-8 2000000 814 ns/op 384 B/op 14 allocs/op
308-
BenchmarkEncodeNestedStruct-8 1000000 1483 ns/op 693 B/op 16 allocs/op
309-
BenchmarkEncodeNestedStructParallel-8 3000000 525 ns/op 693 B/op 16 allocs/op
287+
goarch: arm64
288+
pkg: github.com/go-playground/form/v4/benchmarks
289+
BenchmarkSimpleUserDecodeStruct-8 8704111 121.1 ns/op 64 B/op 1 allocs/op
290+
BenchmarkSimpleUserDecodeStructParallel-8 35916134 32.89 ns/op 64 B/op 1 allocs/op
291+
BenchmarkSimpleUserEncodeStruct-8 3746173 320.7 ns/op 485 B/op 10 allocs/op
292+
BenchmarkSimpleUserEncodeStructParallel-8 7293147 180.0 ns/op 485 B/op 10 allocs/op
293+
BenchmarkPrimitivesDecodeStructAllPrimitivesTypes-8 2993259 400.5 ns/op 96 B/op 1 allocs/op
294+
BenchmarkPrimitivesDecodeStructAllPrimitivesTypesParallel-8 13023300 97.70 ns/op 96 B/op 1 allocs/op
295+
BenchmarkPrimitivesEncodeStructAllPrimitivesTypes-8 643202 1767 ns/op 2977 B/op 35 allocs/op
296+
BenchmarkPrimitivesEncodeStructAllPrimitivesTypesParallel-8 1000000 1202 ns/op 2978 B/op 35 allocs/op
297+
BenchmarkComplexArrayDecodeStructAllTypes-8 172630 6822 ns/op 2008 B/op 121 allocs/op
298+
BenchmarkComplexArrayDecodeStructAllTypesParallel-8 719788 1735 ns/op 2009 B/op 121 allocs/op
299+
BenchmarkComplexArrayEncodeStructAllTypes-8 197052 5839 ns/op 7087 B/op 104 allocs/op
300+
BenchmarkComplexArrayEncodeStructAllTypesParallel-8 348039 3247 ns/op 7089 B/op 104 allocs/op
301+
BenchmarkComplexMapDecodeStructAllTypes-8 139246 8550 ns/op 5313 B/op 130 allocs/op
302+
BenchmarkComplexMapDecodeStructAllTypesParallel-8 409018 3143 ns/op 5317 B/op 130 allocs/op
303+
BenchmarkComplexMapEncodeStructAllTypes-8 208833 5515 ns/op 4257 B/op 103 allocs/op
304+
BenchmarkComplexMapEncodeStructAllTypesParallel-8 523833 2182 ns/op 4258 B/op 103 allocs/op
305+
BenchmarkDecodeNestedStruct-8 807690 1408 ns/op 344 B/op 14 allocs/op
306+
BenchmarkDecodeNestedStructParallel-8 3409441 359.6 ns/op 344 B/op 14 allocs/op
307+
BenchmarkEncodeNestedStruct-8 1488520 803.6 ns/op 653 B/op 16 allocs/op
308+
BenchmarkEncodeNestedStructParallel-8 3570204 346.6 ns/op 653 B/op 16 allocs/op
310309
```
311310

312311
Competitor benchmarks can be found [here](https://github.com/go-playground/form/blob/master/benchmarks/benchmarks.md)
@@ -319,15 +318,6 @@ Here is a list of software that compliments using this library post decoding.
319318
* [Validator](https://github.com/go-playground/validator) - Go Struct and Field validation, including Cross Field, Cross Struct, Map, Slice and Array diving.
320319
* [mold](https://github.com/go-playground/mold) - Is a general library to help modify or set data within data structures and other objects.
321320

322-
Package Versioning
323-
----------
324-
I'm jumping on the vendoring bandwagon, you should vendor this package as I will not
325-
be creating different version with gopkg.in like allot of my other libraries.
326-
327-
Why? because my time is spread pretty thin maintaining all of the libraries I have + LIFE,
328-
it is so freeing not to worry about it and will help me keep pouring out bigger and better
329-
things for you the community.
330-
331321
License
332322
------
333323
Distributed under MIT License, please see license file in code for more details.

decoder.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,11 @@ func (d *decoder) parseMapData() {
109109
// no need to check for error, it will always pass
110110
// as we have done the checking to ensure
111111
// the value is a number ahead of time.
112-
ke.ivalue, _ = strconv.Atoi(ke.value)
112+
var err error
113+
ke.ivalue, err = strconv.Atoi(ke.value)
114+
if err != nil {
115+
ke.ivalue = -1
116+
}
113117

114118
if ke.ivalue > rd.sliceLen {
115119
rd.sliceLen = ke.ivalue

decoder_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1909,3 +1909,30 @@ func TestDecoder_EmptyArrayBool(t *testing.T) {
19091909
err := d.Decode(v, in)
19101910
Equal(t, err, nil)
19111911
}
1912+
1913+
func TestDecoder_InvalidSliceIndex(t *testing.T) {
1914+
type PostsRequest struct {
1915+
PostIds []string
1916+
}
1917+
in := url.Values{
1918+
"PostIds[]": []string{"1", "2"},
1919+
}
1920+
1921+
v := new(PostsRequest)
1922+
d := NewDecoder()
1923+
err := d.Decode(v, in)
1924+
NotEqual(t, err, nil)
1925+
Equal(t, err.Error(), "Field Namespace:PostIds ERROR:invalid slice index ''")
1926+
1927+
// No error with proper name
1928+
type PostsRequest2 struct {
1929+
PostIds []string `form:"PostIds[]"`
1930+
}
1931+
1932+
v2 := new(PostsRequest2)
1933+
err = d.Decode(v2, in)
1934+
Equal(t, err, nil)
1935+
Equal(t, len(v2.PostIds), 2)
1936+
Equal(t, v2.PostIds[0], "1")
1937+
Equal(t, v2.PostIds[1], "2")
1938+
}

0 commit comments

Comments
 (0)