Commit cf73548b authored by Niels Möller's avatar Niels Möller
Browse files

(xalloc): New function. Made all other

functions use xalloc instead of alloca.

Rev: src/nettle/testsuite/testutils.c:1.24
Rev: src/nettle/testsuite/testutils.h:1.20
parent e99a1c24
...@@ -34,6 +34,19 @@ static const signed char hex_digits[0x100] = ...@@ -34,6 +34,19 @@ static const signed char hex_digits[0x100] =
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1
}; };
void *
xalloc(size_t size)
{
void *p = malloc(size);
if (!p)
{
fprintf(stderr, "Virtual memory exhausted.\n");
abort();
}
return p;
}
unsigned unsigned
decode_hex_length(const char *h) decode_hex_length(const char *h)
{ {
...@@ -95,9 +108,7 @@ decode_hex_dup(const char *hex) ...@@ -95,9 +108,7 @@ decode_hex_dup(const char *hex)
uint8_t *p; uint8_t *p;
unsigned length = decode_hex_length(hex); unsigned length = decode_hex_length(hex);
p = malloc(length); p = xalloc(length);
if (!p)
abort();
if (decode_hex(p, hex)) if (decode_hex(p, hex))
return p; return p;
...@@ -166,8 +177,8 @@ test_cipher(const struct nettle_cipher *cipher, ...@@ -166,8 +177,8 @@ test_cipher(const struct nettle_cipher *cipher,
const uint8_t *cleartext, const uint8_t *cleartext,
const uint8_t *ciphertext) const uint8_t *ciphertext)
{ {
void *ctx = alloca(cipher->context_size); void *ctx = xalloc(cipher->context_size);
uint8_t *data = alloca(length); uint8_t *data = xalloc(length);
cipher->set_encrypt_key(ctx, key_length, key); cipher->set_encrypt_key(ctx, key_length, key);
cipher->encrypt(ctx, length, data, cleartext); cipher->encrypt(ctx, length, data, cleartext);
...@@ -180,6 +191,9 @@ test_cipher(const struct nettle_cipher *cipher, ...@@ -180,6 +191,9 @@ test_cipher(const struct nettle_cipher *cipher,
if (!MEMEQ(length, data, cleartext)) if (!MEMEQ(length, data, cleartext))
FAIL(); FAIL();
free(ctx);
free(data);
} }
void void
...@@ -191,9 +205,9 @@ test_cipher_cbc(const struct nettle_cipher *cipher, ...@@ -191,9 +205,9 @@ test_cipher_cbc(const struct nettle_cipher *cipher,
const uint8_t *ciphertext, const uint8_t *ciphertext,
const uint8_t *iiv) const uint8_t *iiv)
{ {
void *ctx = alloca(cipher->context_size); void *ctx = xalloc(cipher->context_size);
uint8_t *data = alloca(length); uint8_t *data = xalloc(length);
uint8_t *iv = alloca(cipher->block_size); uint8_t *iv = xalloc(cipher->block_size);
cipher->set_encrypt_key(ctx, key_length, key); cipher->set_encrypt_key(ctx, key_length, key);
memcpy(iv, iiv, cipher->block_size); memcpy(iv, iiv, cipher->block_size);
...@@ -214,6 +228,10 @@ test_cipher_cbc(const struct nettle_cipher *cipher, ...@@ -214,6 +228,10 @@ test_cipher_cbc(const struct nettle_cipher *cipher,
if (!MEMEQ(length, data, cleartext)) if (!MEMEQ(length, data, cleartext))
FAIL(); FAIL();
free(ctx);
free(data);
free(iv);
} }
void void
...@@ -226,8 +244,8 @@ test_cipher_stream(const struct nettle_cipher *cipher, ...@@ -226,8 +244,8 @@ test_cipher_stream(const struct nettle_cipher *cipher,
{ {
unsigned block; unsigned block;
void *ctx = alloca(cipher->context_size); void *ctx = xalloc(cipher->context_size);
uint8_t *data = alloca(length + 1); uint8_t *data = xalloc(length + 1);
for (block = 1; block <= length; block++) for (block = 1; block <= length; block++)
{ {
...@@ -258,6 +276,9 @@ test_cipher_stream(const struct nettle_cipher *cipher, ...@@ -258,6 +276,9 @@ test_cipher_stream(const struct nettle_cipher *cipher,
if (!MEMEQ(length, data, cleartext)) if (!MEMEQ(length, data, cleartext))
FAIL(); FAIL();
free(ctx);
free(data);
} }
void void
...@@ -266,8 +287,8 @@ test_hash(const struct nettle_hash *hash, ...@@ -266,8 +287,8 @@ test_hash(const struct nettle_hash *hash,
const uint8_t *data, const uint8_t *data,
const uint8_t *digest) const uint8_t *digest)
{ {
void *ctx = alloca(hash->context_size); void *ctx = xalloc(hash->context_size);
uint8_t *buffer = alloca(hash->digest_size); uint8_t *buffer = xalloc(hash->digest_size);
hash->init(ctx); hash->init(ctx);
hash->update(ctx, length, data); hash->update(ctx, length, data);
...@@ -287,6 +308,9 @@ test_hash(const struct nettle_hash *hash, ...@@ -287,6 +308,9 @@ test_hash(const struct nettle_hash *hash,
if (buffer[hash->digest_size - 1]) if (buffer[hash->digest_size - 1])
FAIL(); FAIL();
free(ctx);
free(buffer);
} }
void void
...@@ -296,10 +320,10 @@ test_armor(const struct nettle_armor *armor, ...@@ -296,10 +320,10 @@ test_armor(const struct nettle_armor *armor,
const uint8_t *ascii) const uint8_t *ascii)
{ {
unsigned ascii_length = strlen(ascii); unsigned ascii_length = strlen(ascii);
uint8_t *buffer = alloca(1 + ascii_length); uint8_t *buffer = xalloc(1 + ascii_length);
uint8_t *check = alloca(1 + armor->decode_length(ascii_length)); uint8_t *check = xalloc(1 + armor->decode_length(ascii_length));
void *encode = alloca(armor->encode_context_size); void *encode = xalloc(armor->encode_context_size);
void *decode = alloca(armor->decode_context_size); void *decode = xalloc(armor->decode_context_size);
unsigned done; unsigned done;
ASSERT(ascii_length ASSERT(ascii_length
...@@ -333,6 +357,11 @@ test_armor(const struct nettle_armor *armor, ...@@ -333,6 +357,11 @@ test_armor(const struct nettle_armor *armor,
if (0x55 != check[data_length]) if (0x55 != check[data_length])
FAIL(); FAIL();
free(buffer);
free(check);
free(encode);
free(decode);
} }
#if HAVE_LIBGMP #if HAVE_LIBGMP
......
...@@ -22,6 +22,9 @@ ...@@ -22,6 +22,9 @@
#include "nettle-meta.h" #include "nettle-meta.h"
void *
xalloc(size_t size);
/* Decodes a NUL-terminated hex string. */ /* Decodes a NUL-terminated hex string. */
unsigned unsigned
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment