1. 10

  2. 6

    A bit of tangentially related trivia from The Design and Implementation of the 4.3BSD Operating System (pg 57):

    The stack is also created from zero-filled memory. Although the stack should not need to be zero filled, early UNIX systems made it so. In an attempt to save some startup time, the kernel was modified to skip the zero filling of the stack, leaving the random previous contents of the page instead. Numerous programs stopped working because they depended on the local variables in their main procedure being initialized to zero. Consequently, the zero filling of the stack was restored.

    1. 3

      This is exactly why I always zero-initialize everything on the stack (for non-native types like structs or arrays, I use memset.) Also note that HardenedBSD uses llvm’s stack variable auto-init feature by default.

      1. 2

        Footguns galore. C is a great language in a lot of ways, but it’s not at all surprising that just about every piece of software is riddled with bugs when pretty much all of it is built on top of a foundation of C code.

        1. 1

          By the way, I normally compile all code with -Wuninitialized. But that would not have warned us here, since sin is initialized (by being pointed to &server), but server is not

          That is not the issue. The issue is that a pointer to server is passed to another function, and the compiler does not know if that function is responsible for initializing the memory.