Fix Blob::offset lower-bound checks guarding the dim instead of the index#7107
Open
Chessing234 wants to merge 1 commit intoBVLC:masterfrom
Open
Fix Blob::offset lower-bound checks guarding the dim instead of the index#7107Chessing234 wants to merge 1 commit intoBVLC:masterfrom
Chessing234 wants to merge 1 commit intoBVLC:masterfrom
Conversation
…ndex Issue BVLC#6391. The four-argument Blob::offset(n, c, h, w) has lower-bound guards of the form: CHECK_GE(n, 0); CHECK_GE(channels(), 0); CHECK_GE(height(), 0); CHECK_GE(width(), 0); Only the first line checks the index. The other three check the *dimensions* of the blob (channels(), height(), width()), which for any constructed blob are structurally non-negative -- these are vacuous asserts that fire only if the blob itself is malformed, and never guard the index the caller passed in. A negative c/h/w is silently accepted and then used to compute ((n * channels() + c) * height() + h) * width() + w pointing outside the buffer. The vector overload a few lines below already gets this right: CHECK_GE(indices[i], 0); CHECK_LT(indices[i], shape(i)); This change only touches the three misnamed CHECK_GE calls so the lower bounds match the vector overload and the bounds the call actually needs. Upper-bound hardening (CHECK_LE -> CHECK_LT) is out of scope; it is handled separately by BVLC#7102.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Issue #6391 (complements #7102, which fixes the upper bounds).
Bug
In
include/caffe/blob.hpp, the four-argumentBlob::offset(n, c, h, w)writes its lower-bound guards as:Only the first
CHECK_GE(n, 0)actually checks the index. The next threeCHECK_GEcalls check the dimensions of the blob (channels(),height(),width()), not the indicesc,h,w. For any constructed blob those dims are structurally non-negative, so the asserts are vacuous — a negativec/h/wis accepted and then fed intowhich addresses memory outside the buffer.
Root cause
Almost certainly a copy-paste slip when this legacy-accessor overload was written. The sibling vector overload immediately below shows the intended pattern:
— guarding the index, not the dimension.
Fix
Point the three
CHECK_GEcalls atc,h,w(the actual indices) so the legacy overload enforces the same[0, dim)contract as the vector overload:Scope is intentionally limited to the lower-bound variable mistake. Upper-bound hardening (
CHECK_LE→CHECK_LT) is handled separately by #7102; the two PRs touch disjoint lines in the same function and can land in either order without conflict.