-
Notifications
You must be signed in to change notification settings - Fork 126
Improve flate decompression logic and significantly improve memory usage #340
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
Conversation
| capHint := limit | ||
| if l <= limit/2 { | ||
| capHint = l * 2 | ||
| } | ||
| decompressed.Grow(capHint) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe this can be simplified to:
decompressed := bytes.NewBuffer(make([]byte, 0, min(limit, len(compressed) * 2)))There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The if checking the l <= limit/2 protects against an int overflow on 32 bit devices if a packet is 1gb, which is impossible but having the if instead of min is technically correct
| // Handle no limit | ||
| if limit == math.MaxInt { | ||
| if _, err := io.Copy(&decompressed, r); err != nil { | ||
| return nil, fmt.Errorf("decompress flate: %w", err) | ||
| } | ||
| return decompressed.Bytes(), nil | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there ever a sensible case to have no limit? I get that this is probably to avoid overflow, but could just do a max(limit + 1, math.MaxInt) somewhere.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good idea
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
limit + 1 would cause an overflow so we cant handle this without checking limit == math.MaxInt, which is what the listener sets if the user configures no limit
gophertunnel/minecraft/listener.go
Line 133 in 8096f00
| cfg.MaxDecompressedLen = math.MaxInt |
Sandertv
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think these changes look sensible. I assume this has been tested thoroughly?
Uh oh!
There was an error while loading. Please reload this page.