Skip to content

Commit

Permalink
calloc: introduce LIBSSH2_CALLOC()
Browse files Browse the repository at this point in the history
A simple function using LIBSSH2_ALLOC + memset, since this pattern was
used in multiple places and this simplies code in general.
  • Loading branch information
bagder committed Dec 22, 2014
1 parent 977a3b6 commit 031566f
Show file tree
Hide file tree
Showing 12 changed files with 39 additions and 50 deletions.
5 changes: 2 additions & 3 deletions src/agent.c
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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);
Expand Down
7 changes: 2 additions & 5 deletions src/channel.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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) {
Expand Down
5 changes: 2 additions & 3 deletions src/comp.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* Copyright (c) 2004-2007, Sara Golemon <[email protected]>
* Copyright (c) 2010, Daniel Stenberg <[email protected]>
* Copyright (c) 2010-2014, Daniel Stenberg <[email protected]>
* All rights reserved.
*
* Redistribution and use in source and binary forms,
Expand Down Expand Up @@ -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;
Expand Down
5 changes: 2 additions & 3 deletions src/hostkey.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* Copyright (c) 2004-2006, Sara Golemon <[email protected]>
* Copyright (c) 2009 by Daniel Stenberg
* Copyright (c) 2009-2014 by Daniel Stenberg
* All rights reserved.
*
* Redistribution and use in source and binary forms,
Expand Down Expand Up @@ -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++) {
Expand Down
6 changes: 2 additions & 4 deletions src/knownhost.c
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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) {
Expand Down
3 changes: 2 additions & 1 deletion src/libssh2_priv.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* Copyright (c) 2004-2008, 2010, Sara Golemon <[email protected]>
* Copyright (c) 2009-2011 by Daniel Stenberg
* Copyright (c) 2009-2014 by Daniel Stenberg
* Copyright (c) 2010 Simon Josefsson
* All rights reserved.
*
Expand Down Expand Up @@ -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))
Expand Down
11 changes: 10 additions & 1 deletion src/misc.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* Copyright (c) 2004-2007 Sara Golemon <[email protected]>
* Copyright (c) 2009-2010 by Daniel Stenberg
* Copyright (c) 2009-2014 by Daniel Stenberg
* Copyright (c) 2010 Simon Josefsson
* All rights reserved.
*
Expand Down Expand Up @@ -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;
}
3 changes: 2 additions & 1 deletion src/misc.h
Original file line number Diff line number Diff line change
@@ -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.
*
Expand Down Expand Up @@ -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 */
Expand Down
9 changes: 3 additions & 6 deletions src/packet.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* Copyright (c) 2004-2007, Sara Golemon <[email protected]>
* 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.
*
Expand Down Expand Up @@ -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 "
Expand All @@ -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,
Expand Down Expand Up @@ -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;
Expand Down
3 changes: 1 addition & 2 deletions src/publickey.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
8 changes: 3 additions & 5 deletions src/sftp.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* Copyright (c) 2004-2008, Sara Golemon <[email protected]>
* Copyright (c) 2007 Eli Fant <[email protected]>
* 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,
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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;

Expand Down
24 changes: 8 additions & 16 deletions src/userauth.c
Original file line number Diff line number Diff line change
Expand Up @@ -1501,49 +1501,41 @@ 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) */
session->userauth_kybd_prompts[i].length =
_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] */
Expand Down

0 comments on commit 031566f

Please sign in to comment.