public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* malloc(1/0) ??
@ 2000-11-07  3:59 RAJESH BALAN
  2000-11-07  7:54 ` David Schwartz
  2000-11-08  0:41 ` Igmar Palsenberg
  0 siblings, 2 replies; 23+ messages in thread
From: RAJESH BALAN @ 2000-11-07  3:59 UTC (permalink / raw)
  To: linux-kernel

hi,
why does this program works. when executed, it doesnt
give a segmentation fault. when the program requests
memory, is a standard chunk is allocated irrespective
of the what the user specifies. please explain.
 
main()
{
   char *s;
   s = (char*)malloc(0);
   strcpy(s,"fffff");
   printf("%s\n",s);
}

NOTE:
  i know its a 'C' problem. but i wanted to know how
this works 


__________________________________________________
Do You Yahoo!?
Thousands of Stores.  Millions of Products.  All in one Place.
http://shopping.yahoo.com/
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
Please read the FAQ at http://www.tux.org/lkml/

^ permalink raw reply	[flat|nested] 23+ messages in thread
* Re: malloc(1/0) ??
@ 2000-11-07  6:45 Dan Kegel
  2000-11-07  7:13 ` J. Dow
  0 siblings, 1 reply; 23+ messages in thread
From: Dan Kegel @ 2000-11-07  6:45 UTC (permalink / raw)
  To: atmproj, linux-kernel

atmproj@yahoo.com asked:
> [Why does this program not crash?]
>
> main() 
> { 
>    char *s; 
>    s = (char*)malloc(0); 
>    strcpy(s,"fffff"); 
>    printf("%s\n",s); 
> } 

It doesn't crash because the standard malloc is
optimized for speed, not for finding bugs.

Try linking it with a debugging malloc, e.g.
  cc bug.c -lefence
and watch it dump core.

- Dan
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
Please read the FAQ at http://www.tux.org/lkml/

^ permalink raw reply	[flat|nested] 23+ messages in thread
* RE: malloc (1/0) ??
@ 2000-11-07  9:26 David Feuer
  0 siblings, 0 replies; 23+ messages in thread
From: David Feuer @ 2000-11-07  9:26 UTC (permalink / raw)
  To: linux-kernel

As long as you don't try to do any more mm once you've allocated with 
malloc(0), and as long as you haven't done any previous allocations with 
malloc, you should be able to scribble all over malloc.  In fact, if you 
want, I think you can scribble all over your own stack without causing 
Linux any trouble.

I'm guessing (and this is only an educated guess), that you could do some 
really strange things like

void scribble(void)
{
int x[50];
do_scribble(x);
}
void do_scribble(int *x)
{
char y[50];
x[70]=54;
x[71]=32;
x[50]=3;x[51]=12;  /* watch out */
}
void main(void)
{
scribble();
}


Depending how the storage structure works for your C compiler (sorry, I 
don't remember), this COULD scribble integers onto your character 
array.  The line marked "watch out" COULD severely scribble the return 
pointer and make the program crash in really ugly ways. Alternatively, it 
might not.  Depends how you stack it.


As a less severe example, if you want, you can do something really funky like

x=(char *)malloc(100);
x=(char *)realloc (x,50);
y=x+50;  /*could be a fencepost error: not worth my time to check*/


Writing to y will scribble on malloc's territory, but as long as you don't 
call malloc again, you should be fine.  This way you can get any amount of 
scribble space.  Of course, this only works on normal versions of malloc 
that don't try to return memory to the OS, etc.
--
This message has been brought to you by the letter alpha and the number pi.
David Feuer
David_Feuer@brown.edu

-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
Please read the FAQ at http://www.tux.org/lkml/

^ permalink raw reply	[flat|nested] 23+ messages in thread
* RE: malloc(1/0) ??
@ 2000-11-07 16:12 Jesse Pollard
  2000-11-07 16:38 ` lost
  0 siblings, 1 reply; 23+ messages in thread
From: Jesse Pollard @ 2000-11-07 16:12 UTC (permalink / raw)
  To: davids, RAJESH BALAN, linux-kernel


> 
> > hi,
> > why does this program works. when executed, it doesnt
> > give a segmentation fault. when the program requests
> > memory, is a standard chunk is allocated irrespective
> > of the what the user specifies. please explain.
> >
> > main()
> > {
> >    char *s;
> >    s = (char*)malloc(0);
> >    strcpy(s,"fffff");
> >    printf("%s\n",s);
> > }
> >
> > NOTE:
> >   i know its a 'C' problem. but i wanted to know how
> > this works
> 
> 	The program does not work. A program works if it does what it's supposed to
> do. If you want to argue that this program is supposed to print "ffffff"
> then explain to me why the 'malloc' contains a zero in parenthesis.
> 
> 	The program can't possibly work because it invokes undefined behavior. It
> is impossible to determine what a program that invokes undefined behavior is
> 'supposed to do'.

All true, but the reason it "works" is that malloc WILL allocate some memory,
even if it's only a few bytes of header.:

       |       |   (other memory block controled by malloc/free...)
       |-------|
       | header|
       |       |    - address returned to program
       | next  |
       | header|    (next memory block...)

Now the strcpy may have copied the string "fffff" over the next header.
The copy worked, the printf worked (its buffers were already allocated...)
BUT... If you allocate more memory via malloc, you will get an error
(eventually). I believe malloc(0) allocates 4 bytes as a minimum, though
this particular call IS undefined. You also did not check to see if
malloc did return something (It did, or you would have gotten a segmentation
fault from writing to location 0 with strcpy).

-------------------------------------------------------------------------
Jesse I Pollard, II
Email: pollard@navo.hpc.mil

Any opinions expressed are solely my own.
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
Please read the FAQ at http://www.tux.org/lkml/

^ permalink raw reply	[flat|nested] 23+ messages in thread

end of thread, other threads:[~2000-11-09 14:55 UTC | newest]

Thread overview: 23+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2000-11-07  3:59 malloc(1/0) ?? RAJESH BALAN
2000-11-07  7:54 ` David Schwartz
2000-11-07  7:59   ` Andrej Hosna
2000-11-07  8:50     ` David Schwartz
2000-11-07  8:09   ` Lyle Coder
2000-11-07  8:46     ` Matti Aarnio
2000-11-08  0:29       ` Rogier Wolff
2000-11-08  0:36         ` David Schwartz
2000-11-08  0:54     ` Igmar Palsenberg
2000-11-08  0:50   ` Igmar Palsenberg
2000-11-08 22:11     ` H. Peter Anvin
2000-11-08 22:11       ` Rasmus Andersen
2000-11-09 16:03       ` Igmar Palsenberg
2000-11-08  0:41 ` Igmar Palsenberg
2000-11-07 23:58   ` Tim Waugh
2000-11-08 12:38     ` Igmar Palsenberg
  -- strict thread matches above, loose matches on Subject: below --
2000-11-07  6:45 Dan Kegel
2000-11-07  7:13 ` J. Dow
2000-11-07  7:52   ` David Schwartz
2000-11-08  0:47   ` Igmar Palsenberg
2000-11-07  9:26 malloc (1/0) ?? David Feuer
2000-11-07 16:12 malloc(1/0) ?? Jesse Pollard
2000-11-07 16:38 ` lost

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox