Sadly it seems to happen if you make your own functions too, so it possibly happens with any case where the compiler thinks that that buffer won’t be used anymore.
I see that the article provides a solution at the end. But it seems to me a warning should be made by the compiler, instead of having to adapt all functions to prevent this from happening. Or be able to disable this specific optimization.
If the memory is not accessed after you store something there (i.e., memset),
the compiler will reorder the instructions and the dead store is removed.
There was some interesting discussion on GCC’s bugzilla about this. Is that a bug or not? I think it is.
After some Googling I found the explicit_bzero function that @kristapsdz mentioned, but the glibc version. The trick it uses is to add asm volatile ("" ::: "memory")after the memset call. That prevents any stores prior to that point to be reordered.
Sadly it seems to happen if you make your own functions too, so it possibly happens with any case where the compiler thinks that that buffer won’t be used anymore.
I see that the article provides a solution at the end. But it seems to me a warning should be made by the compiler, instead of having to adapt all functions to prevent this from happening. Or be able to disable this specific optimization.
Yeah, after some reading it seems to be due to dead store optimization.
If the memory is not accessed after you store something there (i.e., memset), the compiler will reorder the instructions and the dead store is removed.
There was some interesting discussion on GCC’s bugzilla about this. Is that a bug or not? I think it is.
After some Googling I found the
explicit_bzerofunction that @kristapsdz mentioned, but the glibc version. The trick it uses is to addasm volatile ("" ::: "memory")after the memset call. That prevents any stores prior to that point to be reordered.The tag should be
C++, notC.Do you know how I can edit the tags? Or do the mods have to? Couldn’t find the option :(
Apparently you click suggest
Relevant: https://man.openbsd.org/explicit_bzero.3