Azure / Azure/azure-c-shared-utility

tlsio_openssl_create uses malloc + per-member init; calloc would be more robust to future members

Open
#698 0 comments 0 reactions 1 assignee Claimed by @ewertons View on GitHub
enhancement good first issue
Dominant language
C
Stars
116
Forks
216
Avg merge
2d 3h
Merged PRs (30d)
11

Description

### Summary

`tlsio_openssl_create` allocates `TLS_IO_INSTANCE` with `malloc` and then initialises each member individually. This is correct today, but it is fragile: a member added later without a matching assignment would be left uninitialised, and nothing catches that at compile time.

`adapters/tlsio_openssl.c`, at `03fa57c6`:

```c
result = malloc(sizeof(TLS_IO_INSTANCE));
```
(line 1288, with the per-member assignments at lines 1293 and 1328-1363)

### Current state

Not a live defect. I checked every member of `TLS_IO_INSTANCE`: all 26 are assigned before the instance is returned, and both early-failure paths free the allocation and return `NULL` before any member is read. Nothing is read uninitialised on any path today.

### Suggested change

Use `calloc(1, sizeof(TLS_IO_INSTANCE))`, keeping the explicit assignments. This makes the zero-initialised state the default, so a member introduced later is `NULL`/`0` rather than indeterminate. The cost is one zeroing of a small struct at create time, off any hot path.

### Notes

- Purely defensive; no behaviour change and no functional bug is being reported.
- The many `= NULL` / `= false` assignments could optionally be dropped afterwards, though keeping them is also fine and makes the intent explicit.
- Same pattern is worth a look in the other TLS adapters if this is taken.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.