os: use calloc() instead of malloc()

Using calloc() instead of malloc() as preventive measure, so there
never can be any hidden bugs or leaks due uninitialized memory.

The extra cost of using this compiler intrinsic should be practically
impossible to measure - in many cases a good compiler can even deduce
if certain areas really don't need to be zero'd (because they're written
to right after allocation) and create more efficient machine code.

The code pathes in question are pretty cold anyways, so it's probably
not worth even thinking about potential extra runtime costs.

Signed-off-by: Enrico Weigelt, metux IT consult <info@metux.net>
This commit is contained in:
Enrico Weigelt, metux IT consult
2025-04-10 19:55:13 +02:00
parent 0127d6ef13
commit 5f619d862d
14 changed files with 34 additions and 45 deletions

View File

@@ -217,7 +217,7 @@ typedef struct _host {
int requested;
} HOST;
#define MakeHost(h,l) (h)=malloc(sizeof *(h)+(l));\
#define MakeHost(h,l) (h)=calloc(1, sizeof *(h)+(l));\
if (h) { \
(h)->addr=(unsigned char *) ((h) + 1);\
(h)->requested = FALSE; \
@@ -590,7 +590,7 @@ DefineSelf(int fd)
ErrorF("Getting interface count: %s\n", strerror(errno));
if (len < (ifn.lifn_count * sizeof(struct lifreq))) {
len = ifn.lifn_count * sizeof(struct lifreq);
bufptr = malloc(len);
bufptr = calloc(1, len);
}
#endif
@@ -1415,7 +1415,7 @@ GetHosts(void **data, int *pnHosts, int *pLen, BOOL * pEnabled)
break;
}
if (n) {
*data = ptr = malloc(n);
*data = ptr = calloc(1, n);
if (!ptr) {
return BadAlloc;
}
@@ -1638,7 +1638,7 @@ siTypeAdd(const char *typeName, siAddrMatchFunc addrMatch,
}
}
s = malloc(sizeof(struct siType));
s = calloc(1, sizeof(struct siType));
if (s == NULL)
return BadAlloc;
@@ -1997,7 +1997,7 @@ static Bool
siLocalCredGetId(const char *addr, int len, siLocalCredPrivPtr lcPriv, int *id)
{
Bool parsedOK = FALSE;
char *addrbuf = malloc(len + 1);
char *addrbuf = calloc(1, len + 1);
if (addrbuf == NULL) {
return FALSE;