# Are uninitialized static kernel variables set to NULL / 0?



## Avernar (Sep 11, 2016)

I've tracked down a kernel bug and want to fix it right away in my own kernel.  Basically a function is being called twice and it is resetting a pair of global variables, a pointer and a size, to NULL and 0 respectively.

It's the ipfw_init_obj_rewriter function in ip_fw_sockopt.c which is getting called a second time when I launch a vnet jail.  This causes the ipfw list command to segfault as the kernel stops returning table names.  It also causes a small memory leak and I don't know what other issues.

If those variables are initialized to NULL and 0 automatically then I can just remove the two lines initializing them later.  They look like they are but I want to make sure.

This is in 11.0-RC2


----------



## Avernar (Sep 11, 2016)

Fixed the bug by changing where the functions are called in ip_fw2.c but I'd still like to know the answer to my question.


----------



## tobik@ (Sep 11, 2016)

They should be initialized with NULL or 0, see the C99 standard, section 6.7.8:


> If an object that has static 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;
> — if it is a union, the first named member is initialized (recursively) according to these rules


with static storage duration meaning (6.7.4):


> An object whose identifier is declared with external or internal linkage, or with the
> storage-class specifier *static* has static storage duration. Its lifetime is the entire
> execution of the program and its stored value is initialized only once, prior to program
> startup.


----------



## Avernar (Sep 11, 2016)

I know the C standard.  The C run time library does that initialization.

However, I've encountered many embeded systems that have a very small C RTL that do not do this.  And operating system kernels do not use the compiler's C RTL.  The have their own custom start up and library routines.

So my question rephrased is: Does the Freebsd kernel's startup code do the C99 section 6.7.8 initialization?


----------



## Juha Nurmela (Sep 11, 2016)

Vaaaguely related sidenote, you can do stuff like

```
#include <sys/cdefs.h>

int main __section(".bss");
```
 to force variables in strange places.

Juha

It does, don't worry


----------

