-
Notifications
You must be signed in to change notification settings - Fork 2
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
fix: limit max size #15
Conversation
func Parse(r io.Reader) (rules []Rule, err error) { | ||
s := bufio.NewScanner(r) | ||
// not too large | ||
b, err := bufio.NewReaderSize(r, maxFileSizeInBytes+1).Peek(maxFileSizeInBytes + 1) | ||
if err != nil && err != io.EOF { | ||
return nil, err | ||
} | ||
if len(b) > maxFileSizeInBytes { | ||
return nil, fmt.Errorf("redirects file size cannot exceed %d bytes", maxFileSizeInBytes) | ||
} | ||
|
||
s := bufio.NewScanner(bytes.NewReader(b)) | ||
for s.Scan() { |
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.
@Jorropo any room for improvement here? I guess we need to read entire thing anyway, so does not matter much, but asking just to be sure i am not missing something obvious.
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 don't see why you use bufio.Reader
with a .Peek
this large, it's a complex beast inside.
You can just do io.ReadAll(&io.LimitedReader{r, maxFileSizeInBytes+1})
.
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.
You can even feed the LimitedReader
directly to the scanner if you want.
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.
You could also match rules while reading instead of returning a list of rules, allowing for a fully streaming implementation.
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.
Thanks, I've switched to simpler code with LimitedReader and streaming in #18
Closes #11
Will need to rebase after #14 is merged.