C++ Logo

std-discussion

Advanced search

Re: C++ language has a big security hole.

From: Amit <amitchoudhary0523_at_[hidden]>
Date: Mon, 17 Feb 2025 15:19:15 +0530
So, you are ok with 'private' keyword being basically useless.

Regards,
Amit


On Mon, Feb 17, 2025, 3:17 PM Tiago Freire via Std-Discussion <
std-discussion_at_[hidden]> wrote:

> This is not a security hole.
> And it has little to do with the language.
> This is just how computers work.
>
> This is not a proposition, it's just spam.
>
>
>
> ------------------------------
> *From:* Std-Discussion <std-discussion-bounces_at_[hidden]> on
> behalf of Amit via Std-Discussion <std-discussion_at_[hidden]>
> *Sent:* Monday, February 17, 2025 10:33:23 AM
> *To:* std-discussion_at_[hidden] <std-discussion_at_[hidden]>
> *Cc:* Amit <amitchoudhary0523_at_[hidden]>
> *Subject:* [std-discussion] C++ language has a big security hole.
>
> C++ language has a big security hole. You can change the values of the
> private member variables directly by getting the pointer to the
> object. So, private member variables are actually not private, they
> are public. Below is the example code:
>
>
> --------------------------------------------------------------------------------
>
> #include <iostream>
>
> using namespace std;
>
> class MyClass
> {
>
> private:
> int i;
> int j;
>
> public:
> MyClass(int a, int b)
> {
> i = a;
> j = b;
> }
>
> void print_data()
> {
> cout << endl;
> cout << "i = " << i << ", j = " << j;
> }
>
> }; // end of class MyClass
>
> int main(void)
> {
>
> MyClass myobj(1, 4);
>
> myobj.print_data();
>
> MyClass *m = &myobj;
>
> int *i_ptr = (int *)(m);
> int *j_ptr = i_ptr + 1;
>
> *i_ptr = 10;
> *j_ptr = 20;
>
> myobj.print_data();
>
> cout << endl << endl;
>
> return 0;
>
> } // end of function main()
>
>
> --------------------------------------------------------------------------------
>
> The output is:
>
> i = 1, j = 4
> <https://www.google.com/maps/search/4+%0D%0Ai+%3D+10?entry=gmail&source=g>
> i = 10
> <https://www.google.com/maps/search/4+%0D%0Ai+%3D+10?entry=gmail&source=g>,
> j = 20
>
> So, you see that the values of the private member variables ('i' and
> 'j') were changed directly by using pointers. So, the 'private'
> keyword actually didn't serve its purpose.
>
> Regards,
> Amit
> --
> Std-Discussion mailing list
> Std-Discussion_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-discussion
>
> --
> Std-Discussion mailing list
> Std-Discussion_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-discussion
>

Received on 2025-02-17 09:49:28