bugaevc / wl-clipboard

Command-line copy/paste utilities for Wayland
GNU General Public License v3.0
1.62k stars 60 forks source link

memset options to avoid uninitialized memory #67

Closed the-isz closed 5 years ago

the-isz commented 5 years ago

Ran across this while implementing the loops option: The global options variable should be zero'd before usage to avoid uninitialized memory.

IIRC, gcc is pretty defensive in this and zero's out memory even in release builds, but I'm pretty sure it's not required by any standard.

bugaevc commented 5 years ago

No, I am pretty sure it is required. Quoting from here:

If an object that has automatic storage duration is not initialized explicitly, its value is indeterminate. If an object that has static or thread storage duration is not initialized explicitly, then:

  • if it has pointer type, it is initialized to a null pointer;
  • if it has arithmetic type, it is initialized to (positive or unsigned) zero;
  • if it is an aggregate, every member is initialized (recursively) according to these rules, and any padding is initialized to zero bits;
  • if it is a union, the first named member is initialized (recursively) according to these rules, and any padding is initialized to zero bits;

See also: https://en.cppreference.com/w/c/language/initialization#Implicit_initialization