0

I have set stack size to 2000Kb by ulimit -s 2000 and ulimit -Ss 2000 for hard limit. And in the below program i have allocated appox 2040000(510000 x 4) bytes which is less than i limited i.e,. 2048000(2000*4)bytes but i see that my program crashes! Can anybody suggest why this happens.

#include <stdio.h>
#include <malloc.h>
int main()
{
    int a[510000] = {0};
    a[510000] = 1;
    printf("%d", a[510000]);
    fflush(stdout);
    sleep(70);
}

EDIT 1: Crash is not because of the array index out of bound as i tried lower index and still crashes. This happens only when i limit by ulimit.

pa1
  • 778
  • 3
  • 11
  • 26

3 Answers3

2

The problem here is, in below mentioned statements

  a[510000] = 1;
  printf("%d", a[510000]);

you're having off-by-one index. The above statements are accessing array out of bounds. This in turn invokes undefined behaviour. One of the side effects of UB, other than getting a nasal demon is segmentation fault (The "Crash!!").

Remember, C uses 0-based array indexing.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • Hi, i am aware of that. so I tried this #include int main() { int a[510000] = {0}; a[51000] = 1; printf("%d", a[51000]); fflush(stdout); sleep(70); } and it crashes. Note: not crashing when not limiting by ulimit! – pa1 Jul 08 '15 at 08:16
2

int a[510000] will be an array with index from 0 to 509999. a[510000] is outside the array range.

LPs
  • 16,045
  • 8
  • 30
  • 61
  • Hi, Please check EDIT 1 in description. – pa1 Jul 08 '15 at 08:20
  • 1
    @Coder Did you try to change the stack size? I guess to 4000 kbytes. – LPs Jul 08 '15 at 08:51
  • Yes, I found that with 4000kb limit it wasn't crashing and with 1000kb it is crashing every time but with 2000kb it crashes randomly. So i think something is getting added beyond want i am allocating, to prove this is there any way i can see the current stack usage by my program? – pa1 Jul 08 '15 at 09:55
  • What you can do is to init your whole stack at a fixed value and look if your stack get corrupted by something. – LPs Jul 08 '15 at 12:05
  • @Coder I tried your code and I see the same thing. But if I launch it as su it always run correctly. – LPs Jul 08 '15 at 12:20
  • Ok. I try to post another example to stack overflow to see if someone has an idea. Mmmm very strange. – LPs Jul 08 '15 at 12:31
  • @Coder Take a look at [THIS](http://stackoverflow.com/questions/31293068/segmentation-fault-with-ulimit-set-correctly/31293911#31293911) – LPs Jul 08 '15 at 13:31
  • Hi LPs, Using su suppresses ulimit i think plus thank you for the link, in which i have a doubt(hope i am not bothering you too much ;)), adding a break point just before main and checking the stack register(?) how do i do this? i mean how do i check the status of a register? If thats the case can i also see the current total stack usage of my application? – pa1 Jul 08 '15 at 14:46
  • @Coder If you run your app with gdb you can print all register by command info registers. – LPs Jul 08 '15 at 14:52
  • i am trying to see the current stack size by setting the breakpoint in GDB, i am looking at difference of ebp and esp registers values, is that the correct way? If not please let me know. – pa1 Jul 08 '15 at 17:42
  • Yes, it is. Usually ebp is set to esp at the start of function. – LPs Jul 09 '15 at 06:23
0

You're corrupting the stack in

a[510000] = 1;

because the last index in that array is one less than 510000. So that assignment overwrites data on the stack and once other statements try to use that data your application crashes.

Alexander Balabin
  • 2,055
  • 11
  • 13