* Added more error printing to logs in server code. Affected
files silcd/server.c and silcd/protocol.c.
+ * Fixed -S option parsing in Irssi SILC Client. Affected file
+ irssi/src/silc/core/silc-core.c.
+
+ * Added silc_buffer_alloc_size function. Affected file is
+ lib/silcutil/silcbuffer.h.
+
Tue Mar 5 14:37:27 EET 2002 Pekka Riikonen <priikone@silcnet.org>
* Changed all silc_[hash|hmac|cipher|pkcs]_default tables to
SILC_BUFFER_LEN macro can do the same. These would save
totally 8 bytes of memory per buffer.
- Add also perhaps function silc_buffer_alloc_size that would
- effectively do:
-
- return silc_buffer_pull_tail(silc_buffer_alloc(size),
- size);
-
- to not require the user to give the pull_tail anymore.
-
o Scheduler can be optimized for FD tasks by changing the fd_queue
to SilcHashTable instead of using linked list. We need to do
one-to-one mapping of FD to task and hash table is more efficient
int i;
for (i = 0; silc_default_ciphers[i].name; i++)
if (!strcmp(silc_default_ciphers[i].name, cipher->name)) {
- silc_cipher_register(&silc_default_ciphers[i]);
+ silc_cipher_register((SilcCipherObject *)&silc_default_ciphers[i]);
break;
}
if (!silc_cipher_is_supported(cipher->name)) {
int i;
for (i = 0; silc_default_hash[i].name; i++)
if (!strcmp(silc_default_hash[i].name, hash->name)) {
- silc_hash_register(&silc_default_hash[i]);
+ silc_hash_register((SilcHashObject *)&silc_default_hash[i]);
break;
}
if (!silc_hash_is_supported(hash->name)) {
silc_server_stop(server);
exit(1);
}
+
/* Register the HMAC */
memset(&hmac_obj, 0, sizeof(hmac_obj));
hmac_obj.name = hmac->name;
int i;
for (i = 0; silc_default_pkcs[i].name; i++)
if (!strcmp(silc_default_pkcs[i].name, pkcs->name)) {
- silc_pkcs_register(&silc_default_pkcs[i]);
+ silc_pkcs_register((SilcPKCSObject *)&silc_default_pkcs[i]);
break;
}
if (!silc_pkcs_is_supported(pkcs->name)) {
SilcUInt32 argc)
{
SilcBufferStruct buffer;
- SilcArgumentPayload new;
+ SilcArgumentPayload newp;
SilcUInt16 p_len = 0;
unsigned char arg_num = 0;
unsigned char arg_type = 0;
SILC_LOG_DEBUG(("Parsing argument payload"));
silc_buffer_set(&buffer, (unsigned char *)payload, payload_len);
- new = silc_calloc(1, sizeof(*new));
- new->argv = silc_calloc(argc, sizeof(unsigned char *));
- new->argv_lens = silc_calloc(argc, sizeof(SilcUInt32));
- new->argv_types = silc_calloc(argc, sizeof(SilcUInt32));
+ newp = silc_calloc(1, sizeof(*newp));
+ if (!newp)
+ return NULL;
+ newp->argv = silc_calloc(argc, sizeof(unsigned char *));
+ if (!newp->argv)
+ goto err;
+ newp->argv_lens = silc_calloc(argc, sizeof(SilcUInt32));
+ if (!newp->argv_lens)
+ goto err;
+ newp->argv_types = silc_calloc(argc, sizeof(SilcUInt32));
+ if (!newp->argv_types)
+ goto err;
/* Get arguments */
arg_num = 1;
if (ret == -1)
goto err;
- new->argv_lens[i] = p_len;
- new->argv_types[i] = arg_type;
+ newp->argv_lens[i] = p_len;
+ newp->argv_types[i] = arg_type;
if (p_len > buffer.len - 3)
break;
/* Get argument data */
silc_buffer_pull(&buffer, 3);
ret = silc_buffer_unformat(&buffer,
- SILC_STR_UI_XNSTRING_ALLOC(&new->argv[i],
+ SILC_STR_UI_XNSTRING_ALLOC(&newp->argv[i],
p_len),
SILC_STR_END);
if (ret == -1)
if (buffer.len != 0)
goto err;
- new->argc = argc;
- new->pos = 0;
+ newp->argc = argc;
+ newp->pos = 0;
silc_buffer_push(&buffer, pull_len);
- return new;
+ return newp;
err:
- if (i) {
- int k;
+ if (i)
+ for (ret = 0; ret < i; ret++)
+ silc_free(newp->argv[ret]);
- for (k = 0; k < i; k++)
- silc_free(new->argv[k]);
- }
-
- silc_free(new->argv);
- silc_free(new->argv_lens);
- silc_free(new->argv_types);
-
- if (new)
- silc_free(new);
+ silc_free(newp->argv);
+ silc_free(newp->argv_lens);
+ silc_free(newp->argv_types);
+ silc_free(newp);
return NULL;
}
for (i = 0; i < argc; i++)
len += 3 + argv_lens[i];
- buffer = silc_buffer_alloc(len);
- silc_buffer_pull_tail(buffer, SILC_BUFFER_END(buffer));
+ buffer = silc_buffer_alloc_size(len);
+ if (!buffer)
+ return NULL;
/* Put arguments */
for (i = 0; i < argc; i++) {
for (i = 0; i < payload->argc; i++)
len += 3 + payload->argv_lens[i];
- buffer = silc_buffer_alloc(len);
- silc_buffer_pull_tail(buffer, SILC_BUFFER_END(buffer));
+ buffer = silc_buffer_alloc_size(len);
+ if (!buffer)
+ return NULL;
/* Put arguments */
for (i = 0; i < payload->argc; i++) {
SilcUInt32 data_len)
{
SilcBufferStruct buffer;
- SilcAuthPayload new;
+ SilcAuthPayload newp;
int ret;
SILC_LOG_DEBUG(("Parsing Authentication Payload"));
silc_buffer_set(&buffer, (unsigned char *)data, data_len);
- new = silc_calloc(1, sizeof(*new));
+ newp = silc_calloc(1, sizeof(*newp));
+ if (!newp)
+ return NULL;
/* Parse the payload */
ret = silc_buffer_unformat(&buffer,
- SILC_STR_UI_SHORT(&new->len),
- SILC_STR_UI_SHORT(&new->auth_method),
- SILC_STR_UI16_NSTRING_ALLOC(&new->random_data,
- &new->random_len),
- SILC_STR_UI16_NSTRING_ALLOC(&new->auth_data,
- &new->auth_len),
+ SILC_STR_UI_SHORT(&newp->len),
+ SILC_STR_UI_SHORT(&newp->auth_method),
+ SILC_STR_UI16_NSTRING_ALLOC(&newp->random_data,
+ &newp->random_len),
+ SILC_STR_UI16_NSTRING_ALLOC(&newp->auth_data,
+ &newp->auth_len),
SILC_STR_END);
if (ret == -1) {
- silc_free(new);
+ silc_free(newp);
return NULL;
}
- if (new->len != buffer.len) {
- silc_auth_payload_free(new);
+ if (newp->len != buffer.len) {
+ silc_auth_payload_free(newp);
return NULL;
}
/* If password authentication, random data must not be set */
- if (new->auth_method == SILC_AUTH_PASSWORD && new->random_len) {
- silc_auth_payload_free(new);
+ if (newp->auth_method == SILC_AUTH_PASSWORD && newp->random_len) {
+ silc_auth_payload_free(newp);
return NULL;
}
- return new;
+ return newp;
}
/* Encodes authentication payload into buffer and returns it */
SILC_LOG_DEBUG(("Encoding Authentication Payload"));
len = 2 + 2 + 2 + random_len + 2 + auth_len;
- buffer = silc_buffer_alloc(len);
- silc_buffer_pull_tail(buffer, SILC_BUFFER_END(buffer));
+ buffer = silc_buffer_alloc_size(len);
+ if (!buffer)
+ return NULL;
silc_buffer_format(buffer,
SILC_STR_UI_SHORT(len),
SILC_STR_UI_SHORT(method),
}
id_len = silc_id_get_len(id, type);
- buf = silc_buffer_alloc(random_len + id_len + pk_len);
- silc_buffer_pull_tail(buf, SILC_BUFFER_END(buf));
+ buf = silc_buffer_alloc_size(random_len + id_len + pk_len);
+ if (!buf) {
+ silc_free(pk);
+ silc_free(id_data);
+ return NULL;
+ }
silc_buffer_format(buf,
SILC_STR_UI_XNSTRING(random, random_len),
SILC_STR_UI_XNSTRING(id_data, id_len),
SILC_STR_END);
ret = silc_memdup(buf->data, buf->len);
+ if (!ret)
+ return NULL;
if (ret_len)
*ret_len = buf->len;
SilcUInt32 payload_len)
{
SilcBufferStruct buffer;
- SilcKeyAgreementPayload new;
+ SilcKeyAgreementPayload newp;
int ret;
SILC_LOG_DEBUG(("Parsing Key Agreement Payload"));
silc_buffer_set(&buffer, (unsigned char *)payload, payload_len);
- new = silc_calloc(1, sizeof(*new));
+ newp = silc_calloc(1, sizeof(*newp));
+ if (!newp)
+ return NULL;
/* Parse the payload */
ret = silc_buffer_unformat(&buffer,
- SILC_STR_UI16_NSTRING_ALLOC(&new->hostname,
- &new->hostname_len),
- SILC_STR_UI_INT(&new->port),
+ SILC_STR_UI16_NSTRING_ALLOC(&newp->hostname,
+ &newp->hostname_len),
+ SILC_STR_UI_INT(&newp->port),
SILC_STR_END);
if (ret == -1) {
- silc_free(new);
+ silc_free(newp);
return NULL;
}
- return new;
+ return newp;
}
/* Encodes the Key Agreement protocol and returns the encoded buffer */
SILC_LOG_DEBUG(("Encoding Key Agreement Payload"));
- buffer = silc_buffer_alloc(2 + len + 4);
- silc_buffer_pull_tail(buffer, SILC_BUFFER_END(buffer));
+ buffer = silc_buffer_alloc_size(2 + len + 4);
+ if (!buffer)
+ return NULL;
silc_buffer_format(buffer,
SILC_STR_UI_SHORT(len),
SILC_STR_UI_XNSTRING(hostname, len),
SilcUInt32 payload_len)
{
SilcBufferStruct buffer;
- SilcChannelPayload new;
+ SilcChannelPayload newp;
int ret;
SILC_LOG_DEBUG(("Parsing channel payload"));
silc_buffer_set(&buffer, (unsigned char *)payload, payload_len);
- new = silc_calloc(1, sizeof(*new));
+ newp = silc_calloc(1, sizeof(*newp));
+ if (!newp)
+ return NULL;
/* Parse the Channel Payload. Ignore the padding. */
ret = silc_buffer_unformat(&buffer,
- SILC_STR_UI16_NSTRING_ALLOC(&new->channel_name,
- &new->name_len),
- SILC_STR_UI16_NSTRING_ALLOC(&new->channel_id,
- &new->id_len),
- SILC_STR_UI_INT(&new->mode),
+ SILC_STR_UI16_NSTRING_ALLOC(&newp->channel_name,
+ &newp->name_len),
+ SILC_STR_UI16_NSTRING_ALLOC(&newp->channel_id,
+ &newp->id_len),
+ SILC_STR_UI_INT(&newp->mode),
SILC_STR_END);
if (ret == -1)
goto err;
- if ((new->name_len < 1 || new->name_len > buffer.len) ||
- (new->id_len < 1 || new->id_len > buffer.len)) {
+ if ((newp->name_len < 1 || newp->name_len > buffer.len) ||
+ (newp->id_len < 1 || newp->id_len > buffer.len)) {
SILC_LOG_ERROR(("Incorrect channel payload in packet, packet dropped"));
goto err;
}
- return new;
+ return newp;
err:
- silc_channel_payload_free(new);
+ silc_channel_payload_free(newp);
return NULL;
}
{
SilcBufferStruct buffer;
SilcDList list;
- SilcChannelPayload new;
+ SilcChannelPayload newp;
int len, ret;
SILC_LOG_DEBUG(("Parsing channel payload list"));
list = silc_dlist_init();
while (buffer.len) {
- new = silc_calloc(1, sizeof(*new));
+ newp = silc_calloc(1, sizeof(*newp));
+ if (!newp)
+ goto err;
ret = silc_buffer_unformat(&buffer,
- SILC_STR_UI16_NSTRING_ALLOC(&new->channel_name,
- &new->name_len),
- SILC_STR_UI16_NSTRING_ALLOC(&new->channel_id,
- &new->id_len),
- SILC_STR_UI_INT(&new->mode),
+ SILC_STR_UI16_NSTRING_ALLOC(&newp->channel_name,
+ &newp->name_len),
+ SILC_STR_UI16_NSTRING_ALLOC(&newp->channel_id,
+ &newp->id_len),
+ SILC_STR_UI_INT(&newp->mode),
SILC_STR_END);
if (ret == -1)
goto err;
- if ((new->name_len < 1 || new->name_len > buffer.len) ||
- (new->id_len < 1 || new->id_len > buffer.len)) {
+ if ((newp->name_len < 1 || newp->name_len > buffer.len) ||
+ (newp->id_len < 1 || newp->id_len > buffer.len)) {
SILC_LOG_ERROR(("Incorrect channel payload in packet, packet dropped"));
goto err;
}
- len = 2 + new->name_len + 2 + new->id_len + 4;
+ len = 2 + newp->name_len + 2 + newp->id_len + 4;
if (buffer.len < len)
break;
silc_buffer_pull(&buffer, len);
- silc_dlist_add(list, new);
+ silc_dlist_add(list, newp);
}
return list;
SILC_LOG_DEBUG(("Encoding message payload"));
- buffer = silc_buffer_alloc(2 + channel_name_len + 2 + channel_id_len + 4);
- silc_buffer_pull_tail(buffer, SILC_BUFFER_END(buffer));
+ buffer = silc_buffer_alloc_size(2 + channel_name_len + 2 +
+ channel_id_len + 4);
+ if (!buffer)
+ return NULL;
/* Encode the Channel Payload */
silc_buffer_format(buffer,
/* Allocate destination decryption buffer since we do not want to modify
the original data buffer, since we might want to call this function
many times for same payload. */
- if (hmac)
+ if (hmac) {
dst = silc_calloc(data_len - iv_len, sizeof(*dst));
- else
+ if (!dst)
+ return FALSE;
+ } else {
dst = data;
+ }
/* Decrypt the channel message */
silc_cipher_decrypt(cipher, data, dst, data_len - iv_len, iv);
SilcHmac hmac)
{
SilcBufferStruct buffer;
- SilcChannelMessagePayload new;
+ SilcChannelMessagePayload newp;
int ret;
SilcUInt32 iv_len, mac_len;
iv_len = silc_cipher_get_block_len(cipher);
mac_len = silc_hmac_len(hmac);
- new = silc_calloc(1, sizeof(*new));
+ newp = silc_calloc(1, sizeof(*newp));
+ if (!newp)
+ return NULL;
/* Parse the Channel Message Payload. Ignore the padding. */
ret = silc_buffer_unformat(&buffer,
- SILC_STR_UI_SHORT(&new->flags),
- SILC_STR_UI16_NSTRING_ALLOC(&new->data,
- &new->data_len),
+ SILC_STR_UI_SHORT(&newp->flags),
+ SILC_STR_UI16_NSTRING_ALLOC(&newp->data,
+ &newp->data_len),
SILC_STR_UI16_NSTRING(NULL, NULL),
- SILC_STR_UI_XNSTRING(&new->mac, mac_len),
- SILC_STR_UI_XNSTRING(&new->iv, iv_len),
+ SILC_STR_UI_XNSTRING(&newp->mac, mac_len),
+ SILC_STR_UI_XNSTRING(&newp->iv, iv_len),
SILC_STR_END);
if (ret == -1)
goto err;
- if (new->data_len > buffer.len) {
+ if (newp->data_len > buffer.len) {
SILC_LOG_ERROR(("Incorrect channel message payload in packet, "
"packet dropped"));
goto err;
}
- return new;
+ return newp;
err:
- silc_channel_message_payload_free(new);
+ silc_channel_message_payload_free(newp);
return NULL;
}
/* Allocate channel payload buffer */
len += pad_len + iv_len;
buffer = silc_buffer_alloc(len);
+ if (!buffer)
+ return NULL;
/* Generate padding */
for (i = 0; i < pad_len; i++) pad[i] = silc_rng_global_get_byte();
SilcUInt32 payload_len)
{
SilcBufferStruct buffer;
- SilcChannelKeyPayload new;
+ SilcChannelKeyPayload newp;
int ret;
SILC_LOG_DEBUG(("Parsing channel key payload"));
silc_buffer_set(&buffer, (unsigned char *)payload, payload_len);
- new = silc_calloc(1, sizeof(*new));
+ newp = silc_calloc(1, sizeof(*newp));
+ if (!newp)
+ return NULL;
/* Parse the Channel Key Payload */
ret =
silc_buffer_unformat(&buffer,
- SILC_STR_UI16_NSTRING_ALLOC(&new->id, &new->id_len),
- SILC_STR_UI16_NSTRING_ALLOC(&new->cipher,
- &new->cipher_len),
- SILC_STR_UI16_NSTRING_ALLOC(&new->key, &new->key_len),
+ SILC_STR_UI16_NSTRING_ALLOC(&newp->id, &newp->id_len),
+ SILC_STR_UI16_NSTRING_ALLOC(&newp->cipher,
+ &newp->cipher_len),
+ SILC_STR_UI16_NSTRING_ALLOC(&newp->key,
+ &newp->key_len),
SILC_STR_END);
if (ret == -1)
goto err;
- if (new->id_len < 1 || new->key_len < 1 || new->cipher_len < 1) {
+ if (newp->id_len < 1 || newp->key_len < 1 || newp->cipher_len < 1) {
SILC_LOG_ERROR(("Incorrect channel key payload in packet"));
goto err;
}
- return new;
+ return newp;
err:
- if (new->id)
- silc_free(new->id);
- if (new->cipher)
- silc_free(new->cipher);
- if (new->key)
- silc_free(new->key);
- silc_free(new);
+ if (newp->id)
+ silc_free(newp->id);
+ if (newp->cipher)
+ silc_free(newp->cipher);
+ if (newp->key)
+ silc_free(newp->key);
+ silc_free(newp);
return NULL;
}
/* Allocate channel payload buffer. Length is 2 + id + 2 + key +
2 + cipher */
len = 2 + id_len + 2 + key_len + 2 + cipher_len;
- buffer = silc_buffer_alloc(len);
-
- silc_buffer_pull_tail(buffer, SILC_BUFFER_END(buffer));
+ buffer = silc_buffer_alloc_size(len);
+ if (!buffer)
+ return NULL;
/* Encode the Channel Payload */
silc_buffer_format(buffer,
SilcUInt32 payload_len)
{
SilcBufferStruct buffer;
- SilcCommandPayload new;
+ SilcCommandPayload newp;
unsigned char args_num;
SilcUInt16 p_len;
int ret;
SILC_LOG_DEBUG(("Parsing command payload"));
silc_buffer_set(&buffer, (unsigned char *)payload, payload_len);
- new = silc_calloc(1, sizeof(*new));
+ newp = silc_calloc(1, sizeof(*newp));
+ if (!newp)
+ return NULL;
/* Parse the Command Payload */
ret = silc_buffer_unformat(&buffer,
SILC_STR_UI_SHORT(&p_len),
- SILC_STR_UI_CHAR(&new->cmd),
+ SILC_STR_UI_CHAR(&newp->cmd),
SILC_STR_UI_CHAR(&args_num),
- SILC_STR_UI_SHORT(&new->ident),
+ SILC_STR_UI_SHORT(&newp->ident),
SILC_STR_END);
if (ret == -1) {
- silc_free(new);
+ silc_free(newp);
return NULL;
}
if (p_len != buffer.len) {
SILC_LOG_ERROR(("Incorrect command payload in packet, packet dropped"));
- silc_free(new);
+ silc_free(newp);
return NULL;
}
- if (new->cmd == 0) {
- silc_free(new);
+ if (newp->cmd == 0) {
+ silc_free(newp);
return NULL;
}
silc_buffer_pull(&buffer, SILC_COMMAND_PAYLOAD_LEN);
if (args_num) {
- new->args = silc_argument_payload_parse(buffer.data, buffer.len, args_num);
- if (!new->args) {
- silc_free(new);
+ newp->args = silc_argument_payload_parse(buffer.data, buffer.len, args_num);
+ if (!newp->args) {
+ silc_free(newp);
return NULL;
}
}
silc_buffer_push(&buffer, SILC_COMMAND_PAYLOAD_LEN);
- return new;
+ return newp;
}
/* Encodes Command Payload returning it to SilcBuffer. */
}
len += SILC_COMMAND_PAYLOAD_LEN;
- buffer = silc_buffer_alloc(len);
- silc_buffer_pull_tail(buffer, SILC_BUFFER_END(buffer));
+ buffer = silc_buffer_alloc_size(len);
+ if (!buffer)
+ return NULL;
/* Create Command payload */
silc_buffer_format(buffer,
}
len += SILC_COMMAND_PAYLOAD_LEN;
- buffer = silc_buffer_alloc(len);
- silc_buffer_pull_tail(buffer, SILC_BUFFER_END(buffer));
+ buffer = silc_buffer_alloc_size(len);
+ if (!buffer) {
+ if (args)
+ silc_buffer_free(args);
+ return NULL;
+ }
/* Create Command payload */
silc_buffer_format(buffer,
unsigned char *x;
SilcUInt32 x_len;
SilcUInt32 x_type;
- SilcBuffer buffer;
+ SilcBuffer buffer = NULL;
int i, k = 0;
if (argc) {
argv = silc_calloc(argc, sizeof(unsigned char *));
+ if (!argv)
+ return NULL;
argv_lens = silc_calloc(argc, sizeof(SilcUInt32));
+ if (!argv_lens)
+ return NULL;
argv_types = silc_calloc(argc, sizeof(SilcUInt32));
+ if (!argv_types)
+ return NULL;
for (i = 0, k = 0; i < argc; i++) {
x_type = va_arg(ap, SilcUInt32);
continue;
argv[k] = silc_memdup(x, x_len);
+ if (!argv[k])
+ goto out;
argv_lens[k] = x_len;
argv_types[k] = x_type;
k++;
buffer = silc_command_payload_encode(cmd, k, argv, argv_lens,
argv_types, ident);
+ out:
for (i = 0; i < k; i++)
silc_free(argv[i]);
silc_free(argv);
unsigned char *x;
SilcUInt32 x_len;
SilcUInt32 x_type;
- SilcBuffer buffer;
+ SilcBuffer buffer = NULL;
int i, k;
argc++;
argv = silc_calloc(argc, sizeof(unsigned char *));
+ if (!argv)
+ return NULL;
argv_lens = silc_calloc(argc, sizeof(SilcUInt32));
+ if (!argv_lens) {
+ silc_free(argv);
+ return NULL;
+ }
argv_types = silc_calloc(argc, sizeof(SilcUInt32));
+ if (!argv_types) {
+ silc_free(argv_lens);
+ silc_free(argv);
+ return NULL;
+ }
SILC_PUT16_MSB(status, status_data);
argv[0] = silc_memdup(status_data, sizeof(status_data));
+ if (!argv[0]) {
+ silc_free(argv_types);
+ silc_free(argv_lens);
+ silc_free(argv);
+ return NULL;
+ }
argv_lens[0] = sizeof(status_data);
argv_types[0] = 1;
continue;
argv[k] = silc_memdup(x, x_len);
+ if (!argv[k])
+ goto out;
argv_lens[k] = x_len;
argv_types[k] = x_type;
k++;
buffer = silc_command_payload_encode(cmd, k, argv, argv_lens,
argv_types, ident);
+ out:
for (i = 0; i < k; i++)
silc_free(argv[i]);
silc_free(argv);
SilcUInt32 payload_len)
{
SilcBufferStruct buffer;
- SilcIDPayload new;
+ SilcIDPayload newp;
int ret;
SILC_LOG_DEBUG(("Parsing ID payload"));
silc_buffer_set(&buffer, (unsigned char *)payload, payload_len);
- new = silc_calloc(1, sizeof(*new));
+ newp = silc_calloc(1, sizeof(*newp));
+ if (!newp)
+ return NULL;
ret = silc_buffer_unformat(&buffer,
- SILC_STR_UI_SHORT(&new->type),
- SILC_STR_UI_SHORT(&new->len),
+ SILC_STR_UI_SHORT(&newp->type),
+ SILC_STR_UI_SHORT(&newp->len),
SILC_STR_END);
if (ret == -1)
goto err;
silc_buffer_pull(&buffer, 4);
- if (new->len > buffer.len)
+ if (newp->len > buffer.len)
goto err;
ret = silc_buffer_unformat(&buffer,
- SILC_STR_UI_XNSTRING_ALLOC(&new->id, new->len),
+ SILC_STR_UI_XNSTRING_ALLOC(&newp->id, newp->len),
SILC_STR_END);
if (ret == -1)
goto err;
silc_buffer_push(&buffer, 4);
- return new;
+ return newp;
err:
- silc_free(new);
+ silc_free(newp);
return NULL;
}
type == SILC_ID_CLIENT ? "Client" :
type == SILC_ID_SERVER ? "Server" : "Channel"));
- buffer = silc_buffer_alloc(4 + id_len);
- silc_buffer_pull_tail(buffer, SILC_BUFFER_END(buffer));
+ buffer = silc_buffer_alloc_size(4 + id_len);
+ if (!buffer)
+ return NULL;
silc_buffer_format(buffer,
SILC_STR_UI_SHORT(type),
SILC_STR_UI_SHORT(id_len),
case SILC_ID_SERVER:
server_id = (SilcServerID *)id;
ret_id = silc_calloc(id_len, sizeof(unsigned char));
+ if (!ret_id)
+ return NULL;
memcpy(ret_id, server_id->ip.data, server_id->ip.data_len);
SILC_PUT16_MSB(server_id->port, &ret_id[4]);
SILC_PUT16_MSB(server_id->rnd, &ret_id[6]);
case SILC_ID_CLIENT:
client_id = (SilcClientID *)id;
ret_id = silc_calloc(id_len, sizeof(unsigned char));
+ if (!ret_id)
+ return NULL;
memcpy(ret_id, client_id->ip.data, client_id->ip.data_len);
ret_id[4] = client_id->rnd;
memcpy(&ret_id[5], client_id->hash, CLIENTID_HASH_LEN);
case SILC_ID_CHANNEL:
channel_id = (SilcChannelID *)id;
ret_id = silc_calloc(id_len, sizeof(unsigned char));
+ if (!ret_id)
+ return NULL;
memcpy(ret_id, channel_id->ip.data, channel_id->ip.data_len);
SILC_PUT16_MSB(channel_id->port, &ret_id[4]);
SILC_PUT16_MSB(channel_id->rnd, &ret_id[6]);
return NULL;
server_id = silc_calloc(1, sizeof(*server_id));
+ if (!server_id)
+ return NULL;
memcpy(server_id->ip.data, id, (id_len > ID_SERVER_LEN_PART + 4 ?
16 : 4));
server_id->ip.data_len = (id_len > ID_SERVER_LEN_PART + 4 ? 16 : 4);
return NULL;
client_id = silc_calloc(1, sizeof(*client_id));
+ if (!client_id)
+ return NULL;
memcpy(client_id->ip.data, id, (id_len > ID_CLIENT_LEN_PART + 4 ?
16 : 4));
client_id->ip.data_len = (id_len > ID_CLIENT_LEN_PART + 4 ? 16 : 4);
return NULL;
channel_id = silc_calloc(1, sizeof(*channel_id));
+ if (!channel_id)
+ return NULL;
memcpy(channel_id->ip.data, id, (id_len > ID_CHANNEL_LEN_PART + 4 ?
16 : 4));
channel_id->ip.data_len = (id_len > ID_CHANNEL_LEN_PART + 4 ? 16 : 4);
SILC_LOG_DEBUG(("Allocating new cache"));
cache = silc_calloc(1, sizeof(*cache));
+ if (!cache)
+ return NULL;
cache->id_table = silc_hash_table_alloc(count, silc_hash_id,
(void *)(SilcUInt32)id_type,
silc_hash_id_compare,
cache->destructor = destructor;
cache->type = id_type;
+ if (!cache->id_table || !cache->name_table || !cache->context_table) {
+ if (cache->id_table)
+ silc_hash_table_free(cache->id_table);
+ if (cache->name_table)
+ silc_hash_table_free(cache->name_table);
+ if (cache->context_table)
+ silc_hash_table_free(cache->context_table);
+ silc_free(cache);
+ return NULL;
+ }
+
return cache;
}
/* Allocate new cache entry */
c = silc_calloc(1, sizeof(*c));
+ if (!c)
+ return FALSE;
c->id = id;
c->name = name;
c->expire = expire;
return TRUE;
list = silc_idcache_list_alloc();
+ if (!list)
+ return FALSE;
silc_hash_table_foreach(cache->id_table, silc_idcache_get_all_foreach, list);
if (silc_idcache_list_count(list) == 0) {
SilcIDCacheList list;
list = silc_idcache_list_alloc();
+ if (!list)
+ return FALSE;
if (!ret)
return TRUE;
SilcIDCacheList list;
list = silc_idcache_list_alloc();
+ if (!list)
+ return FALSE;
if (!ret)
return TRUE;
SilcIDCacheList list;
list = silc_calloc(1, sizeof(*list));
+ if (!list)
+ return FALSE;
return list;
}
i = list->cache_dyn_count;
list->cache_dyn = silc_realloc(list->cache_dyn,
sizeof(*list->cache_dyn) * (i + 5));
+ if (!list->cache_dyn)
+ return;
/* NULL the reallocated area */
for (k = i; k < (i + 5); k++)
SilcUInt32 payload_len)
{
SilcBufferStruct buffer;
- SilcNotifyPayload new;
+ SilcNotifyPayload newp;
SilcUInt16 len;
int ret;
SILC_LOG_DEBUG(("Parsing Notify payload"));
silc_buffer_set(&buffer, (unsigned char *)payload, payload_len);
- new = silc_calloc(1, sizeof(*new));
+ newp = silc_calloc(1, sizeof(*newp));
+ if (!newp)
+ return NULL;
ret = silc_buffer_unformat(&buffer,
- SILC_STR_UI_SHORT(&new->type),
+ SILC_STR_UI_SHORT(&newp->type),
SILC_STR_UI_SHORT(&len),
- SILC_STR_UI_CHAR(&new->argc),
+ SILC_STR_UI_CHAR(&newp->argc),
SILC_STR_END);
if (ret == -1)
goto err;
if (len > buffer.len)
goto err;
- if (new->argc) {
+ if (newp->argc) {
silc_buffer_pull(&buffer, 5);
- new->args = silc_argument_payload_parse(buffer.data, buffer.len,
- new->argc);
+ newp->args = silc_argument_payload_parse(buffer.data, buffer.len,
+ newp->argc);
silc_buffer_push(&buffer, 5);
}
- return new;
+ return newp;
err:
- silc_free(new);
+ silc_free(newp);
return NULL;
}
if (argc) {
argv = silc_calloc(argc, sizeof(unsigned char *));
+ if (!argv)
+ return NULL;
argv_lens = silc_calloc(argc, sizeof(SilcUInt32));
+ if (!argv_lens) {
+ silc_free(argv);
+ return NULL;
+ }
argv_types = silc_calloc(argc, sizeof(SilcUInt32));
+ if (!argv_types) {
+ silc_free(argv_lens);
+ silc_free(argv);
+ return NULL;
+ }
for (i = 0, k = 0; i < argc; i++) {
x = va_arg(ap, unsigned char *);
continue;
argv[k] = silc_memdup(x, x_len);
+ if (!argv[k])
+ return NULL;
argv_lens[k] = x_len;
argv_types[k] = i + 1;
k++;
}
len += 5;
- buffer = silc_buffer_alloc(len);
- silc_buffer_pull_tail(buffer, SILC_BUFFER_END(buffer));
+ buffer = silc_buffer_alloc_size(len);
+ if (!buffer)
+ return NULL;
silc_buffer_format(buffer,
SILC_STR_UI_SHORT(type),
SILC_STR_UI_SHORT(len),
int len;
len = 5 + (args ? args->len : 0);
- buffer = silc_buffer_alloc(len);
- silc_buffer_pull_tail(buffer, SILC_BUFFER_END(buffer));
+ buffer = silc_buffer_alloc_size(len);
+ if (!buffer)
+ return NULL;
silc_buffer_format(buffer,
SILC_STR_UI_SHORT(type),
SILC_STR_UI_SHORT(len),
SILC_UNSET_INBUF_PENDING(sock);
parse_ctx = silc_calloc(1, sizeof(*parse_ctx));
+ if (!parse_ctx)
+ return FALSE;
parse_ctx->packet = silc_packet_context_alloc();
parse_ctx->packet->buffer = silc_buffer_alloc(paddedlen + mac_len);
parse_ctx->packet->type = sock->inbuf->data[3];
SilcPacketContext *silc_packet_context_alloc(void)
{
SilcPacketContext *ctx = silc_calloc(1, sizeof(*ctx));
+ if (!ctx)
+ return NULL;
ctx->users++;
return ctx;
}
SilcCipher cipher)
{
SilcBufferStruct buffer;
- SilcPrivateMessagePayload new;
+ SilcPrivateMessagePayload newp;
int ret;
SILC_LOG_DEBUG(("Parsing private message payload"));
silc_cipher_decrypt(cipher, buffer.data, buffer.data,
buffer.len, cipher->iv);
- new = silc_calloc(1, sizeof(*new));
+ newp = silc_calloc(1, sizeof(*newp));
+ if (!newp)
+ return NULL;
/* Parse the Private Message Payload. Ignore the padding. */
ret = silc_buffer_unformat(&buffer,
- SILC_STR_UI_SHORT(&new->flags),
- SILC_STR_UI16_NSTRING_ALLOC(&new->message,
- &new->message_len),
+ SILC_STR_UI_SHORT(&newp->flags),
+ SILC_STR_UI16_NSTRING_ALLOC(&newp->message,
+ &newp->message_len),
SILC_STR_END);
if (ret == -1) {
SILC_LOG_DEBUG(("Incorrect private message payload"));
goto err;
}
- if ((new->message_len < 1 || new->message_len > buffer.len)) {
+ if ((newp->message_len < 1 || newp->message_len > buffer.len)) {
SILC_LOG_DEBUG(("Incorrect private message payload in packet, "
"packet dropped"));
goto err;
}
- return new;
+ return newp;
err:
- silc_private_message_payload_free(new);
+ silc_private_message_payload_free(newp);
return NULL;
}
}
/* Allocate private message payload buffer */
- buffer = silc_buffer_alloc(len);
+ buffer = silc_buffer_alloc_size(len);
+ if (!buffer)
+ return NULL;
/* Encode the Channel Message Payload */
- silc_buffer_pull_tail(buffer, SILC_BUFFER_END(buffer));
silc_buffer_format(buffer,
SILC_STR_UI_SHORT(flags),
SILC_STR_UI_SHORT(data_len),
/* Allocate new SilcBuffer */
sb = (SilcBuffer)silc_calloc(1, sizeof(*sb));
+ if (!sb)
+ return NULL;
/* Allocate the actual data area */
sb->head = (unsigned char *)silc_calloc(len, sizeof(*sb->head));
+ if (!sb->head)
+ return NULL;
/* Set pointers to the new buffer */
sb->truelen = len;
return (unsigned char *)memcpy(sb->tail, data, len);
}
+/* Allocates `len' bytes size buffer and moves the tail area automaticlly
+ `len' bytes so that the buffer is ready to use without calling the
+ silc_buffer_pull_tail. */
+
+static inline
+SilcBuffer silc_buffer_alloc_size(SilcUInt32 len)
+{
+ SilcBuffer sb = silc_buffer_alloc(len);
+ if (!sb)
+ return NULL;
+ silc_buffer_pull_tail(sb, len);
+ return sb;
+}
+
/* Clears and initialiazes the buffer to the state as if it was just
allocated by silc_buffer_alloc. */
{
SilcBuffer sb_new;
- sb_new = silc_buffer_alloc(sb->len);
- silc_buffer_pull_tail(sb_new, SILC_BUFFER_END(sb_new));
+ sb_new = silc_buffer_alloc_size(sb->len);
+ if (!sb_new)
+ return NULL;
silc_buffer_put(sb_new, sb->data, sb->len);
return sb_new;
{
SilcBuffer sb_new;
- sb_new = silc_buffer_alloc(sb->truelen);
- silc_buffer_pull_tail(sb_new, SILC_BUFFER_END(sb_new));
+ sb_new = silc_buffer_alloc_size(sb->truelen);
+ if (!sb_new)
+ return NULL;
silc_buffer_put(sb_new, sb->head, sb->truelen);
sb_new->data = sb_new->head + (sb->data - sb->head);
sb_new->tail = sb_new->data + sb->len;
if (newsize <= sb->truelen)
return sb;
- sb_new = silc_buffer_alloc(newsize);
- silc_buffer_pull_tail(sb_new, SILC_BUFFER_END(sb_new));
+ sb_new = silc_buffer_alloc_size(newsize);
+ if (!sb_new)
+ return NULL;
silc_buffer_put(sb_new, sb->head, sb->truelen);
sb_new->data = sb_new->head + (sb->data - sb->head);
sb_new->tail = sb_new->data + sb->len;