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

Check overflow for memory read #121

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions rvgo/fast/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,12 @@ func (r *memReader) Read(dest []byte) (n int, err error) {
return 0, io.EOF
}

if r.addr > 1<<64-1-r.count {
Copy link
Contributor

Choose a reason for hiding this comment

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

Can r.count exceed 63 causing underflow? Maybe it makes sense to do the following?

Suggested change
if r.addr > 1<<64-1-r.count {
if r.count < 64 && r.addr > 1<<64-1-r.count {

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This is done to check if the r.addr + r.count does not exceed uint64 max (1<<64-1). I think bit shift has higher operator precedence than subtraction, so underflow won't happen.

// If adding r.count to r.addr would exceed the maximum uint64 value,
// we consider that an overflow condition and stop.
return 0, io.EOF
}

// Keep iterating over memory until we have all our data.
// It may wrap around the address range, and may not be aligned
endAddr := r.addr + r.count
Expand Down
Loading