-
make a vector constant?
Code:
#include <iostream>
#include <vector>
using namespace std;
void test(vector <int*> v)
{
for (int i = 0; i < v.size(); i++)
{
if (v[0] == v[i])
{
cout << v[i] << endl;
}
else
{
cout << "ugly address man.\n";
}
}
}
int main()
{
int j = 2;
vector <int*> v; // size of the vector starts at 0
for (int i = 0; i < 2; i++)
{
if (i != 1)
{
v.push_back(&i); // v now has 1 element
test(v);
}
else
{
v.push_back(&j); // v now has 1 element
//v[1] = v[0];
test(v);
}
}
}
you see line 36 //v[1] = v[0]; change the element 1 to look like element 0 when uncommented? i want to know how to make the element 1 constant so it can't be changed. and do this for every element in the vector, please. i googled how but i didn't find anything.
-
i see here is something like what i want but its not possible using vector push back.
c++ - Why can't I push_back to a vector of const elements? - Stack Overflow
is there some other way to make an array dynamically grow, like vector push back, and also make the array elements const so they can't be changed?
-
apparently:
"The size of an array is static in C++. You cannot dynamically resize it. That's what std::vector is for:"
and
"With STL containers, the value type is required to be assignable. (not const)"
so the only way to ensure the contents are secure is to write to a file? arrays won't resize, and vectors can be made to copy other values insecurely into the element.
-
You're very focussed on the implementation, i.e., you have a vector<int*> and you want to constrain it in some way.
Another way of looking at things is that you have something that you're trying to model (represent as an abstraction), and internally it happens to make use of a vector<int*>. You then determine what operations you want to include in this thing that you model, and you then write a class that has a vector<int*> private member. So, naturally this class will not provide an operation that will result in assignment to an existing element of the internal vector.
-
this is what i came up with:
Code:
#include <iostream>
#include <vector>
#include <fstream>
#include <string>
using namespace std;
void ReadTheTest()
{
string myText;
string first;
int counter = 0;
ifstream MyReadFile("test.txt");
while (getline(MyReadFile, myText))
{
if (counter == 0)
{
first = myText;
}
else
{
if (first != myText)
{
cout << "bad address man.\n";
}
}
counter++;
}
MyReadFile.close();
}
void test(vector <int*> v)
{
for (int i = 0; i < v.size(); i++)
{
ofstream myfile;
myfile.open("test.txt", ios_base::app);
myfile << v[i] << "\n";
myfile.close();
}
}
int main()
{
int j = 2;
vector <int*> v; // size of the vector starts at 0
for (int i = 0; i < 2; i++)
{
if (i != 1)
{
v.push_back(&i); // v now has 1 element
ofstream myfile;
myfile.open("test.txt", ios_base::app);
myfile << v[i] << "\n";
myfile.close();
}
else
{
v.push_back(&j); // v now has 1 element
test(v);
v[1] = v[0];
}
test(v);
}
ReadTheTest();
}
in main, in the else, line 65, the element 0 wrote over element 1, which hides the fact that in line 63 the value j wrote to the element 1 too.
so for security when element 1 is going to be used it runs the test first. then if the vector element is wrote over its able to be identified.
-
That's a rather inefficient solution, and is based on detection rather than prevention. I recommend that you consider writing a class that I suggested in my previous post.
I note that none of these are really concerned with security per se: if you have an attacker who can change your code to insert such an assignment, then they can of course change your code to remove your test function call, or change your code to change the class I suggested, etc. Rather, the techniques here are for guiding (or even forcing) client code into doing what you expect.
-
write the j address in the file, this sends a error in the ReadTheTest function, then get a count of how many addresses are bad. should just be one. that function then sends a blip somewhere showing if its just one or no blip to show somethings wrong.
when does j get written in? some random time decided by the online server testing the SW.
Code:
#include <iostream>
#include <vector>
#include <fstream>
#include <string>
using namespace std;
void ReadTheTest()
{
string myText;
string first;
int counter = 0;
ifstream MyReadFile("test.txt");
while (getline(MyReadFile, myText))
{
if (counter == 0)
{
first = myText;
}
else
{
if (first != myText)
{
cout << "bad address man.\n";
}
}
counter++;
}
MyReadFile.close();
}
void test(vector <int*> v)
{
for (int i = 0; i < v.size(); i++)
{
ofstream myfile;
myfile.open("test.txt", ios_base::app);
myfile << v[i] << "\n";
myfile.close();
}
}
int main()
{
int x;
cout << "Type a number: "; // the online server randomly types in a number
cin >> x;
int j = 2;
vector <int*> v; // size of the vector starts at 0
for (int i = 0; i < 2; i++)
{
if ((i != 1) || (x != 1))
{
v.push_back(&i); // v now has 1 element
ofstream myfile;
myfile.open("test.txt", ios_base::app);
myfile << v[i] << "\n";
myfile.close();
}
else if (x == 1) // maybe this is also set by the online server
{
v.push_back(&j); // v now has 1 element
test(v);
v[1] = v[0];
}
test(v);
}
ReadTheTest();
}
-
Compile this program:
Code:
#include <iostream>
#include <vector>
class AddressVerifier
{
public:
void push_back(int* address)
{
addresses.push_back(address);
}
const int* operator[](std::size_t i) const
{
return addresses[i];
}
std::size_t size() const
{
return addresses.size();
}
private:
std::vector<int*> addresses;
};
using namespace std;
void test(AddressVerifier& v)
{
for (size_t i = 0; i < v.size(); i++)
{
if (v[0] == v[i])
{
cout << v[i] << endl;
}
else
{
cout << "ugly address man.\n";
}
}
}
int main()
{
int j = 2;
AddressVerifier v;
for (int i = 0; i < 2; i++)
{
if (i != 1)
{
v.push_back(&i); // v now has 1 element
test(v);
}
else
{
v.push_back(&j); // v now has 1 element
//v[1] = v[0];
test(v);
}
}
}
You can see that it follows very closely your program in post #1, except for the replacement of the direct use of the vector with the AddressVerifier class.
Uncomment the line that you were talking about in post #1 and observe the compile error.
-
-
here's how i delete the vector in the code in post 7. does this look right, the last 2 lines in main:
Code:
#include <iostream>
#include <vector>
#include <fstream>
#include <string>
using namespace std;
void ReadTheTest()
{
string myText;
string first;
int counter = 0;
ifstream MyReadFile("test.txt");
while (getline(MyReadFile, myText))
{
if (counter == 0)
{
first = myText;
}
else
{
if (first != myText)
{
cout << "bad address man.\n";
}
}
counter++;
}
MyReadFile.close();
}
void test(vector <int*> v)
{
for (int i = 0; i < v.size(); i++)
{
ofstream myfile;
myfile.open("test.txt", ios_base::app);
myfile << v [ i ] << "\n";
myfile.close();
}
}
int main()
{
int x;
cout << "Type a number: "; // the online server randomly types in a number
cin >> x;
int j = 2;
vector <int*> v; // size of the vector starts at 0
for (int i = 0; i < 2; i++)
{
if ((i != 1) || (x != 1))
{
v.push_back(&i); // v now has 1 element
ofstream myfile;
myfile.open("test.txt", ios_base::app);
myfile << v [ i ] << "\n";
myfile.close();
}
else if (x == 1) // maybe this is also set by the online server
{
v.push_back(&j); // v now has 1 element
test(v);
v[1] = v[0];
}
test(v);
}
ReadTheTest();
v.clear();
v.shrink_to_fit();
}
to do the same in the code you made in post 8, just make a setter that does these last 2 lines in main in the class then call that setter when your done with the vector.
-
It would be pointless since the main function ends immediately afterwards.
-
No, you cannot delete what you (or a library that you used) did not new.
But again, it is pointless to manually destroy the vector when it would have been automatically destroyed immediately after the code that you use to destroy it.
-
i saw that but i wasn't sure. ty.