# /= doesn't do what you think it does?

Printable View

Show 80 post(s) from this thread on one page
Page 2 of 4 First 1234 Last
• 09-21-2008
anon
What I did is use the at method where the index for F comes from an "untrusted" source (in very indirect ways from a map). Try this:

Code:

```#include <iostream> #include <vector> #include <map> #include <exception> using namespace std; int main(){     try {             vector<vector<float> > kF;             vector<float> F;             map <int, string> namesmap;             namesmap[1] = "one";             namesmap[2] = "two";             namesmap[3] = "three";             namesmap[4] = "four";             vector<map<string,int> > kFmap;             kFmap.resize(2);             kFmap[0][namesmap[1]] = 5;             kFmap[0][namesmap[2]] = 10;             kFmap[0][namesmap[3]] = 5;             kFmap[0][namesmap[4]] = 20;             kFmap[1][namesmap[1]] = 5;             kFmap[1][namesmap[2]] = 5;             kFmap[1][namesmap[3]] = 5;             kFmap[1][namesmap[4]] = 20;             for (unsigned int k = 0; k < 2; k++){                     cout << "k = " << k << endl;                     F.resize(6);                     for (int i = 1; i <= 4; i++){                             F.at(kFmap[k][namesmap[i]])++;                     }                     for (int i = 1; i <= 4; i++){                             cout << kFmap[k][namesmap[i]] << " kFmap | ";                             cout << F.at(kFmap[k][namesmap[i]]) << " | ";                             F.at(kFmap[k][namesmap[i]]) /= (10); //bug                             cout << F.at(kFmap[k][namesmap[i]]) << " | ";                             cout << endl;                     } cout << endl;                     kF.push_back(F);                     F.clear();             }     }     catch (exception& e) {         cout << e.what() << '\n';     }         return 0; }```
• 09-21-2008
elninio
What is the reasoning behind this? Can you tell me about untrusted sources? I'd like to know how this stuff works...

Edit: that didn't do it for me, I get vector::_M_range_check error. And when I try it in my main program I don't even get that - just the same faulty results!
• 09-21-2008
laserlight
Quote:

Originally Posted by Salem
If you extract this code, put a minimal support environment around it, and it works, then you can be pretty sure the real problem is somewhere else.

Indeed, hence the idea is not "to extend it to wider sets", but to use it to determine if the problem is in that portion of code, or likely to be elsewhere.

Quote:

Originally Posted by elninio
What is the reasoning behind this? Can you tell me about untrusted sources? I'd like to know how this stuff works...

I think anon made the same mistake as me: talking too much :)

Here "untrusted" merely means that you cannot be sure that the index provided by the "source" is a valid index for use with the vector. You can either perform a check yourself (compare the index with the vector's size()), or use at() with the index, upon which a std::out_of_range exception will be thrown if the index is invalid (out of range).

Quote:

Originally Posted by elninio
Edit: that didn't do it for me, I get vector::_M_range_check error.

That means that an exception was thrown, and you are using an index that is out of range (i.e., trying to access the vector out of bounds).
• 09-21-2008
anon
It looks that you are trying to use the values in the map, such as 10 and 20, to index into a vector that has only size 6. Your logic doesn't hold.

By "untrusted" I meant a value that doesn't come from an obvious place (such as a loop counter) and that isn't validated.
• 09-21-2008
CornedBee
There's no such thing as C++99. There's C99, which is completely irrevelant to your problem.

Stop seeking the problem with the compiler. It does not contain bugs that any application that is not extremely complex or irregular will uncover.
• 09-21-2008
elninio
Quote:

Originally Posted by CornedBee
There's no such thing as C++99. There's C99, which is completely irrevelant to your problem.

Stop seeking the problem with the compiler. It does not contain bugs that any application that is not extremely complex or irregular will uncover.

Not seeking to put blame on the compiler, just hoping that maybe some newer standard could give me a warning about some bad practice which i may be doing that may be contributing to the problem.
• 09-21-2008
elninio
Quote:

Originally Posted by anon
It looks that you are trying to use the values in the map, such as 10 and 20, to index into a vector that has only size 6. Your logic doesn't hold.

By "untrusted" I meant a value that doesn't come from an obvious place (such as a loop counter) and that isn't validated.

The size 6 refers to the span of maps, 10 and 20 are INSIDE the map, right?

vector [ map1 map2 ...]
5 5
10 5
5 5
20 20
• 09-21-2008
tabstop
Quote:

Originally Posted by elninio
The size 6 refers to the span of maps, 10 and 20 are INSIDE the map, right?

vector [ map1 map2 ...]
5 5
10 5
5 5
20 20

so kFmap returns 5, 10, and 20 as above, yes. Then you do F[number you just got] -- but F[10] and F[20] don't exist....
• 09-21-2008
laserlight
One example of a possible out of bounds access is this:
Code:

```for (int i = 1; i <= 4; i++){     F.at(kFmap[k][namesmap[i]])++; }```
kFmap only has 2 elements, but you access kFmap[2], kFmap[3] and kFmap[4].
• 09-21-2008
iMalc
Quote:

Originally Posted by laserlight
One example of a possible out of bounds access is this:
Code:

```for (int i = 1; i <= 4; i++){     F.at(kFmap[k][namesmap[i]])++; }```
kFmap only has 2 elements, but you access kFmap[2], kFmap[3] and kFmap[4].

Yep, it looks to me like that explains the problem too.
• 09-21-2008
tabstop
Quote:

Originally Posted by laserlight
One example of a possible out of bounds access is this:
Code:

```for (int i = 1; i <= 4; i++){     F.at(kFmap[k][namesmap[i]])++; }```
kFmap only has 2 elements, but you access kFmap[2], kFmap[3] and kFmap[4].

And k only looped from zero to one.
• 09-21-2008
elninio
Quote:

Originally Posted by laserlight
One example of a possible out of bounds access is this:
Code:

```for (int i = 1; i <= 4; i++){     F.at(kFmap[k][namesmap[i]])++; }```
kFmap only has 2 elements, but you access kFmap[2], kFmap[3] and kFmap[4].

the ith elemeth of kFmap exists. I have two maps of size 4 in it.

I don't access kFmap[3] and [4] because the k for loop only goes up to 2.

F.at(kFmap[k][namesmap[i]])++; , namesmap[i] goes up to 4, and that is VALID.

kFmap[k] goes up to < 2 , and that is also VALID.
• 09-21-2008
elninio
Quote:

Originally Posted by tabstop
And k only looped from zero to one.

Yes, thats the whole point. kFmap is only defined from 0 to 1. The elements within its map are defined from 1 2 3 4. Valid again.
• 09-21-2008
elninio
Quote:

Originally Posted by tabstop
so kFmap returns 5, 10, and 20 as above, yes. Then you do F[number you just got] -- but F[10] and F[20] don't exist....

you never access F[10], F increments in terms of k. so only 0 and 1 are accessed. We are accessing the kth element. Look at the K for loop, it only goes to < 2.
• 09-21-2008
tabstop
Quote:

Originally Posted by elninio
you never access F[10], F increments in terms of k. so only 0 and 1 are accessed. We are accessing the kth element. Look at the K for loop, it only goes to < 2.

Read your own code:
Code:

`F.at(kFmap[k][namesmap[i]]) /= (10);`
Let's suppose k is 0 and i is 4.
Then kFmap[0] is fine, that gets us a map. namesmap[4] is "four". We can then do the mapping -- kFmap[0]["four"] = 20. (You assigned it as kFmap[0][namesmap[4]], but that's fine.) So, the bit inside the at is 20 -- but F.at(20) throws an exception, since F only goes to 5.
Show 80 post(s) from this thread on one page
Page 2 of 4 First 1234 Last