forked from nullstyle/go-xdr
-
Notifications
You must be signed in to change notification settings - Fork 12
decoder: add MaxOutputBytes decode option #22
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
c677e1b
decoder: add MaxOutputBytes option for cumulative decoded output size…
tamirms 0a81e76
Strengthen not_reached test assertion with errors.Is check
tamirms 996964b
Rename MaxOutputBytes to MaxDecodedSize for clarity
tamirms fcf91d6
Rename MaxDecodedSize to MaxMemoryBytes
tamirms 9948664
Bump DecodeDefaultMaxDepth from 200 to 250
tamirms 46e87d7
ci: add complete job for stable branch protection
tamirms 105ca0d
ci: restrict GITHUB_TOKEN permissions to read-only
tamirms File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,3 @@ | ||
| module github.com/stellar/go-xdr | ||
|
|
||
| go 1.12 | ||
| go 1.25 | ||
|
tamirms marked this conversation as resolved.
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,11 @@ | ||
| #!/bin/bash | ||
| # The script does automatic checking on a Go package and its sub-packages, including: | ||
| # 1. goimports (https://golang.org/x/tools/cmd/goimports) | ||
| # 2. golint (https://github.com/golang/lint) | ||
| # 3. go vet (https://golang.org/cmd/vet) | ||
| # 4. test coverage (https://blog.golang.org/cover) | ||
| # 2. go vet (https://golang.org/cmd/vet) | ||
| # 3. test coverage (https://blog.golang.org/cover) | ||
|
|
||
| set -ex | ||
|
|
||
| test -z "$(goimports -l -w .)" | ||
| test -z "$(golint ./... )" | ||
| go vet ./... | ||
| go test -covermode=atomic -race ./... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| package xdr | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "encoding/binary" | ||
| "errors" | ||
| "testing" | ||
| "unsafe" | ||
| ) | ||
|
|
||
| // wideStruct has a large in-memory footprint (256 bytes) relative to its | ||
| // minimal XDR wire representation. | ||
| type wideStruct struct { | ||
| F0, F1, F2, F3, F4, F5, F6, F7 int64 | ||
| F8, F9, F10, F11, F12, F13, F14, F15 int64 | ||
| F16, F17, F18, F19, F20, F21, F22, F23 int64 | ||
| F24, F25, F26, F27, F28, F29, F30, F31 int64 | ||
| } | ||
|
|
||
| // makeArrayPayload creates an XDR-encoded variable-length array header | ||
| // followed by zero-filled element data. | ||
| func makeArrayPayload(payloadSize int) []byte { | ||
| payload := make([]byte, payloadSize) | ||
| declaredLen := uint32(payloadSize - 4) | ||
| binary.BigEndian.PutUint32(payload[0:4], declaredLen) | ||
| return payload | ||
| } | ||
|
|
||
| func TestMaxMemoryBytes(t *testing.T) { | ||
| payloadSize := 100000 | ||
| payload := makeArrayPayload(payloadSize) | ||
| structSize := int64(unsafe.Sizeof(wideStruct{})) // 256 | ||
|
tamirms marked this conversation as resolved.
|
||
|
|
||
| t.Run("unlimited", func(t *testing.T) { | ||
| var result []wideStruct | ||
| reader := bytes.NewReader(payload) | ||
| _, err := UnmarshalWithOptions(reader, &result, DecodeOptions{}) | ||
| if len(result) == 0 { | ||
| t.Errorf("expected some decoded elements with no limit, err=%v", err) | ||
| } | ||
| }) | ||
|
tamirms marked this conversation as resolved.
|
||
|
|
||
| t.Run("exceeded", func(t *testing.T) { | ||
| budget := int64(256) * structSize // 65536 bytes | ||
| var result []wideStruct | ||
| reader := bytes.NewReader(payload) | ||
| _, err := UnmarshalWithOptions(reader, &result, DecodeOptions{ | ||
| MaxMemoryBytes: budget, | ||
| }) | ||
| if err == nil { | ||
| t.Error("expected error when output byte limit exceeded") | ||
| } | ||
| maxExpected := int(budget/structSize) + 1 | ||
| if len(result) > maxExpected { | ||
| t.Errorf("decoded %d elements, expected at most %d", len(result), maxExpected) | ||
| } | ||
| }) | ||
|
|
||
| t.Run("not_reached", func(t *testing.T) { | ||
| // Budget larger than what the payload can produce — decode | ||
| // should succeed without hitting the limit. | ||
| budget := int64(3000) * structSize | ||
| var result []wideStruct | ||
| reader := bytes.NewReader(payload) | ||
| _, err := UnmarshalWithOptions(reader, &result, DecodeOptions{ | ||
| MaxMemoryBytes: budget, | ||
| }) | ||
| if errors.Is(err, ErrMemoryLimitExceeded) { | ||
| t.Errorf("expected budget not to be exceeded, got %v", err) | ||
| } | ||
| if len(result) == 0 { | ||
| t.Errorf("expected some decoded elements before hitting end of input, err=%v", err) | ||
| } | ||
| }) | ||
|
tamirms marked this conversation as resolved.
|
||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.