when i debug it using VC, it will report "Expression: vector subscript out of range".
vector subscript out of range #4239
pull LongShao007 wants to merge 1 commits into bitcoin:master from LongShao007:patch-3 changing 1 files +4 −4-
LongShao007 commented at 7:38 AM on May 28, 2014: contributor
-
34cb1fc5d6
vector subscript out of range
when i debug it using VC, it will report "Expression: vector subscript out of range".
-
laanwj commented at 7:41 AM on May 28, 2014: member
This change is not correct. VC is correct that the vector subscript is out of range, but in this case that is what we want, because we're trying to serialize the entire array.
-
BitcoinPullTester commented at 7:57 AM on May 28, 2014: none
Automatic sanity-testing: FAILED BUILD/TEST, see http://jenkins.bluematt.me/pull-tester/34cb1fc5d6c7293b5c135b85c3dd3668e05ac2b8 for binaries and test log.
This could happen for one of several reasons:
- It chanages paths in makefile.linux-mingw or otherwise changes build scripts in a way that made them incompatible with the automated testing scripts (please tweak those patches in qa/pull-tester)
- It adds/modifies tests which test network rules (thanks for doing that), which conflicts with a patch applied at test time
- It does not build on either Linux i386 or Win32 (via MinGW cross compile)
- The test suite fails on either Linux i386 or Win32
- The block test-cases failed (lookup the first bNN identifier which failed in https://github.com/TheBlueMatt/test-scripts/blob/master/FullBlockTestGenerator.java)
If you believe this to be in error, please ping BlueMatt on freenode or TheBlueMatt here.
This test script verifies pulls every time they are updated. It, however, dies sometimes and fails to test properly. If you are waiting on a test, please check timestamps to verify that the test.log is moving at http://jenkins.bluematt.me/pull-tester/current/ Contact BlueMatt on freenode if something looks broken.
-
haowang1013 commented at 9:35 AM on May 28, 2014: none
Shouldn't the correct fix be to still use &compr[compr.size()-1] while change CFlatData to use inclusive upper bound?
By definition compr[compr.size()] is a out of bound read even though it may not cause noticeable errors in certain implementations.
-
laanwj commented at 9:49 AM on May 28, 2014: member
Inclusive upper bound is incredibly rare in computer science. Would be extremely confusing. It could be considered to pass a pointer and a size though.
Note that taking the address of compr[compr.size()] is not an out-of-bounds access in itself. The memory is never accessed.
-
sipa commented at 9:52 AM on May 28, 2014: member
Would using compr.end() work? It returns an iterator pointing to the same address as &compr[compr.size()].
-
haowang1013 commented at 10:02 AM on May 28, 2014: none
Good point, I think compr.end() works the best in terms of satisfying the runtime as well as increasing readability.
I personally don't have preference between inclusive and exclusive upper bounds, none of them is more confusing than the other :)
-
laanwj commented at 10:26 AM on May 28, 2014: member
The foremost problem with inclusive upper bounds is that they also require a record/type size. An exclusive upper bound is unambiguously the top of the address range encompassing the objects.
-
laanwj commented at 7:03 AM on May 29, 2014: member
Using .end() makes sense here. But you'd still need something like
&(*compr.begin())and&(*compr.end())to go from a vector random access iterator to a pointer. That may trigger yet another warning or may even fail in some cases if extra debug checking is compiled in.Seemingly, getting the beginning and end pointer of a vector is not trivial to do in a way that is guaranteed to be portable and guaranteed to be working for empty vectors.
See also http://stackoverflow.com/a/1339767/216357 which introduces
begin_ptrandend_ptrfunctions. -
LongShao007 commented at 10:12 AM on May 29, 2014: contributor
ok, i will try!
-
brandondahler commented at 2:45 AM on May 30, 2014: contributor
Why not use pointer arithmetic? This would prevent all warning messages, produce the same results as it currently is, and look purposeful (since it is).
s << CFlatData(&compr[0], (&compr[0]) + compr.size()); -
laanwj commented at 6:36 AM on May 30, 2014: member
@brandondahler Did you check the link I gave?
&compr[0]won't work portably for empty vectors (you're still referencing out of bounds in that case). That's why I referred to the stackoverflow topic which defines the following, which I think makes sense:template <class T, class TAl> inline T* begin_ptr(std::vector<T,TAl>& v) {return v.empty() ? NULL : &v[0];} template <class T, class TAl> inline const T* begin_ptr(const std::vector<T,TAl>& v) {return v.empty() ? NULL : &v[0];} template <class T, class TAl> inline T* end_ptr(std::vector<T,TAl>& v) {return v.empty() ? NULL : (begin_ptr(v) + v.size());} template <class T, class TAl> inline const T* end_ptr(const std::vector<T,TAl>& v) {return v.empty() ? NULL : (begin_ptr(v) + v.size());} - LongShao007 closed this on Jun 3, 2014
- LongShao007 deleted the branch on Jun 3, 2014
- laanwj referenced this in commit 7d3fcf9744 on Jun 5, 2014
- laanwj referenced this in commit 2ede43ebe0 on Jun 5, 2014
- laanwj referenced this in commit b7075118d6 on Jun 23, 2014
- laanwj referenced this in commit fa126effc2 on Jun 23, 2014
- MathyV referenced this in commit e47268421f on Nov 3, 2014
- reddink referenced this in commit c55b5a9686 on May 27, 2020
- DrahtBot locked this on Sep 8, 2021