3

I should get stack smashing error here . What is the reason I am not getting it?

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

struct mun
{
    int len;
    char str[0];

};

int main(void)
{

    //char mp[8];
    struct mun *p=malloc(sizeof(struct mun)+2);
    p->len=8;
    strcpy(p->str,"munjalllfff");
    //strcpy(mp,"munjalllfff");

    printf("%s\n",p->str);
    //printf("%s\n",mp);

    return 0;
}

Please explain if possible or (a topic name or link will enough for me.)

Jens
  • 69,818
  • 15
  • 125
  • 179
munjal007
  • 245
  • 1
  • 2
  • 6

4 Answers4

12

Most C implementations won't bother to protect the stack or heap from being overwritten by a few bytes only. (There's a library aptly named Electric Fence that can do so). Chances are, if you write enough data, you'll eventually write beyond the allowed address space and the program crashes in one way or another (this depends on many factors, like OS, compiler, options). As you noticed by now, this answer is very vague. The reason is that what you do is technically called undefined behavior by the C Standard, which means the implementation is free to do anything, including nothing.

Why is it so? Why doesn't the C Standard have a clause saying

3.1.4.1.5 When an access outside of allocated memory is attempted, a statement equivalent to fprintf(stderr, "illegal access at %p\n", (void *)address); shall be executed.

The reason is that this would place a heavy burden on the implementation. The compiler probably would have to generate code to check for illegal accesses after almost all pointer modifications and function calls. C is, by design, a tiny language where programmers get mostly what they ask for and no "invisible code" in addition.

And then, stderr may be closed or non-existent :-)

Jens
  • 69,818
  • 15
  • 125
  • 179
11

You're invoking undefined behavior. Anything could happen. If you're lucky it will crash, if you're unlucky it will sign you up for Google+.

John Zwinck
  • 239,568
  • 38
  • 324
  • 436
  • 2
    While true, this is completely unhelpful. [Jens's answer](http://stackoverflow.com/questions/26083866/why-i-am-not-getting-stack-smashing-error-here/26083930#26083930) makes the same point, but he explains what that means: that is a good answer. – Gilles 'SO- stop being evil' Sep 28 '14 at 10:27
  • 1
    @Gilles On the other hand, this answer makes OP to make research about UB. I'm sure this is the better way to learn things. – kotlomoy Sep 28 '14 at 10:46
1

This is not on the stack, use free(p) and you may see some errors!

Danny Birch
  • 603
  • 4
  • 16
0

This is clearly Undefined behavior. It may work and may not!

To avoid it you should use strncpy() while copying the string.

strncpy(p->str,"munjalllfff",sizeof(p->str));

Also don't forget to free() the memory you have allocated using malloc().

ani627
  • 5,578
  • 8
  • 39
  • 45
  • While true, this is completely unhelpful. [Jens's answer](http://stackoverflow.com/questions/26083866/why-i-am-not-getting-stack-smashing-error-here/26083930#26083930) makes the same point, but he explains what that means: that is a good answer. – Gilles 'SO- stop being evil' Sep 28 '14 at 10:27
  • @Gilles: Add more information. I hope it will be helpful for OP. – ani627 Sep 28 '14 at 10:48