Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #15125

Re: Memory leaking in memmove

From Barry Schwarz <schwarzb@dqel.com>
Newsgroups comp.lang.c
Subject Re: Memory leaking in memmove
Date 2011-12-28 10:36 -0800
Organization Aioe.org NNTP Server
Message-ID <p0mmf718v78395o7h06ak3ut4krp83fnra@4ax.com> (permalink)
References <15848343.42.1325064299310.JavaMail.geo-discussion-forums@prdv33>

Show all headers | View raw


On Wed, 28 Dec 2011 01:24:59 -0800 (PST), ArifulHossain tuhin
<etothepowerpi@gmail.com> wrote:

>My application is leaking memory in a memmove call. I have made it sure from coredumps. 
>
>the offending code snippet reads as follows:
>unsigned char pad_len;
>pad_len = (unsigned char)((random() % RANSTOP ) + RANSTART);

Are you using the cast to suppress an over-zealous diagnostic about
conversion between signed and unsigned or about int to char?  If not,
it serves no purpose.

In a subsequent message, you stated RANSTOP was 3 and RANSTART was 9.
random() is not a standard function but if it works like rand() than
the first addend will be in the range of 0 to 2.  Even if random()
produces negative values, the range would be -2 to 2.  Consequently
the sum must be in the range 9 to 11 (or 7 to 11).

>if(pad_len%30 == 0)

So when will this ever evaluate to true?

>pad_len = 9;

Even without braces, indenting is still a good idea.

>if(packet->size > 0){

What is the initial value of size?

>	memmove(packet->data.buf + pad_len, packet->data.buf, packet->size);

Is this the only call to memmove in your program?  If this is indeed
the statement causing the seg fault, you need to look at the current
values of pad_len and size.  Since pad_len <= 11, size is probably the
real culprit.

>	packet->size += pad_len;

If this block is in a loop that does not terminate as expected, it is
quite reasonable for size to grow larger than expected.

>	packet->data.buf[0] = pad_len;
>	for(j=1; j< pad_len; j++)
>	packet->data.buf[j] = (char)256%j;

This would not silence the diagnostic I mentioned above.  The division
is performed on int values so (char)256 is promoted.  The result is an
int which could generate either the signed->unsigned or int->char
complaints.

Changing it to (char)(256%j) could still lead to the signed->unsigned
diagnostic.

For the cast to have any value, it needs to be (unsigned char)(256%j).
Since the existing code apparently did not cause any of the
diagnostics which would prompt this, I have to assume this cast is
pointless also.

>	EnDeCrypt(packet->data.buf, packet->size, key, KEYLEN);

I don't know if it matters, but 256%j will generate multiple instances
of 0 in various elements of buf.

>}
>
>the packet data structure is decalared as follows:
>
>struct rtp_packet {
>    size_t      size;
>    ................
>    (some fields)
>    ................
>    struct rtp_packet *next;
>    struct rtp_packet *prev;
>    union {
>	rtp_hdr_t       header;
>	unsigned char   buf[8192];
>    } data;
>}; 
>
>As the data.buf is statically allocated, valgrind does not complain anything about it.
>Is it running out of memory? or any other problem?
>Any suggestion?

-- 
Remove del for email

Back to comp.lang.c | Previous | Next — Previous in thread | Find similar | Unroll thread


Thread

Memory leaking in memmove ArifulHossain tuhin <etothepowerpi@gmail.com> - 2011-12-28 01:24 -0800
  Re: Memory leaking in memmove Nick Keighley <nick_keighley_nospam@hotmail.com> - 2011-12-28 02:20 -0800
    Re: Memory leaking in memmove ArifulHossain tuhin <etothepowerpi@gmail.com> - 2011-12-28 02:37 -0800
      Re: Memory leaking in memmove Philip Lantz <prl@canterey.us> - 2011-12-28 02:42 -0800
      Re: Memory leaking in memmove Nick Keighley <nick_keighley_nospam@hotmail.com> - 2011-12-28 03:36 -0800
        Re: Memory leaking in memmove ArifulHossain tuhin <etothepowerpi@gmail.com> - 2011-12-28 03:50 -0800
      Re: Memory leaking in memmove Seebs <usenet-nospam@seebs.net> - 2011-12-28 18:17 +0000
  Re: Memory leaking in memmove Philip Lantz <prl@canterey.us> - 2011-12-28 02:36 -0800
    Re: Memory leaking in memmove ArifulHossain tuhin <etothepowerpi@gmail.com> - 2011-12-28 03:57 -0800
      Re: Memory leaking in memmove Dr Nick <3-nospam@temporary-address.org.uk> - 2011-12-28 12:21 +0000
        Re: Memory leaking in memmove ArifulHossain tuhin <etothepowerpi@gmail.com> - 2011-12-28 04:26 -0800
      Re: Memory leaking in memmove jt@toerring.de (Jens Thoms Toerring) - 2011-12-28 12:46 +0000
  Re: Memory leaking in memmove ArifulHossain tuhin <etothepowerpi@gmail.com> - 2011-12-28 05:54 -0800
  Re: Memory leaking in memmove Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-12-28 14:46 +0000
    Re: Memory leaking in memmove Philip Lantz <prl@canterey.us> - 2011-12-28 12:20 -0800
      Re: Memory leaking in memmove Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-12-28 21:50 +0000
  Re: Memory leaking in memmove Barry Schwarz <schwarzb@dqel.com> - 2011-12-28 10:36 -0800

csiph-web