tags:

views:

212

answers:

5

I was told that the optimal way to program in C++ is to use STL and string rather than arrays and character arrays.

i.e.,

vector<int> myInt;

rather than

int myInt[20]

However, I don't understand the rational behind why it would result in security problems.

+9  A: 

I suggest you read up on buffer overruns, then. It's much more likely that a programmer creates or risks buffer overruns when using raw arrays, since they give you less protection and don't offer an API. Sure, it's possible to shoot yourself in the foot using STL too, but at least it's harder.

unwind
"...but in C++, it blows your leg off" - Stroustrup
Dave Van den Eynde
+1  A: 

C++ arrays do not perform bounds checking, on either insert or read and it is quite easy to accidentally access items from outside of the array bounds.

From an OO perspective, the vector also has more knowledge about itself and so can take care of its own housekeeping.

Richard
+2  A: 

Arrays don't perform bound checking. Hence they are very vulnerable to bound checking errors which can be hard to detect.

Note: the following code has a programming error.

int Data[] = { 1, 2, 3, 4 };
int Sum = 0;

for (int i = 0; i <= 4; ++i) Sum += Data[i];

Using arrays like this, you won't get an exception that helps you find the error; only an incorrect result.

Arrays don't know their own size, whereas a vector defines begin and end methods to access its elements. With arrays you'll always have to rely on pointer arithmetics (And since they are nothing but pointers you can accidentially cast them)

Dario
Vectors don't perform bounds checking either, except perhaps in debug mode with libraries from some vendors. It's certainly not in vector's contract.
Frederik Slijkerman
Vectors do perform bounds checking if you use the at() member function instead of operator[]
anon
A: 

Your example has a static array with a fixed number of items; depending on your algorithm, this may be just as safe as a vector with a fixed number of items.

However, as a rule of thumb, when you want to dynamically allocate an array of items, a vector is much easier and also lets you make fewer mistakes. Any time you have to think, there's a possibility for a bug, which might be exploited.

Frederik Slijkerman
+4  A: 

There appears to be some confusion here about what security vectors can and cannot provide. Ignoring the use of iterators, there are three main ways of accessing elements ina vector.

  • the operator[] function of vector - this provides no bounds checking and will result in undefined behaviour on a bounds error, in the same way as an array would if you use an invalid index.

  • the at() vector member function - this provides bounds checking and will raise an exception if an invalid index is used, at a small performance cost

  • the C++ operator [] for the vector's underlying array - this provides no bounds checking, but gives the highest possible access speed.

anon
"will give exactly the same behaviour as an array would if you use an invalid index." I don't think that's guaranteed, beyond that they're both undefined and that the most likely implementation will result in it being the case.
Steve Jessop
by exactly the same I meant exactly the same undefined behaviour - I'll clarify the answer
anon