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:
parent
0127d6ef13
commit
5f619d862d
10
os/access.c
10
os/access.c
|
@ -217,7 +217,7 @@ typedef struct _host {
|
||||||
int requested;
|
int requested;
|
||||||
} HOST;
|
} HOST;
|
||||||
|
|
||||||
#define MakeHost(h,l) (h)=malloc(sizeof *(h)+(l));\
|
#define MakeHost(h,l) (h)=calloc(1, sizeof *(h)+(l));\
|
||||||
if (h) { \
|
if (h) { \
|
||||||
(h)->addr=(unsigned char *) ((h) + 1);\
|
(h)->addr=(unsigned char *) ((h) + 1);\
|
||||||
(h)->requested = FALSE; \
|
(h)->requested = FALSE; \
|
||||||
|
@ -590,7 +590,7 @@ DefineSelf(int fd)
|
||||||
ErrorF("Getting interface count: %s\n", strerror(errno));
|
ErrorF("Getting interface count: %s\n", strerror(errno));
|
||||||
if (len < (ifn.lifn_count * sizeof(struct lifreq))) {
|
if (len < (ifn.lifn_count * sizeof(struct lifreq))) {
|
||||||
len = ifn.lifn_count * sizeof(struct lifreq);
|
len = ifn.lifn_count * sizeof(struct lifreq);
|
||||||
bufptr = malloc(len);
|
bufptr = calloc(1, len);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
@ -1415,7 +1415,7 @@ GetHosts(void **data, int *pnHosts, int *pLen, BOOL * pEnabled)
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (n) {
|
if (n) {
|
||||||
*data = ptr = malloc(n);
|
*data = ptr = calloc(1, n);
|
||||||
if (!ptr) {
|
if (!ptr) {
|
||||||
return BadAlloc;
|
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)
|
if (s == NULL)
|
||||||
return BadAlloc;
|
return BadAlloc;
|
||||||
|
|
||||||
|
@ -1997,7 +1997,7 @@ static Bool
|
||||||
siLocalCredGetId(const char *addr, int len, siLocalCredPrivPtr lcPriv, int *id)
|
siLocalCredGetId(const char *addr, int len, siLocalCredPrivPtr lcPriv, int *id)
|
||||||
{
|
{
|
||||||
Bool parsedOK = FALSE;
|
Bool parsedOK = FALSE;
|
||||||
char *addrbuf = malloc(len + 1);
|
char *addrbuf = calloc(1, len + 1);
|
||||||
|
|
||||||
if (addrbuf == NULL) {
|
if (addrbuf == NULL) {
|
||||||
return FALSE;
|
return FALSE;
|
||||||
|
|
|
@ -12,7 +12,7 @@
|
||||||
void *
|
void *
|
||||||
XNFalloc(unsigned long amount)
|
XNFalloc(unsigned long amount)
|
||||||
{
|
{
|
||||||
void *ptr = malloc(amount);
|
void *ptr = calloc(1, amount);
|
||||||
|
|
||||||
if (!ptr)
|
if (!ptr)
|
||||||
FatalError("Out of memory");
|
FatalError("Out of memory");
|
||||||
|
|
|
@ -181,7 +181,7 @@ DetermineClientCmd(pid_t pid, const char **cmdname, const char **cmdargs)
|
||||||
size_t len = argmax;
|
size_t len = argmax;
|
||||||
int32_t argc = -1;
|
int32_t argc = -1;
|
||||||
|
|
||||||
char * const procargs = malloc(len);
|
char * const procargs = calloc(1, len);
|
||||||
if (!procargs) {
|
if (!procargs) {
|
||||||
ErrorF("Failed to allocate memory (%lu bytes) for KERN_PROCARGS2 result for pid %d: %s\n", len, pid, strerror(errno));
|
ErrorF("Failed to allocate memory (%lu bytes) for KERN_PROCARGS2 result for pid %d: %s\n", len, pid, strerror(errno));
|
||||||
return;
|
return;
|
||||||
|
@ -278,7 +278,7 @@ DetermineClientCmd(pid_t pid, const char **cmdname, const char **cmdargs)
|
||||||
|
|
||||||
/* Read KERN_PROC_ARGS contents. Similar to /proc/pid/cmdline
|
/* Read KERN_PROC_ARGS contents. Similar to /proc/pid/cmdline
|
||||||
* the process name and each argument are separated by NUL byte. */
|
* the process name and each argument are separated by NUL byte. */
|
||||||
char *const procargs = malloc(len);
|
char *const procargs = calloc(1, len);
|
||||||
if (sysctl(mib, ARRAY_SIZE(mib), procargs, &len, NULL, 0) != 0) {
|
if (sysctl(mib, ARRAY_SIZE(mib), procargs, &len, NULL, 0) != 0) {
|
||||||
ErrorF("Failed to get KERN_PROC_ARGS for PID %d: %s\n", pid, strerror(errno));
|
ErrorF("Failed to get KERN_PROC_ARGS for PID %d: %s\n", pid, strerror(errno));
|
||||||
free(procargs);
|
free(procargs);
|
||||||
|
@ -383,7 +383,7 @@ DetermineClientCmd(pid_t pid, const char **cmdname, const char **cmdargs)
|
||||||
char *args = NULL;
|
char *args = NULL;
|
||||||
|
|
||||||
if (argsize > 0)
|
if (argsize > 0)
|
||||||
args = malloc(argsize);
|
args = calloc(1, argsize);
|
||||||
if (args) {
|
if (args) {
|
||||||
int i = 0;
|
int i = 0;
|
||||||
|
|
||||||
|
|
|
@ -614,10 +614,9 @@ ClientReady(int fd, int xevents, void *data)
|
||||||
static ClientPtr
|
static ClientPtr
|
||||||
AllocNewConnection(XtransConnInfo trans_conn, int fd, CARD32 conn_time)
|
AllocNewConnection(XtransConnInfo trans_conn, int fd, CARD32 conn_time)
|
||||||
{
|
{
|
||||||
OsCommPtr oc;
|
|
||||||
ClientPtr client;
|
ClientPtr client;
|
||||||
|
|
||||||
oc = malloc(sizeof(OsCommRec));
|
OsCommPtr oc = calloc(1, sizeof(OsCommRec));
|
||||||
if (!oc)
|
if (!oc)
|
||||||
return NullClient;
|
return NullClient;
|
||||||
oc->trans_conn = trans_conn;
|
oc->trans_conn = trans_conn;
|
||||||
|
|
|
@ -409,7 +409,7 @@ InputThreadPreInit(void)
|
||||||
if (pipe(hotplugPipe) < 0)
|
if (pipe(hotplugPipe) < 0)
|
||||||
FatalError("input-thread: could not create pipe");
|
FatalError("input-thread: could not create pipe");
|
||||||
|
|
||||||
inputThreadInfo = malloc(sizeof(InputThreadInfo));
|
inputThreadInfo = calloc(1, sizeof(InputThreadInfo));
|
||||||
if (!inputThreadInfo)
|
if (!inputThreadInfo)
|
||||||
FatalError("input-thread: could not allocate memory");
|
FatalError("input-thread: could not allocate memory");
|
||||||
|
|
||||||
|
|
10
os/io.c
10
os/io.c
|
@ -972,12 +972,10 @@ FlushClient(ClientPtr who, OsCommPtr oc, const void *__extraBuf, int extraCount)
|
||||||
static ConnectionInputPtr
|
static ConnectionInputPtr
|
||||||
AllocateInputBuffer(void)
|
AllocateInputBuffer(void)
|
||||||
{
|
{
|
||||||
ConnectionInputPtr oci;
|
ConnectionInputPtr oci = calloc(1, sizeof(ConnectionInput));
|
||||||
|
|
||||||
oci = malloc(sizeof(ConnectionInput));
|
|
||||||
if (!oci)
|
if (!oci)
|
||||||
return NULL;
|
return NULL;
|
||||||
oci->buffer = malloc(BUFSIZE);
|
oci->buffer = calloc(1, BUFSIZE);
|
||||||
if (!oci->buffer) {
|
if (!oci->buffer) {
|
||||||
free(oci);
|
free(oci);
|
||||||
return NULL;
|
return NULL;
|
||||||
|
@ -993,9 +991,7 @@ AllocateInputBuffer(void)
|
||||||
static ConnectionOutputPtr
|
static ConnectionOutputPtr
|
||||||
AllocateOutputBuffer(void)
|
AllocateOutputBuffer(void)
|
||||||
{
|
{
|
||||||
ConnectionOutputPtr oco;
|
ConnectionOutputPtr oco = calloc(1, sizeof(ConnectionOutput));
|
||||||
|
|
||||||
oco = malloc(sizeof(ConnectionOutput));
|
|
||||||
if (!oco)
|
if (!oco)
|
||||||
return NULL;
|
return NULL;
|
||||||
oco->buf = calloc(1, BUFSIZE);
|
oco->buf = calloc(1, BUFSIZE);
|
||||||
|
|
5
os/log.c
5
os/log.c
|
@ -82,7 +82,7 @@ OR PERFORMANCE OF THIS SOFTWARE.
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
#include <stdlib.h> /* for malloc() */
|
#include <stdlib.h> /* for calloc() */
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
#include <X11/Xfuncproto.h>
|
#include <X11/Xfuncproto.h>
|
||||||
|
@ -773,7 +773,6 @@ AuditPrefix(void)
|
||||||
{
|
{
|
||||||
time_t tm;
|
time_t tm;
|
||||||
char *autime, *s;
|
char *autime, *s;
|
||||||
char *tmpBuf;
|
|
||||||
int len;
|
int len;
|
||||||
|
|
||||||
time(&tm);
|
time(&tm);
|
||||||
|
@ -781,7 +780,7 @@ AuditPrefix(void)
|
||||||
if ((s = strchr(autime, '\n')))
|
if ((s = strchr(autime, '\n')))
|
||||||
*s = '\0';
|
*s = '\0';
|
||||||
len = strlen(AUDIT_PREFIX) + strlen(autime) + 10 + 1;
|
len = strlen(AUDIT_PREFIX) + strlen(autime) + 10 + 1;
|
||||||
tmpBuf = malloc(len);
|
char *tmpBuf = calloc(1, len);
|
||||||
if (!tmpBuf)
|
if (!tmpBuf)
|
||||||
return NULL;
|
return NULL;
|
||||||
snprintf(tmpBuf, len, AUDIT_PREFIX, autime, (unsigned long) getpid());
|
snprintf(tmpBuf, len, AUDIT_PREFIX, autime, (unsigned long) getpid());
|
||||||
|
|
|
@ -49,12 +49,10 @@ static struct auth {
|
||||||
int
|
int
|
||||||
MitAddCookie(unsigned short data_length, const char *data, XID id)
|
MitAddCookie(unsigned short data_length, const char *data, XID id)
|
||||||
{
|
{
|
||||||
struct auth *new;
|
struct auth *new = calloc(1, sizeof(struct auth));
|
||||||
|
|
||||||
new = malloc(sizeof(struct auth));
|
|
||||||
if (!new)
|
if (!new)
|
||||||
return 0;
|
return 0;
|
||||||
new->data = malloc((unsigned) data_length);
|
new->data = calloc(1, (unsigned) data_length);
|
||||||
if (!new->data) {
|
if (!new->data) {
|
||||||
free(new);
|
free(new);
|
||||||
return 0;
|
return 0;
|
||||||
|
|
|
@ -43,7 +43,7 @@ strndup(const char *str, size_t n)
|
||||||
for (len = 0; len < n && str[len]; len++)
|
for (len = 0; len < n && str[len]; len++)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
if ((copy = malloc(len + 1)) == NULL)
|
if ((copy = calloc(1, len + 1)) == NULL)
|
||||||
return (NULL);
|
return (NULL);
|
||||||
memcpy(copy, str, len);
|
memcpy(copy, str, len);
|
||||||
copy[len] = '\0';
|
copy[len] = '\0';
|
||||||
|
|
|
@ -96,7 +96,7 @@ __stdcall unsigned long GetTickCount(void);
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
#include <ctype.h> /* for isspace */
|
#include <ctype.h> /* for isspace */
|
||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
#include <stdlib.h> /* for malloc() */
|
#include <stdlib.h> /* for calloc() */
|
||||||
|
|
||||||
#if defined(TCPCONN)
|
#if defined(TCPCONN)
|
||||||
#ifndef WIN32
|
#ifndef WIN32
|
||||||
|
@ -1030,7 +1030,7 @@ Popen(const char *command, const char *type)
|
||||||
if ((*type != 'r' && *type != 'w') || type[1])
|
if ((*type != 'r' && *type != 'w') || type[1])
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
if ((cur = malloc(sizeof(struct pid))) == NULL)
|
if ((cur = calloc(1, sizeof(struct pid))) == NULL)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
if (pipe(pdes) < 0) {
|
if (pipe(pdes) < 0) {
|
||||||
|
|
|
@ -256,7 +256,7 @@ XdmAuthorizationValidate(unsigned char *plain, int length,
|
||||||
*reason = "Bad XDM authorization key length";
|
*reason = "Bad XDM authorization key length";
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
client = malloc(sizeof(XdmClientAuthRec));
|
client = calloc(1, sizeof(XdmClientAuthRec));
|
||||||
if (!client)
|
if (!client)
|
||||||
return NULL;
|
return NULL;
|
||||||
XdmClientAuthDecode(plain, client);
|
XdmClientAuthDecode(plain, client);
|
||||||
|
@ -322,7 +322,6 @@ XdmAuthorizationValidate(unsigned char *plain, int length,
|
||||||
int
|
int
|
||||||
XdmAddCookie(unsigned short data_length, const char *data, XID id)
|
XdmAddCookie(unsigned short data_length, const char *data, XID id)
|
||||||
{
|
{
|
||||||
XdmAuthorizationPtr new;
|
|
||||||
unsigned char *rho_bits, *key_bits;
|
unsigned char *rho_bits, *key_bits;
|
||||||
|
|
||||||
switch (data_length) {
|
switch (data_length) {
|
||||||
|
@ -354,7 +353,7 @@ XdmAddCookie(unsigned short data_length, const char *data, XID id)
|
||||||
/* the first octet of the key must be zero */
|
/* the first octet of the key must be zero */
|
||||||
if (key_bits[0] != '\0')
|
if (key_bits[0] != '\0')
|
||||||
return 0;
|
return 0;
|
||||||
new = malloc(sizeof(XdmAuthorizationRec));
|
XdmAuthorizationPtr new = calloc(1, sizeof(XdmAuthorizationRec));
|
||||||
if (!new)
|
if (!new)
|
||||||
return 0;
|
return 0;
|
||||||
new->next = xdmAuth;
|
new->next = xdmAuth;
|
||||||
|
@ -371,12 +370,11 @@ XdmCheckCookie(unsigned short cookie_length, const char *cookie,
|
||||||
{
|
{
|
||||||
XdmAuthorizationPtr auth;
|
XdmAuthorizationPtr auth;
|
||||||
XdmClientAuthPtr client;
|
XdmClientAuthPtr client;
|
||||||
unsigned char *plain;
|
|
||||||
|
|
||||||
/* Auth packets must be a multiple of 8 bytes long */
|
/* Auth packets must be a multiple of 8 bytes long */
|
||||||
if (cookie_length & 7)
|
if (cookie_length & 7)
|
||||||
return (XID) -1;
|
return (XID) -1;
|
||||||
plain = malloc(cookie_length);
|
unsigned char *plain = calloc(1, cookie_length);
|
||||||
if (!plain)
|
if (!plain)
|
||||||
return (XID) -1;
|
return (XID) -1;
|
||||||
for (auth = xdmAuth; auth; auth = auth->next) {
|
for (auth = xdmAuth; auth; auth = auth->next) {
|
||||||
|
|
|
@ -398,7 +398,7 @@ XdmcpRegisterAuthentication(const char *name,
|
||||||
XdmcpReallocARRAYofARRAY8(&AuthenticationDatas,
|
XdmcpReallocARRAYofARRAY8(&AuthenticationDatas,
|
||||||
AuthenticationDatas.length + 1) &&
|
AuthenticationDatas.length + 1) &&
|
||||||
(newFuncs =
|
(newFuncs =
|
||||||
malloc((AuthenticationNames.length +
|
calloc(1, (AuthenticationNames.length +
|
||||||
1) * sizeof(AuthenticationFuncsRec))))) {
|
1) * sizeof(AuthenticationFuncsRec))))) {
|
||||||
XdmcpDisposeARRAY8(&AuthenticationName);
|
XdmcpDisposeARRAY8(&AuthenticationName);
|
||||||
XdmcpDisposeARRAY8(&AuthenticationData);
|
XdmcpDisposeARRAY8(&AuthenticationData);
|
||||||
|
@ -502,7 +502,7 @@ XdmcpRegisterConnection(int type, const char *address, int addrlen)
|
||||||
}
|
}
|
||||||
if (ConnectionAddresses.length + 1 == 256)
|
if (ConnectionAddresses.length + 1 == 256)
|
||||||
return;
|
return;
|
||||||
newAddress = malloc(addrlen * sizeof(CARD8));
|
newAddress = calloc(addrlen, sizeof(CARD8));
|
||||||
if (!newAddress)
|
if (!newAddress)
|
||||||
return;
|
return;
|
||||||
if (!XdmcpReallocARRAY16(&ConnectionTypes, ConnectionTypes.length + 1)) {
|
if (!XdmcpReallocARRAY16(&ConnectionTypes, ConnectionTypes.length + 1)) {
|
||||||
|
@ -541,7 +541,7 @@ XdmcpRegisterAuthorization(const char *name, int namelen)
|
||||||
ARRAY8 authName;
|
ARRAY8 authName;
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
authName.data = malloc(namelen * sizeof(CARD8));
|
authName.data = calloc(namelen, sizeof(CARD8));
|
||||||
if (!authName.data)
|
if (!authName.data)
|
||||||
return;
|
return;
|
||||||
if (!XdmcpReallocARRAYofARRAY8
|
if (!XdmcpReallocARRAYofARRAY8
|
||||||
|
|
|
@ -103,7 +103,7 @@ Xvasprintf(char **ret, const char *_X_RESTRICT_KYWD format, va_list va)
|
||||||
size = vsnprintf(NULL, 0, format, va2);
|
size = vsnprintf(NULL, 0, format, va2);
|
||||||
va_end(va2);
|
va_end(va2);
|
||||||
|
|
||||||
*ret = malloc(size + 1);
|
*ret = calloc(1, size + 1);
|
||||||
if (*ret == NULL)
|
if (*ret == NULL)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
|
|
13
os/xsha1.c
13
os/xsha1.c
|
@ -45,8 +45,7 @@
|
||||||
void *
|
void *
|
||||||
x_sha1_init(void)
|
x_sha1_init(void)
|
||||||
{
|
{
|
||||||
SHA1_CTX *ctx = malloc(sizeof(*ctx));
|
SHA1_CTX *ctx = calloc(1, sizeof(SHA1_CTX));
|
||||||
|
|
||||||
if (!ctx)
|
if (!ctx)
|
||||||
return NULL;
|
return NULL;
|
||||||
SHA1Init(ctx);
|
SHA1Init(ctx);
|
||||||
|
@ -79,7 +78,7 @@ x_sha1_final(void *ctx, unsigned char result[20])
|
||||||
void *
|
void *
|
||||||
x_sha1_init(void)
|
x_sha1_init(void)
|
||||||
{
|
{
|
||||||
CC_SHA1_CTX *ctx = malloc(sizeof(*ctx));
|
CC_SHA1_CTX *ctx = calloc(1, sizeof(CC_SHA1_CTX));
|
||||||
|
|
||||||
if (!ctx)
|
if (!ctx)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
@ -117,7 +116,7 @@ static HCRYPTPROV hProv;
|
||||||
void *
|
void *
|
||||||
x_sha1_init(void)
|
x_sha1_init(void)
|
||||||
{
|
{
|
||||||
HCRYPTHASH *ctx = malloc(sizeof(*ctx));
|
HCRYPTHASH *ctx = calloc(1, sizeof(HCRYPTHASH));
|
||||||
|
|
||||||
if (!ctx)
|
if (!ctx)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
@ -155,7 +154,7 @@ x_sha1_final(void *ctx, unsigned char result[20])
|
||||||
void *
|
void *
|
||||||
x_sha1_init(void)
|
x_sha1_init(void)
|
||||||
{
|
{
|
||||||
struct sha1_ctx *ctx = malloc(sizeof(*ctx));
|
struct sha1_ctx *ctx = calloc(1, sizeof(struct sha1_ctx ));
|
||||||
|
|
||||||
if (!ctx)
|
if (!ctx)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
@ -229,7 +228,7 @@ x_sha1_final(void *ctx, unsigned char result[20])
|
||||||
void *
|
void *
|
||||||
x_sha1_init(void)
|
x_sha1_init(void)
|
||||||
{
|
{
|
||||||
sha1_ctx *ctx = malloc(sizeof(*ctx));
|
sha1_ctx *ctx = calloc(1, sizeof(sha1_ctx));
|
||||||
|
|
||||||
if (!ctx)
|
if (!ctx)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
@ -261,7 +260,7 @@ void *
|
||||||
x_sha1_init(void)
|
x_sha1_init(void)
|
||||||
{
|
{
|
||||||
int ret;
|
int ret;
|
||||||
SHA_CTX *ctx = malloc(sizeof(*ctx));
|
SHA_CTX *ctx = calloc(1, sizeof(SHA_CTX));
|
||||||
|
|
||||||
if (!ctx)
|
if (!ctx)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
Loading…
Reference in New Issue