Re: Jemalloc SEGV for 1MB chunk

From: Channa <channa.kad_at_gmail.com>
Date: Wed, 28 Jan 2009 17:55:23 +0530
Hi,
Thanks for the reply.

I understand , after terminating the string with NULL character no SEGV is seen.

But if i change the request size to a value less than 1MB for eg: 4096 Bytes,

As in the below test code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
int i;
char *buf;
size_t size = 4096  ;

        buf = malloc(size);
  for (i = 0; i < size; i++)
    buf[i] = 'a';
printf("The length of buff is : %d\n",strlen(buf));
free(buf);
return 0;
}

I dont see any issues, without terminating the string with NULL
character the test code works fine. The issue is seen only for size
1MB exactly.

Can anyone explain this behaviour?

Thanks in Advance,
Channa



On 28/01/2009, Danny Braniss <danny_at_cs.huji.ac.il> wrote:
> > Hi,
>  > Thanks for your reply.
>  > You mean to say i should modify the test as below:
>  >
>  > #include <stdio.h>
>  > #include <stdlib.h>
>  > #include <string.h>
>  >
>  >
>  > int main()
>  > {
>  > int i;
>  > char *buf;
>  > size_t size = 1048576 ;
>  >
>  >         buf = malloc(size);
>  >   for (i = 0; i <= 1048575; i++)
>  >     buf[i] = 'a';
>  > buf[size]='\0';
>  > printf("The length of buff is : %d\n",strlen(buf));
>  > free(buf);
>  > return 0;
>  > }
>  >
>  > I NULL terminated the string
>  > buf[size] = '\0'  <== The last character is NULL
>  >
>  > But still i get a SEGV at strlen.
>  >
>  > Could you please tell me if my changes above are correct?
>  >
>
> clear case of off by one.
>  you are requesting 'size' bytes, indexing starts at 0, all the way to size-1
>  which is ALL the bytes you malloc'ed
>  then you zero the size+1 byte, ah, btw, it's not strlen that is SEGV'ing.
>
>
>  danny
>
>
>  > Regards,
>  > Channa
>  >
>  >
>  > On 28/01/2009, Christoph Mallon <christoph.mallon_at_gmx.de> wrote:
>  > > Channa schrieb:
>  > >
>  > >
>  > > > Hi All,
>  > > > I am using jemalloc.c source from FreeBSD-current source.
>  > > > When i allocate 1MB of memory using malloc() and use it as the below
>  > > > test shows
>  > > >
>  > > > #include <stdio.h>
>  > > > #include <stdlib.h>
>  > > > #include <string.h>
>  > > >
>  > > > int main()
>  > > > {
>  > > >        int i;
>  > > >        char *buf;
>  > > >        size_t size = 1048576 ;
>  > > >
>  > > >        buf = malloc(size);
>  > > >        for (i = 0; i < 1048576; i++)
>  > > >                buf[i] = 'a';
>  > > >        printf("The length of buff is : %d\n",strlen(buf));
>  > > >        free(buf);
>  > > >        return 0;
>  > > > }
>  > > >
>  > > > When i try to call strlen(buf) SEGV is recived.
>  > > >
>  > > > This behaviour is seen when only for 1MB chunk if i allocate
>  > > > memory lesser than 1MB no issues noticed.
>  > > >
>  > > > Could anyone see similar problem?
>  > > > Is the above test wrong?
>  > > > Or some issue with huge memory allocation in jemalloc?
>  > > >
>  > > > Your response will be very helpful.
>  > > >
>  > > > Thanks & Regards,
>  > > > Channa
>  > > >
>  > >
>  > >  You did not NUL-terminate ('\0') the string.
>  > >
>
> > _______________________________________________
>  > freebsd-current_at_freebsd.org mailing list
>  > http://lists.freebsd.org/mailman/listinfo/freebsd-current
>  > To unsubscribe, send any mail to "freebsd-current-unsubscribe_at_freebsd.org"
>  >
>
>
>
Received on Wed Jan 28 2009 - 11:25:24 UTC

This archive was generated by hypermail 2.4.0 : Wed May 19 2021 - 11:39:41 UTC