看汇编代码。
注意四个now()之间的代码。
编译器检测出通过memcpy给dst赋值了但没用,没调用memcpy
要想不让它优化掉memcpy,可以在后面printf引用一下dst里的某处内容,或者给dst加volatile。
可以查一下SecureZeroMemory怎么来的,就是因为用memset()清空内存中的密码这个操作,会被优化掉。
Notes
memset may be optimized away (under the as-if rules) if the object modified by this function is not accessed again for the rest of its lifetime (e.g., gcc bug 8537). For that reason, this function cannot be used to scrub memory (e.g., to fill an array that stored a password with zeroes).
This optimization is prohibited for memset_explicit and memset_s: they are guaranteed to perform the memory write.
Third-party solutions for that include FreeBSD explicit_bzero or Microsoft SecureZeroMemory.
https://en.cppreference.com/w/c/string/byte/memset
--
修改:z16166 FROM 123.118.191.*
FROM 123.118.191.*