sbrk(2) question

der Mouse mouse at thunder.mcrcim.mcgill.edu
Fri Mar 15 20:53:40 AEST 1991


In article <2045 at necisa.ho.necisa.oz.au>, boyd at necisa.ho.necisa.oz.au (Boyd Roberts) writes:
> In article <4312 at skye.ed.ac.uk> richard at aiai.UUCP (Richard Tobin) writes:
>> While experimenting, I was reminded of another hazard of sbrk().
>> For example, this program produces a segmentation fault on a Sun4
>> under SunOS 4.1:

[edited -dM]
>>        sbrk(100000);
>>        printf("hello world\n");
>>        sbrk(-100000);
>>        printf("hello again\n");

> Well, I thought everybody new that sbrk(2) and malloc(3) just don't
> mix.  Odds on printf(3) calls malloc and gets some memory whose
> address is > sbrk(0) + 100000*.  The second printf references this
> memory after the second sbrk and it's length violation city!

You are correct, of course, but on SunOS 4.1 it's even worse than this.

Apparently Sun decided that nobody wants to use sbrk and malloc in the
same program.  malloc and sbrk are incompatble.  Even when all
arguments to sbrk are positive multiples of the page size, you *still*
get a core dump when you mix sbrk() and malloc()!  I don't know what
possessed them to introduce this bug; I guess it's just another
instance of Sun deciding they didn't care about compatibility with the
rest of the world.

I don't know why they even bothered to provide sbrk(), because nearly
everything winds up using some library routine or other which
eventually calls malloc().

(If I sound annoyed, it's because I am.  I spent days trying to track
down that core dump....)

					der Mouse

			old: mcgill-vision!mouse
			new: mouse at larry.mcrcim.mcgill.edu



More information about the Comp.unix.questions mailing list