diff options
author | Andreas Schneider <mail@cynapses.org> | 2009-04-16 14:40:47 +0000 |
---|---|---|
committer | Andreas Schneider <mail@cynapses.org> | 2009-04-16 14:40:47 +0000 |
commit | 841de3439dbab380a06a0d11d674eca7c7981055 (patch) | |
tree | 1906aa30df7cebc7f822d744e9001f28ba6a7d3f /libssh/dh.c | |
parent | 1a22d18afacea8202b482e1f1dc95a6ac5e7064f (diff) | |
download | libssh-841de3439dbab380a06a0d11d674eca7c7981055.tar.gz libssh-841de3439dbab380a06a0d11d674eca7c7981055.tar.xz libssh-841de3439dbab380a06a0d11d674eca7c7981055.zip |
Add memory error check to make_bignum_string().
git-svn-id: svn+ssh://svn.berlios.de/svnroot/repos/libssh/trunk@502 7dcaeef0-15fb-0310-b436-a5af3365683c
Diffstat (limited to 'libssh/dh.c')
-rw-r--r-- | libssh/dh.c | 43 |
1 files changed, 27 insertions, 16 deletions
diff --git a/libssh/dh.c b/libssh/dh.c index f2bd20e4..1ddbc1c2 100644 --- a/libssh/dh.c +++ b/libssh/dh.c @@ -315,27 +315,38 @@ int dh_generate_f(SSH_SESSION *session) { return 0; } -STRING *make_bignum_string(bignum num){ - STRING *ptr; - int pad=0; - unsigned int len=bignum_num_bytes(num); - unsigned int bits=bignum_num_bits(num); - /* remember if the fist bit is set, it is considered as a negative number. so 0's must be appended */ - if(!(bits%8) && bignum_is_bit_set(num,bits-1)) - pad++; +STRING *make_bignum_string(bignum num) { + STRING *ptr = NULL; + int pad = 0; + unsigned int len = bignum_num_bytes(num); + unsigned int bits = bignum_num_bits(num); + + /* Remember if the fist bit is set, it is considered as a + * negative number. So 0's must be appended */ + if (!(bits % 8) && bignum_is_bit_set(num, bits - 1)) { + pad++; + } + #ifdef DEBUG_CRYPTO - fprintf(stderr, "%d bits, %d bytes, %d padding\n", bits, len, pad); + fprintf(stderr, "%d bits, %d bytes, %d padding\n", bits, len, pad); #endif /* DEBUG_CRYPTO */ - ptr=malloc(4 + len + pad); - ptr->size=htonl(len+pad); - if(pad) - ptr->string[0]=0; + + ptr = malloc(4 + len + pad); + if (ptr == NULL) { + return NULL; + } + ptr->size = htonl(len + pad); + if (pad) { + ptr->string[0] = 0; + } + #ifdef HAVE_LIBGCRYPT - bignum_bn2bin(num,len,ptr->string+pad); + bignum_bn2bin(num, len, ptr->string + pad); #elif HAVE_LIBCRYPTO - bignum_bn2bin(num,ptr->string+pad); + bignum_bn2bin(num, ptr->string + pad); #endif - return ptr; + + return ptr; } bignum make_string_bn(STRING *string){ |