Re: Subject : Help regarding Bug 90341 - Clean up excessive const_cast'ing

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi,

On Wed, Nov 23, 2022 at 8:37 PM Siddharth Khattar <skhattar111@xxxxxxxxx> wrote:
Hello all,

So I was aiming to do a medium difficulty level easy hack and came
across this one (Bug 90341) which interested me. I then studied what
const_cast does and what was the main issue that this bug was
addressing. As I looked into it more and ran the command " git grep
--word-regexp --count const_cast | sort --numeric-sort --key=2
--field-separator=: ", I got various results and decided to look into
core/xmloff/source/style/impastpl.cxx (lines 144 & 146).
I also decided to go through the previous commits made in the bug
thread for ideas and help, such as these:
https://git.libreoffice.org/core/+/7b152167a4e4c3eaac95aee8f282873681c90092%5E%21
and https://cgit.freedesktop.org/libreoffice/core/commit/?id=4ae319ae462f3f094452046e392c8c15446736ae.
I then decided to experiment with the file (impastpl.cxx) and then
removed const & const_cast from the above-mentioned lines. Then, when
I tried to build from source I got these errors (please see attached
text file).

I wasn't sure what to make of these errors as nothing helpful came
when I googled them & I also am not heavily experienced in c++ coding.
Then I started reading some documentation of the xmloff file to come
to understand that these files were related to outputting "basic ODF
import/export filter implementation for most applications".

One of the people at LibreOffice (buovjaga) also helped me with this
and pointed out that the any2string function in that file appeared in:
https://git.libreoffice.org/core/commit/c1015fdd51909495cefdc0258c3899cd73d4d2df.
I pondered about contacting the original contributor who pushed this
commit for help but as the commit was 9+ years old I dropped that idea
pretty quickly
I'd really appreciate it if the dev team could help me in any way with
this easy hack and tell me in which direction I should go!

It's quite simple - if a variable is const it means it should not be changed. any.getValue() is returning such a variable (const void*), but the variable is then put into data2string function, which accepts a non-const variable that can be changed (void*).
So if you don't o a const_cast, the compiler will complain that you can't put a variable that is said to be unchangeable into a function where it requires the variable to be changeable, so it complains that it can't do that.

This is the reason there was/is a const_cast at that place, which says that "the compiler is dumb and doesn't know what's talking about so covert that unchangable variable to be changeable so the compiler is silent and stops bugging the developers". 

So how to solve this - at least in this case: looking at the data2string function the "void* data" input variable is later on cast to a const something variable anyway, so maybe try to change the input variable type from "void* data" to "const void* data" and fix any issues you have later on in the body with this data variable.

Also note that in another const_cast case the situation might be different and you must check how to solve the issue on a case by case basis (some possible solutions are in the bug report)
 
Thanking you for your time,
Siddharth Khattar

Tomaž

[Index of Archives]     [LARTC]     [Bugtraq]     [Yosemite Forum]     [Photo]

  Powered by Linux