From d1bf9068a90bc72c3c76ce2d5d3fd40fd3dc1e24 Mon Sep 17 00:00:00 2001 From: Jakub Jelen Date: Fri, 1 Aug 2025 11:55:50 +0200 Subject: [PATCH] Use calloc instead of zeroizing structure after malloc Signed-off-by: Jakub Jelen Reviewed-by: Andreas Schneider --- src/agent.c | 3 +-- src/auth.c | 3 +-- src/pcap.c | 8 ++++---- src/pki.c | 3 +-- tests/unittests/torture_callbacks.c | 5 ++--- 5 files changed, 9 insertions(+), 13 deletions(-) diff --git a/src/agent.c b/src/agent.c index e2dc4715..0037d20e 100644 --- a/src/agent.c +++ b/src/agent.c @@ -136,11 +136,10 @@ ssh_agent ssh_agent_new(struct ssh_session_struct *session) { ssh_agent agent = NULL; - agent = malloc(sizeof(struct ssh_agent_struct)); + agent = calloc(1, sizeof(struct ssh_agent_struct)); if (agent == NULL) { return NULL; } - ZERO_STRUCTP(agent); agent->count = 0; agent->sock = ssh_socket_new(session); diff --git a/src/auth.c b/src/auth.c index 565f7b81..ef3ef1ac 100644 --- a/src/auth.c +++ b/src/auth.c @@ -1027,12 +1027,11 @@ int ssh_userauth_agent(ssh_session session, const char *username) } if (!session->agent_state) { - session->agent_state = malloc(sizeof(struct ssh_agent_state_struct)); + session->agent_state = calloc(1, sizeof(struct ssh_agent_state_struct)); if (!session->agent_state) { ssh_set_error_oom(session); return SSH_AUTH_ERROR; } - ZERO_STRUCTP(session->agent_state); session->agent_state->state = SSH_AGENT_STATE_NONE; } diff --git a/src/pcap.c b/src/pcap.c index 3d295427..1a98e1cd 100644 --- a/src/pcap.c +++ b/src/pcap.c @@ -126,11 +126,10 @@ ssh_pcap_file ssh_pcap_file_new(void) { struct ssh_pcap_file_struct *pcap = NULL; - pcap = malloc(sizeof(struct ssh_pcap_file_struct)); + pcap = calloc(1, sizeof(struct ssh_pcap_file_struct)); if (pcap == NULL) { return NULL; } - ZERO_STRUCTP(pcap); return pcap; } @@ -296,12 +295,13 @@ void ssh_pcap_file_free(ssh_pcap_file pcap) */ ssh_pcap_context ssh_pcap_context_new(ssh_session session) { - ssh_pcap_context ctx = (struct ssh_pcap_context_struct *)malloc(sizeof(struct ssh_pcap_context_struct)); + ssh_pcap_context ctx = NULL; + + ctx = calloc(1, sizeof(struct ssh_pcap_context_struct)); if (ctx == NULL) { ssh_set_error_oom(session); return NULL; } - ZERO_STRUCTP(ctx); ctx->session = session; return ctx; } diff --git a/src/pki.c b/src/pki.c index 2d90f619..e0fa4d57 100644 --- a/src/pki.c +++ b/src/pki.c @@ -833,11 +833,10 @@ ssh_signature ssh_signature_new(void) { struct ssh_signature_struct *sig = NULL; - sig = malloc(sizeof(struct ssh_signature_struct)); + sig = calloc(1, sizeof(struct ssh_signature_struct)); if (sig == NULL) { return NULL; } - ZERO_STRUCTP(sig); return sig; } diff --git a/tests/unittests/torture_callbacks.c b/tests/unittests/torture_callbacks.c index ac5b4b1b..ee8b0383 100644 --- a/tests/unittests/torture_callbacks.c +++ b/tests/unittests/torture_callbacks.c @@ -21,11 +21,10 @@ static int myauthcallback (const char *prompt, char *buf, size_t len, static int setup(void **state) { - struct ssh_callbacks_struct *cb; + struct ssh_callbacks_struct *cb = NULL; - cb = malloc(sizeof(struct ssh_callbacks_struct)); + cb = calloc(1, sizeof(struct ssh_callbacks_struct)); assert_non_null(cb); - ZERO_STRUCTP(cb); cb->userdata = (void *) 0x0badc0de; cb->auth_function = myauthcallback;