1
1

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.
Этот коммит содержится в:
Daniel Stenberg 2014-12-22 15:59:21 +01:00
родитель 977a3b6a76
Коммит 031566f9cc
12 изменённых файлов: 39 добавлений и 50 удалений

Просмотреть файл

@ -1,6 +1,6 @@
/* /*
* Copyright (c) 2009 by Daiki Ueno * Copyright (c) 2009 by Daiki Ueno
* Copyright (C) 2010 by Daniel Stenberg * Copyright (C) 2014 by Daniel Stenberg
* All rights reserved. * All rights reserved.
* *
* Redistribution and use in source and binary forms, * Redistribution and use in source and binary forms,
@ -660,13 +660,12 @@ libssh2_agent_init(LIBSSH2_SESSION *session)
{ {
LIBSSH2_AGENT *agent; LIBSSH2_AGENT *agent;
agent = LIBSSH2_ALLOC(session, sizeof *agent); agent = LIBSSH2_CALLOC(session, sizeof *agent);
if (!agent) { if (!agent) {
_libssh2_error(session, LIBSSH2_ERROR_ALLOC, _libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate space for agent connection"); "Unable to allocate space for agent connection");
return NULL; return NULL;
} }
memset(agent, 0, sizeof *agent);
agent->fd = LIBSSH2_INVALID_SOCKET; agent->fd = LIBSSH2_INVALID_SOCKET;
agent->session = session; agent->session = session;
_libssh2_list_init(&agent->head); _libssh2_list_init(&agent->head);

Просмотреть файл

@ -158,14 +158,12 @@ _libssh2_channel_open(LIBSSH2_SESSION * session, const char *channel_type,
"Opening Channel - win %d pack %d", window_size, "Opening Channel - win %d pack %d", window_size,
packet_size); packet_size);
session->open_channel = session->open_channel =
LIBSSH2_ALLOC(session, sizeof(LIBSSH2_CHANNEL)); LIBSSH2_CALLOC(session, sizeof(LIBSSH2_CHANNEL));
if (!session->open_channel) { if (!session->open_channel) {
_libssh2_error(session, LIBSSH2_ERROR_ALLOC, _libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate space for channel data"); "Unable to allocate space for channel data");
return NULL; return NULL;
} }
memset(session->open_channel, 0, sizeof(LIBSSH2_CHANNEL));
session->open_channel->channel_type_len = channel_type_len; session->open_channel->channel_type_len = channel_type_len;
session->open_channel->channel_type = session->open_channel->channel_type =
LIBSSH2_ALLOC(session, channel_type_len); 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) { if (data[0] == SSH_MSG_REQUEST_SUCCESS) {
LIBSSH2_LISTENER *listener; LIBSSH2_LISTENER *listener;
listener = LIBSSH2_ALLOC(session, sizeof(LIBSSH2_LISTENER)); listener = LIBSSH2_CALLOC(session, sizeof(LIBSSH2_LISTENER));
if (!listener) if (!listener)
_libssh2_error(session, LIBSSH2_ERROR_ALLOC, _libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate memory for listener queue"); "Unable to allocate memory for listener queue");
else { else {
memset(listener, 0, sizeof(LIBSSH2_LISTENER));
listener->host = listener->host =
LIBSSH2_ALLOC(session, session->fwdLstn_host_len + 1); LIBSSH2_ALLOC(session, session->fwdLstn_host_len + 1);
if (!listener->host) { if (!listener->host) {

Просмотреть файл

@ -1,5 +1,5 @@
/* Copyright (c) 2004-2007, Sara Golemon <sarag@libssh2.org> /* Copyright (c) 2004-2007, Sara Golemon <sarag@libssh2.org>
* Copyright (c) 2010, Daniel Stenberg <daniel@haxx.se> * Copyright (c) 2010-2014, Daniel Stenberg <daniel@haxx.se>
* All rights reserved. * All rights reserved.
* *
* Redistribution and use in source and binary forms, * Redistribution and use in source and binary forms,
@ -141,13 +141,12 @@ comp_method_zlib_init(LIBSSH2_SESSION * session, int compr,
z_stream *strm; z_stream *strm;
int status; int status;
strm = LIBSSH2_ALLOC(session, sizeof(z_stream)); strm = LIBSSH2_CALLOC(session, sizeof(z_stream));
if (!strm) { if (!strm) {
return _libssh2_error(session, LIBSSH2_ERROR_ALLOC, return _libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate memory for " "Unable to allocate memory for "
"zlib compression/decompression"); "zlib compression/decompression");
} }
memset(strm, 0, sizeof(z_stream));
strm->opaque = (voidpf) session; strm->opaque = (voidpf) session;
strm->zalloc = (alloc_func) comp_method_zlib_alloc; strm->zalloc = (alloc_func) comp_method_zlib_alloc;

Просмотреть файл

@ -1,5 +1,5 @@
/* Copyright (c) 2004-2006, Sara Golemon <sarag@libssh2.org> /* Copyright (c) 2004-2006, Sara Golemon <sarag@libssh2.org>
* Copyright (c) 2009 by Daniel Stenberg * Copyright (c) 2009-2014 by Daniel Stenberg
* All rights reserved. * All rights reserved.
* *
* Redistribution and use in source and binary forms, * Redistribution and use in source and binary forms,
@ -347,13 +347,12 @@ hostkey_method_ssh_dss_signv(LIBSSH2_SESSION * session,
libssh2_sha1_ctx ctx; libssh2_sha1_ctx ctx;
int i; int i;
*signature = LIBSSH2_ALLOC(session, 2 * SHA_DIGEST_LENGTH); *signature = LIBSSH2_CALLOC(session, 2 * SHA_DIGEST_LENGTH);
if (!*signature) { if (!*signature) {
return -1; return -1;
} }
*signature_len = 2 * SHA_DIGEST_LENGTH; *signature_len = 2 * SHA_DIGEST_LENGTH;
memset(*signature, 0, 2 * SHA_DIGEST_LENGTH);
libssh2_sha1_init(&ctx); libssh2_sha1_init(&ctx);
for(i = 0; i < veccount; i++) { for(i = 0; i < veccount; i++) {

Просмотреть файл

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2009-2013 by Daniel Stenberg * Copyright (c) 2009-2014 by Daniel Stenberg
* All rights reserved. * All rights reserved.
* *
* Redistribution and use in source and binary forms, * 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, return _libssh2_error(hosts->session, LIBSSH2_ERROR_INVAL,
"No key type set"); "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, return _libssh2_error(hosts->session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate memory for known host " "Unable to allocate memory for known host "
"entry"); "entry");
memset(entry, 0, sizeof(struct known_host));
entry->typemask = typemask; entry->typemask = typemask;
switch(entry->typemask & LIBSSH2_KNOWNHOST_TYPE_MASK) { switch(entry->typemask & LIBSSH2_KNOWNHOST_TYPE_MASK) {

Просмотреть файл

@ -1,5 +1,5 @@
/* Copyright (c) 2004-2008, 2010, Sara Golemon <sarag@libssh2.org> /* Copyright (c) 2004-2008, 2010, Sara Golemon <sarag@libssh2.org>
* Copyright (c) 2009-2011 by Daniel Stenberg * Copyright (c) 2009-2014 by Daniel Stenberg
* Copyright (c) 2010 Simon Josefsson * Copyright (c) 2010 Simon Josefsson
* All rights reserved. * All rights reserved.
* *
@ -152,6 +152,7 @@ static inline int writev(int sock, struct iovec *iov, int nvecs)
#define LIBSSH2_ALLOC(session, count) \ #define LIBSSH2_ALLOC(session, count) \
session->alloc((count), &(session)->abstract) session->alloc((count), &(session)->abstract)
#define LIBSSH2_CALLOC(session, count) _libssh2_calloc(session, count)
#define LIBSSH2_REALLOC(session, ptr, count) \ #define LIBSSH2_REALLOC(session, ptr, count) \
((ptr) ? session->realloc((ptr), (count), &(session)->abstract) : \ ((ptr) ? session->realloc((ptr), (count), &(session)->abstract) : \
session->alloc((count), &(session)->abstract)) session->alloc((count), &(session)->abstract))

Просмотреть файл

@ -1,5 +1,5 @@
/* Copyright (c) 2004-2007 Sara Golemon <sarag@libssh2.org> /* Copyright (c) 2004-2007 Sara Golemon <sarag@libssh2.org>
* Copyright (c) 2009-2010 by Daniel Stenberg * Copyright (c) 2009-2014 by Daniel Stenberg
* Copyright (c) 2010 Simon Josefsson * Copyright (c) 2010 Simon Josefsson
* All rights reserved. * All rights reserved.
* *
@ -619,3 +619,12 @@ int __cdecl _libssh2_gettimeofday(struct timeval *tp, void *tzp)
#endif #endif
void *_libssh2_calloc(LIBSSH2_SESSION* session, size_t size)
{
void *p = LIBSSH2_ALLOC(session, size);
if(p) {
memset(p, 0, size);
}
return p;
}

Просмотреть файл

@ -1,6 +1,6 @@
#ifndef __LIBSSH2_MISC_H #ifndef __LIBSSH2_MISC_H
#define __LIBSSH2_MISC_H #define __LIBSSH2_MISC_H
/* Copyright (c) 2009-2011 by Daniel Stenberg /* Copyright (c) 2009-2014 by Daniel Stenberg
* *
* All rights reserved. * 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_htonu32(unsigned char *buf, uint32_t val);
void _libssh2_store_u32(unsigned char **buf, uint32_t value); 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_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__) #if defined(LIBSSH2_WIN32) && !defined(__MINGW32__) && !defined(__CYGWIN__)
/* provide a private one */ /* provide a private one */

Просмотреть файл

@ -1,6 +1,6 @@
/* Copyright (c) 2004-2007, Sara Golemon <sarag@libssh2.org> /* Copyright (c) 2004-2007, Sara Golemon <sarag@libssh2.org>
* Copyright (c) 2005,2006 Mikhail Gusarov * 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 * Copyright (c) 2010 Simon Josefsson
* All rights reserved. * All rights reserved.
* *
@ -139,7 +139,7 @@ packet_queue_listener(LIBSSH2_SESSION * session, unsigned char *data,
break; break;
} }
channel = LIBSSH2_ALLOC(session, sizeof(LIBSSH2_CHANNEL)); channel = LIBSSH2_CALLOC(session, sizeof(LIBSSH2_CHANNEL));
if (!channel) { if (!channel) {
_libssh2_error(session, LIBSSH2_ERROR_ALLOC, _libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate a channel for " "Unable to allocate a channel for "
@ -150,8 +150,6 @@ packet_queue_listener(LIBSSH2_SESSION * session, unsigned char *data,
} }
listen_state->channel = channel; listen_state->channel = channel;
memset(channel, 0, sizeof(LIBSSH2_CHANNEL));
channel->session = session; channel->session = session;
channel->channel_type_len = sizeof("forwarded-tcpip") - 1; channel->channel_type_len = sizeof("forwarded-tcpip") - 1;
channel->channel_type = LIBSSH2_ALLOC(session, channel->channel_type = LIBSSH2_ALLOC(session,
@ -299,14 +297,13 @@ packet_x11_open(LIBSSH2_SESSION * session, unsigned char *data,
if (session->x11) { if (session->x11) {
if (x11open_state->state == libssh2_NB_state_allocated) { if (x11open_state->state == libssh2_NB_state_allocated) {
channel = LIBSSH2_ALLOC(session, sizeof(LIBSSH2_CHANNEL)); channel = LIBSSH2_CALLOC(session, sizeof(LIBSSH2_CHANNEL));
if (!channel) { if (!channel) {
_libssh2_error(session, LIBSSH2_ERROR_ALLOC, _libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"allocate a channel for new connection"); "allocate a channel for new connection");
failure_code = SSH_OPEN_RESOURCE_SHORTAGE; failure_code = SSH_OPEN_RESOURCE_SHORTAGE;
goto x11_exit; goto x11_exit;
} }
memset(channel, 0, sizeof(LIBSSH2_CHANNEL));
channel->session = session; channel->session = session;
channel->channel_type_len = sizeof("x11") - 1; channel->channel_type_len = sizeof("x11") - 1;

Просмотреть файл

@ -350,13 +350,12 @@ static LIBSSH2_PUBLICKEY *publickey_init(LIBSSH2_SESSION *session)
} }
session->pkeyInit_pkey = session->pkeyInit_pkey =
LIBSSH2_ALLOC(session, sizeof(LIBSSH2_PUBLICKEY)); LIBSSH2_CALLOC(session, sizeof(LIBSSH2_PUBLICKEY));
if (!session->pkeyInit_pkey) { if (!session->pkeyInit_pkey) {
_libssh2_error(session, LIBSSH2_ERROR_ALLOC, _libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate a new publickey structure"); "Unable to allocate a new publickey structure");
goto err_exit; goto err_exit;
} }
memset(session->pkeyInit_pkey, 0, sizeof(LIBSSH2_PUBLICKEY));
session->pkeyInit_pkey->channel = session->pkeyInit_channel; session->pkeyInit_pkey->channel = session->pkeyInit_channel;
session->pkeyInit_pkey->version = 0; session->pkeyInit_pkey->version = 0;

Просмотреть файл

@ -1,6 +1,6 @@
/* Copyright (c) 2004-2008, Sara Golemon <sarag@libssh2.org> /* Copyright (c) 2004-2008, Sara Golemon <sarag@libssh2.org>
* Copyright (c) 2007 Eli Fant <elifantu@mail.ru> * Copyright (c) 2007 Eli Fant <elifantu@mail.ru>
* Copyright (c) 2009-2013 by Daniel Stenberg * Copyright (c) 2009-2014 by Daniel Stenberg
* All rights reserved. * All rights reserved.
* *
* Redistribution and use in source and binary forms, * Redistribution and use in source and binary forms,
@ -781,13 +781,12 @@ static LIBSSH2_SFTP *sftp_init(LIBSSH2_SESSION *session)
sftp_handle = sftp_handle =
session->sftpInit_sftp = session->sftpInit_sftp =
LIBSSH2_ALLOC(session, sizeof(LIBSSH2_SFTP)); LIBSSH2_CALLOC(session, sizeof(LIBSSH2_SFTP));
if (!sftp_handle) { if (!sftp_handle) {
_libssh2_error(session, LIBSSH2_ERROR_ALLOC, _libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate a new SFTP structure"); "Unable to allocate a new SFTP structure");
goto sftp_init_error; goto sftp_init_error;
} }
memset(sftp_handle, 0, sizeof(LIBSSH2_SFTP));
sftp_handle->channel = session->sftpInit_channel; sftp_handle->channel = session->sftpInit_channel;
sftp_handle->request_id = 0; sftp_handle->request_id = 0;
@ -1173,14 +1172,13 @@ sftp_open(LIBSSH2_SFTP *sftp, const char *filename,
return NULL; return NULL;
} }
fp = LIBSSH2_ALLOC(session, sizeof(LIBSSH2_SFTP_HANDLE)); fp = LIBSSH2_CALLOC(session, sizeof(LIBSSH2_SFTP_HANDLE));
if (!fp) { if (!fp) {
_libssh2_error(session, LIBSSH2_ERROR_ALLOC, _libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate new SFTP handle structure"); "Unable to allocate new SFTP handle structure");
LIBSSH2_FREE(session, data); LIBSSH2_FREE(session, data);
return NULL; return NULL;
} }
memset(fp, 0, sizeof(LIBSSH2_SFTP_HANDLE));
fp->handle_type = open_file ? LIBSSH2_SFTP_HANDLE_FILE : fp->handle_type = open_file ? LIBSSH2_SFTP_HANDLE_FILE :
LIBSSH2_SFTP_HANDLE_DIR; LIBSSH2_SFTP_HANDLE_DIR;

Просмотреть файл

@ -1501,32 +1501,26 @@ userauth_keyboard_interactive(LIBSSH2_SESSION * session,
if(session->userauth_kybd_num_prompts) { if(session->userauth_kybd_num_prompts) {
session->userauth_kybd_prompts = session->userauth_kybd_prompts =
LIBSSH2_ALLOC(session, LIBSSH2_CALLOC(session,
sizeof(LIBSSH2_USERAUTH_KBDINT_PROMPT) * sizeof(LIBSSH2_USERAUTH_KBDINT_PROMPT) *
session->userauth_kybd_num_prompts); session->userauth_kybd_num_prompts);
if (!session->userauth_kybd_prompts) { if (!session->userauth_kybd_prompts) {
_libssh2_error(session, LIBSSH2_ERROR_ALLOC, _libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate memory for " "Unable to allocate memory for "
"keyboard-interactive prompts array"); "keyboard-interactive prompts array");
goto cleanup; goto cleanup;
} }
memset(session->userauth_kybd_prompts, 0,
sizeof(LIBSSH2_USERAUTH_KBDINT_PROMPT) *
session->userauth_kybd_num_prompts);
session->userauth_kybd_responses = session->userauth_kybd_responses =
LIBSSH2_ALLOC(session, LIBSSH2_CALLOC(session,
sizeof(LIBSSH2_USERAUTH_KBDINT_RESPONSE) * sizeof(LIBSSH2_USERAUTH_KBDINT_RESPONSE) *
session->userauth_kybd_num_prompts); session->userauth_kybd_num_prompts);
if (!session->userauth_kybd_responses) { if (!session->userauth_kybd_responses) {
_libssh2_error(session, LIBSSH2_ERROR_ALLOC, _libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate memory for " "Unable to allocate memory for "
"keyboard-interactive responses array"); "keyboard-interactive responses array");
goto cleanup; 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) { for(i = 0; i != session->userauth_kybd_num_prompts; ++i) {
/* string prompt[1] (ISO-10646 UTF-8) */ /* string prompt[1] (ISO-10646 UTF-8) */
@ -1534,16 +1528,14 @@ userauth_keyboard_interactive(LIBSSH2_SESSION * session,
_libssh2_ntohu32(s); _libssh2_ntohu32(s);
s += 4; s += 4;
session->userauth_kybd_prompts[i].text = session->userauth_kybd_prompts[i].text =
LIBSSH2_ALLOC(session, LIBSSH2_CALLOC(session,
session->userauth_kybd_prompts[i].length); session->userauth_kybd_prompts[i].length);
if (!session->userauth_kybd_prompts[i].text) { if (!session->userauth_kybd_prompts[i].text) {
_libssh2_error(session, LIBSSH2_ERROR_ALLOC, _libssh2_error(session, LIBSSH2_ERROR_ALLOC,
"Unable to allocate memory for " "Unable to allocate memory for "
"keyboard-interactive prompt message"); "keyboard-interactive prompt message");
goto cleanup; goto cleanup;
} }
memcpy(session->userauth_kybd_prompts[i].text, s,
session->userauth_kybd_prompts[i].length);
s += session->userauth_kybd_prompts[i].length; s += session->userauth_kybd_prompts[i].length;
/* boolean echo[1] */ /* boolean echo[1] */