|
| 1 | +# Conditionally uninitialized variable |
| 2 | +A common pattern is to initialize a local variable by calling another function (an "initialization" function) with the address of the local variable as a pointer argument. That function is then responsible for writing to the memory location referenced by the pointer. |
| 3 | + |
| 4 | +In some cases, the called function may not always write to the memory pointed to by the pointer argument. In such cases, the function will typically return a "status" code, informing the caller as to whether the initialization succeeded or not. If the caller does not check the status code before reading the local variable, it may read uninitialized memory, which can result in unexpected behavior. |
| 5 | + |
| 6 | + |
| 7 | +## Recommendation |
| 8 | +When using an initialization function that does not guarantee to initialize the memory pointed to by the passed pointer, and returns a status code to indicate whether such initialization occurred, the status code should be checked before reading from the local variable. |
| 9 | + |
| 10 | + |
| 11 | +## Example |
| 12 | +In this hypothetical example we have code for managing a series of devices. The code includes a `DeviceConfig` struct that can represent properties about each device. The `initDeviceConfig` function can be called to initialize one of these structures, by providing a "device number", which can be used to look up the appropriate properties in some data store. If an invalid device number is provided, the function returns a status code of `-1`, and does not initialize the provided pointer. |
| 13 | + |
| 14 | +In the first code sample below, the `notify` function calls the `initDeviceConfig` function with a pointer to the local variable `config`, which is then subsequently accessed to fetch properties of the device. However, the code does not check the return value from the function call to `initDeviceConfig`. If the device number passed to the `notify` function was invalid, the `initDeviceConfig` function will leave the `config` variable uninitialized, which will result in the `notify` function accessing uninitialized memory. |
| 15 | + |
| 16 | + |
| 17 | +```c |
| 18 | +struct DeviceConfig { |
| 19 | + bool isEnabled; |
| 20 | + int channel; |
| 21 | +}; |
| 22 | + |
| 23 | +int initDeviceConfig(DeviceConfig *ref, int deviceNumber) { |
| 24 | + if (deviceNumber >= getMaxDevices()) { |
| 25 | + // No device with that number, return -1 to indicate failure |
| 26 | + return -1; |
| 27 | + } |
| 28 | + // Device with that number, fetch parameters and initialize struct |
| 29 | + ref->isEnabled = fetchIsDeviceEnabled(deviceNumber); |
| 30 | + ref->channel = fetchDeviceChannel(deviceNumber); |
| 31 | + // Return 0 to indicate success |
| 32 | + return 0; |
| 33 | +} |
| 34 | + |
| 35 | +int notify(int deviceNumber) { |
| 36 | + DeviceConfig config; |
| 37 | + initDeviceConfig(&config, deviceNumber); |
| 38 | + // BAD: Using config without checking the status code that is returned |
| 39 | + if (config.isEnabled) { |
| 40 | + notifyChannel(config.channel); |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +``` |
| 45 | +To fix this, the code needs to check that the return value of the call to `initDeviceConfig` is zero. If that is true, then the calling code can safely assume that the local variable has been initialized. |
| 46 | +
|
| 47 | +
|
| 48 | +```c |
| 49 | +struct DeviceConfig { |
| 50 | + bool isEnabled; |
| 51 | + int channel; |
| 52 | +}; |
| 53 | +
|
| 54 | +int initDeviceConfig(DeviceConfig *ref, int deviceNumber) { |
| 55 | + if (deviceNumber >= getMaxDevices()) { |
| 56 | + // No device with that number, return -1 to indicate failure |
| 57 | + return -1; |
| 58 | + } |
| 59 | + // Device with that number, fetch parameters and initialize struct |
| 60 | + ref->isEnabled = fetchIsDeviceEnabled(deviceNumber); |
| 61 | + ref->channel = fetchDeviceChannel(deviceNumber); |
| 62 | + // Return 0 to indicate success |
| 63 | + return 0; |
| 64 | +} |
| 65 | +
|
| 66 | +void notify(int deviceNumber) { |
| 67 | + DeviceConfig config; |
| 68 | + int statusCode = initDeviceConfig(&config, deviceNumber); |
| 69 | + if (statusCode == 0) { |
| 70 | + // GOOD: Status code returned by initialization function is checked, so this is safe |
| 71 | + if (config.isEnabled) { |
| 72 | + notifyChannel(config.channel); |
| 73 | + } |
| 74 | + } |
| 75 | +} |
| 76 | +
|
| 77 | +``` |
| 78 | + |
| 79 | +## References |
| 80 | +* Wikipedia: [Uninitialized variable](https://en.wikipedia.org/wiki/Uninitialized_variable). |
0 commit comments