Re: AW: A: a new bug to old plain C

LIU Hao via Gcc-help <[email protected]>
Newsgroups gmane.comp.gcc.help
Message-ID <[email protected]>
在 2025-12-5 16:10, [email protected] 写道:
> Refering to that example
> 
> int compare (const void *a1, const void *b1) {
>    char *const *a = a1;
>     ...
> 
> Here a and a1 have a different types.
> 
This conversion is allowed due to the special rule about pointer-to-void, which can indeed be exploited 
to modify a const-qualified object (https://gcc.godbolt.org/z/qa7qM9W8Y):

    int
    main(void)
      {
        const char* s1 = "meow";
        void* p = &s1;
        char** pp = p;   // implicit conversion from `void*` drops qualifier
        **pp = 'b';
      }

It can also be exploited in C++ to modify a const-qualified object without `const_cast` 
(https://gcc.godbolt.org/z/cKo3Ghfaq). It's nothing about safety; it's only about what is defined and 
what is not. Anything that is not defined is undefined. There are all kinds of holes in the standard.



-- 
Best regards,
LIU Hao
OpenPGP_signature.asc (application/pgp-signature, 840 B)
-----BEGIN PGP SIGNATURE-----

wsF5BAABCAAjFiEEcrrYZtHjIV4P0vZZ6rypbz4rVSkFAmkyl+oFAwAAAAAACgkQ6rypbz4rVSkq
ug/6A5H10JNw538/TgmFAwfc6RF9vfjiZ82fJ+vxj+i9YB7mZmGgPtyk7vDHavTO7+XL5/xj2DtN
G6yUVq/9n/XdwlDRthJJR63lqyD9A0SS51444K9AM6V0COZY6gSZnhHV99PGVPfzg+skyIPupIWf
DYLKrzNi1j5/VpWOyCwH8/FPAiIJc2dJ8IqwGxWYNe0dp7qOe38XJlSIStDqa/vfV3GoMpBNATnA
D6q40K65NR+hI7M7813yYsL7mK5V7Z0bhAEL/kxnhwrTKkk7wsSqyDTTAKyvEobfcB9LbqOlSHt8
GEokCIoXSVUCW3GaHlE7uo/NvUnR7JNN2xZI+rCxHJcpp2LxnmUNBs9ceqDpi2SJ5cMyo7hrknRa
rszvUjr8gcZx+aYHboh0qnlqZxrDjwG6DET8A45DrZ5s503w98lQFVvPxY+zY5jQf850L6eGjt62
50f6YjWKDSB3EFeIU4z2zghXx/NatAeXzEecpYhtsw6ktvqYP1qFf0+6iDkO0xEwSg1h4Gdv/NFe
oL3hoKJ7jMxUUaSMXcQY1/TYtW8ELuNq9WSZYbp0YkWuj555nhr+ga9fVVMFlskeU7/zZ/DKbJwi
UjNRt/5IAyBrNvNZC3U+BJgqxHV7z8oRjsqhK239lUKDLrvWImYxgljC2iMEm+3bOv4vM6DmdisX
Hs4=
=uwYs
-----END PGP SIGNATURE-----
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.