From 031566f9ccbb351616420572c67d65e936532fdb Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Mon, 22 Dec 2014 15:59:21 +0100 Subject: [PATCH] calloc: introduce LIBSSH2_CALLOC() A simple function using LIBSSH2_ALLOC + memset, since this pattern was used in multiple places and this simplies code in general. --- src/agent.c | 5 ++--- src/channel.c | 7 ++----- src/comp.c | 5 ++--- src/hostkey.c | 5 ++--- src/knownhost.c | 6 ++---- src/libssh2_priv.h | 3 ++- src/misc.c | 11 ++++++++++- src/misc.h | 3 ++- src/packet.c | 9 +++------ src/publickey.c | 3 +-- src/sftp.c | 8 +++----- src/userauth.c | 24 ++++++++---------------- 12 files changed, 39 insertions(+), 50 deletions(-) diff --git a/src/agent.c b/src/agent.c index c203310..785e3c9 100644 --- a/src/agent.c +++ b/src/agent.c @@ -1,6 +1,6 @@ /* * Copyright (c) 2009 by Daiki Ueno - * Copyright (C) 2010 by Daniel Stenberg + * Copyright (C) 2014 by Daniel Stenberg * All rights reserved. * * Redistribution and use in source and binary forms, @@ -660,13 +660,12 @@ libssh2_agent_init(LIBSSH2_SESSION *session) { LIBSSH2_AGENT *agent; - agent = LIBSSH2_ALLOC(session, sizeof *agent); + agent = LIBSSH2_CALLOC(session, sizeof *agent); if (!agent) { _libssh2_error(session, LIBSSH2_ERROR_ALLOC, "Unable to allocate space for agent connection"); return NULL; } - memset(agent, 0, sizeof *agent); agent->fd = LIBSSH2_INVALID_SOCKET; agent->session = session; _libssh2_list_init(&agent->head); diff --git a/src/channel.c b/src/channel.c index 58cee5f..d9dc072 100644 --- a/src/channel.c +++ b/src/channel.c @@ -158,14 +158,12 @@ _libssh2_channel_open(LIBSSH2_SESSION * session, const char *channel_type, "Opening Channel - win %d pack %d", window_size, packet_size); session->open_channel = - LIBSSH2_ALLOC(session, sizeof(LIBSSH2_CHANNEL)); + LIBSSH2_CALLOC(session, sizeof(LIBSSH2_CHANNEL)); if (!session->open_channel) { _libssh2_error(session, LIBSSH2_ERROR_ALLOC, "Unable to allocate space for channel data"); return NULL; } - memset(session->open_channel, 0, sizeof(LIBSSH2_CHANNEL)); - session->open_channel->channel_type_len = channel_type_len; session->open_channel->channel_type = LIBSSH2_ALLOC(session, channel_type_len); @@ -509,12 +507,11 @@ channel_forward_listen(LIBSSH2_SESSION * session, const char *host, if (data[0] == SSH_MSG_REQUEST_SUCCESS) { LIBSSH2_LISTENER *listener; - listener = LIBSSH2_ALLOC(session, sizeof(LIBSSH2_LISTENER)); + listener = LIBSSH2_CALLOC(session, sizeof(LIBSSH2_LISTENER)); if (!listener) _libssh2_error(session, LIBSSH2_ERROR_ALLOC, "Unable to allocate memory for listener queue"); else { - memset(listener, 0, sizeof(LIBSSH2_LISTENER)); listener->host = LIBSSH2_ALLOC(session, session->fwdLstn_host_len + 1); if (!listener->host) { diff --git a/src/comp.c b/src/comp.c index eeb89b4..4560188 100644 --- a/src/comp.c +++ b/src/comp.c @@ -1,5 +1,5 @@ /* Copyright (c) 2004-2007, Sara Golemon - * Copyright (c) 2010, Daniel Stenberg + * Copyright (c) 2010-2014, Daniel Stenberg * All rights reserved. * * Redistribution and use in source and binary forms, @@ -141,13 +141,12 @@ comp_method_zlib_init(LIBSSH2_SESSION * session, int compr, z_stream *strm; int status; - strm = LIBSSH2_ALLOC(session, sizeof(z_stream)); + strm = LIBSSH2_CALLOC(session, sizeof(z_stream)); if (!strm) { return _libssh2_error(session, LIBSSH2_ERROR_ALLOC, "Unable to allocate memory for " "zlib compression/decompression"); } - memset(strm, 0, sizeof(z_stream)); strm->opaque = (voidpf) session; strm->zalloc = (alloc_func) comp_method_zlib_alloc; diff --git a/src/hostkey.c b/src/hostkey.c index 753563d..0bbe152 100644 --- a/src/hostkey.c +++ b/src/hostkey.c @@ -1,5 +1,5 @@ /* Copyright (c) 2004-2006, Sara Golemon - * Copyright (c) 2009 by Daniel Stenberg + * Copyright (c) 2009-2014 by Daniel Stenberg * All rights reserved. * * Redistribution and use in source and binary forms, @@ -347,13 +347,12 @@ hostkey_method_ssh_dss_signv(LIBSSH2_SESSION * session, libssh2_sha1_ctx ctx; int i; - *signature = LIBSSH2_ALLOC(session, 2 * SHA_DIGEST_LENGTH); + *signature = LIBSSH2_CALLOC(session, 2 * SHA_DIGEST_LENGTH); if (!*signature) { return -1; } *signature_len = 2 * SHA_DIGEST_LENGTH; - memset(*signature, 0, 2 * SHA_DIGEST_LENGTH); libssh2_sha1_init(&ctx); for(i = 0; i < veccount; i++) { diff --git a/src/knownhost.c b/src/knownhost.c index 84b56dd..a44dcce 100644 --- a/src/knownhost.c +++ b/src/knownhost.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2009-2013 by Daniel Stenberg + * Copyright (c) 2009-2014 by Daniel Stenberg * All rights reserved. * * Redistribution and use in source and binary forms, @@ -149,13 +149,11 @@ knownhost_add(LIBSSH2_KNOWNHOSTS *hosts, return _libssh2_error(hosts->session, LIBSSH2_ERROR_INVAL, "No key type set"); - if(!(entry = LIBSSH2_ALLOC(hosts->session, sizeof(struct known_host)))) + if(!(entry = LIBSSH2_CALLOC(hosts->session, sizeof(struct known_host)))) return _libssh2_error(hosts->session, LIBSSH2_ERROR_ALLOC, "Unable to allocate memory for known host " "entry"); - memset(entry, 0, sizeof(struct known_host)); - entry->typemask = typemask; switch(entry->typemask & LIBSSH2_KNOWNHOST_TYPE_MASK) { diff --git a/src/libssh2_priv.h b/src/libssh2_priv.h index 0a46980..10d7eb0 100644 --- a/src/libssh2_priv.h +++ b/src/libssh2_priv.h @@ -1,5 +1,5 @@ /* Copyright (c) 2004-2008, 2010, Sara Golemon - * Copyright (c) 2009-2011 by Daniel Stenberg + * Copyright (c) 2009-2014 by Daniel Stenberg * Copyright (c) 2010 Simon Josefsson * All rights reserved. * @@ -152,6 +152,7 @@ static inline int writev(int sock, struct iovec *iov, int nvecs) #define LIBSSH2_ALLOC(session, count) \ session->alloc((count), &(session)->abstract) +#define LIBSSH2_CALLOC(session, count) _libssh2_calloc(session, count) #define LIBSSH2_REALLOC(session, ptr, count) \ ((ptr) ? session->realloc((ptr), (count), &(session)->abstract) : \ session->alloc((count), &(session)->abstract)) diff --git a/src/misc.c b/src/misc.c index 4d89876..725f192 100644 --- a/src/misc.c +++ b/src/misc.c @@ -1,5 +1,5 @@ /* Copyright (c) 2004-2007 Sara Golemon - * Copyright (c) 2009-2010 by Daniel Stenberg + * Copyright (c) 2009-2014 by Daniel Stenberg * Copyright (c) 2010 Simon Josefsson * All rights reserved. * @@ -619,3 +619,12 @@ int __cdecl _libssh2_gettimeofday(struct timeval *tp, void *tzp) #endif + +void *_libssh2_calloc(LIBSSH2_SESSION* session, size_t size) +{ + void *p = LIBSSH2_ALLOC(session, size); + if(p) { + memset(p, 0, size); + } + return p; +} diff --git a/src/misc.h b/src/misc.h index e25248d..f99b773 100644 --- a/src/misc.h +++ b/src/misc.h @@ -1,6 +1,6 @@ #ifndef __LIBSSH2_MISC_H #define __LIBSSH2_MISC_H -/* Copyright (c) 2009-2011 by Daniel Stenberg +/* Copyright (c) 2009-2014 by Daniel Stenberg * * All rights reserved. * @@ -77,6 +77,7 @@ libssh2_uint64_t _libssh2_ntohu64(const unsigned char *buf); void _libssh2_htonu32(unsigned char *buf, uint32_t val); void _libssh2_store_u32(unsigned char **buf, uint32_t value); void _libssh2_store_str(unsigned char **buf, const char *str, size_t len); +void *_libssh2_calloc(LIBSSH2_SESSION* session, size_t size); #if defined(LIBSSH2_WIN32) && !defined(__MINGW32__) && !defined(__CYGWIN__) /* provide a private one */ diff --git a/src/packet.c b/src/packet.c index 789c803..74e77df 100644 --- a/src/packet.c +++ b/src/packet.c @@ -1,6 +1,6 @@ /* Copyright (c) 2004-2007, Sara Golemon * Copyright (c) 2005,2006 Mikhail Gusarov - * Copyright (c) 2009-2013 by Daniel Stenberg + * Copyright (c) 2009-2014 by Daniel Stenberg * Copyright (c) 2010 Simon Josefsson * All rights reserved. * @@ -139,7 +139,7 @@ packet_queue_listener(LIBSSH2_SESSION * session, unsigned char *data, break; } - channel = LIBSSH2_ALLOC(session, sizeof(LIBSSH2_CHANNEL)); + channel = LIBSSH2_CALLOC(session, sizeof(LIBSSH2_CHANNEL)); if (!channel) { _libssh2_error(session, LIBSSH2_ERROR_ALLOC, "Unable to allocate a channel for " @@ -150,8 +150,6 @@ packet_queue_listener(LIBSSH2_SESSION * session, unsigned char *data, } listen_state->channel = channel; - memset(channel, 0, sizeof(LIBSSH2_CHANNEL)); - channel->session = session; channel->channel_type_len = sizeof("forwarded-tcpip") - 1; channel->channel_type = LIBSSH2_ALLOC(session, @@ -299,14 +297,13 @@ packet_x11_open(LIBSSH2_SESSION * session, unsigned char *data, if (session->x11) { if (x11open_state->state == libssh2_NB_state_allocated) { - channel = LIBSSH2_ALLOC(session, sizeof(LIBSSH2_CHANNEL)); + channel = LIBSSH2_CALLOC(session, sizeof(LIBSSH2_CHANNEL)); if (!channel) { _libssh2_error(session, LIBSSH2_ERROR_ALLOC, "allocate a channel for new connection"); failure_code = SSH_OPEN_RESOURCE_SHORTAGE; goto x11_exit; } - memset(channel, 0, sizeof(LIBSSH2_CHANNEL)); channel->session = session; channel->channel_type_len = sizeof("x11") - 1; diff --git a/src/publickey.c b/src/publickey.c index 4145bef..bfee0a8 100644 --- a/src/publickey.c +++ b/src/publickey.c @@ -350,13 +350,12 @@ static LIBSSH2_PUBLICKEY *publickey_init(LIBSSH2_SESSION *session) } session->pkeyInit_pkey = - LIBSSH2_ALLOC(session, sizeof(LIBSSH2_PUBLICKEY)); + LIBSSH2_CALLOC(session, sizeof(LIBSSH2_PUBLICKEY)); if (!session->pkeyInit_pkey) { _libssh2_error(session, LIBSSH2_ERROR_ALLOC, "Unable to allocate a new publickey structure"); goto err_exit; } - memset(session->pkeyInit_pkey, 0, sizeof(LIBSSH2_PUBLICKEY)); session->pkeyInit_pkey->channel = session->pkeyInit_channel; session->pkeyInit_pkey->version = 0; diff --git a/src/sftp.c b/src/sftp.c index 65c3cf1..6c9856f 100644 --- a/src/sftp.c +++ b/src/sftp.c @@ -1,6 +1,6 @@ /* Copyright (c) 2004-2008, Sara Golemon * Copyright (c) 2007 Eli Fant - * Copyright (c) 2009-2013 by Daniel Stenberg + * Copyright (c) 2009-2014 by Daniel Stenberg * All rights reserved. * * Redistribution and use in source and binary forms, @@ -781,13 +781,12 @@ static LIBSSH2_SFTP *sftp_init(LIBSSH2_SESSION *session) sftp_handle = session->sftpInit_sftp = - LIBSSH2_ALLOC(session, sizeof(LIBSSH2_SFTP)); + LIBSSH2_CALLOC(session, sizeof(LIBSSH2_SFTP)); if (!sftp_handle) { _libssh2_error(session, LIBSSH2_ERROR_ALLOC, "Unable to allocate a new SFTP structure"); goto sftp_init_error; } - memset(sftp_handle, 0, sizeof(LIBSSH2_SFTP)); sftp_handle->channel = session->sftpInit_channel; sftp_handle->request_id = 0; @@ -1173,14 +1172,13 @@ sftp_open(LIBSSH2_SFTP *sftp, const char *filename, return NULL; } - fp = LIBSSH2_ALLOC(session, sizeof(LIBSSH2_SFTP_HANDLE)); + fp = LIBSSH2_CALLOC(session, sizeof(LIBSSH2_SFTP_HANDLE)); if (!fp) { _libssh2_error(session, LIBSSH2_ERROR_ALLOC, "Unable to allocate new SFTP handle structure"); LIBSSH2_FREE(session, data); return NULL; } - memset(fp, 0, sizeof(LIBSSH2_SFTP_HANDLE)); fp->handle_type = open_file ? LIBSSH2_SFTP_HANDLE_FILE : LIBSSH2_SFTP_HANDLE_DIR; diff --git a/src/userauth.c b/src/userauth.c index edfe729..8407d14 100644 --- a/src/userauth.c +++ b/src/userauth.c @@ -1501,32 +1501,26 @@ userauth_keyboard_interactive(LIBSSH2_SESSION * session, if(session->userauth_kybd_num_prompts) { session->userauth_kybd_prompts = - LIBSSH2_ALLOC(session, - sizeof(LIBSSH2_USERAUTH_KBDINT_PROMPT) * - session->userauth_kybd_num_prompts); + LIBSSH2_CALLOC(session, + sizeof(LIBSSH2_USERAUTH_KBDINT_PROMPT) * + session->userauth_kybd_num_prompts); if (!session->userauth_kybd_prompts) { _libssh2_error(session, LIBSSH2_ERROR_ALLOC, "Unable to allocate memory for " "keyboard-interactive prompts array"); goto cleanup; } - memset(session->userauth_kybd_prompts, 0, - sizeof(LIBSSH2_USERAUTH_KBDINT_PROMPT) * - session->userauth_kybd_num_prompts); session->userauth_kybd_responses = - LIBSSH2_ALLOC(session, - sizeof(LIBSSH2_USERAUTH_KBDINT_RESPONSE) * - session->userauth_kybd_num_prompts); + LIBSSH2_CALLOC(session, + sizeof(LIBSSH2_USERAUTH_KBDINT_RESPONSE) * + session->userauth_kybd_num_prompts); if (!session->userauth_kybd_responses) { _libssh2_error(session, LIBSSH2_ERROR_ALLOC, "Unable to allocate memory for " "keyboard-interactive responses array"); goto cleanup; } - memset(session->userauth_kybd_responses, 0, - sizeof(LIBSSH2_USERAUTH_KBDINT_RESPONSE) * - session->userauth_kybd_num_prompts); for(i = 0; i != session->userauth_kybd_num_prompts; ++i) { /* string prompt[1] (ISO-10646 UTF-8) */ @@ -1534,16 +1528,14 @@ userauth_keyboard_interactive(LIBSSH2_SESSION * session, _libssh2_ntohu32(s); s += 4; session->userauth_kybd_prompts[i].text = - LIBSSH2_ALLOC(session, - session->userauth_kybd_prompts[i].length); + LIBSSH2_CALLOC(session, + session->userauth_kybd_prompts[i].length); if (!session->userauth_kybd_prompts[i].text) { _libssh2_error(session, LIBSSH2_ERROR_ALLOC, "Unable to allocate memory for " "keyboard-interactive prompt message"); goto cleanup; } - memcpy(session->userauth_kybd_prompts[i].text, s, - session->userauth_kybd_prompts[i].length); s += session->userauth_kybd_prompts[i].length; /* boolean echo[1] */