Thread: stack getting corrupted....

  1. #1
    Registered User
    Join Date
    Jun 2009
    Location
    US of A
    Posts
    305

    stack getting corrupted....

    Okay here is one of the questions that i wanted to figure out. C does not have any array bounds checking as in Java and this can many a times lead to buffer overrun or the program space getting corrupted. I have this program

    [insert]
    Code:
    
    #include <stdio.h>
    #include <stdlib.h>
    
    struct name
    {
    	char arr[20];
    	int age;
    };
    
    
    int main(void)
    {
    
    	struct name n;
    	struct name *np;
    	struct name **npp;
    
    	np = &n;
    	npp = &np;
    	
    	printf("\n Enter your name.");
    	scanf("%s", n.arr);
    
    	printf("\n Enter your age.");
    	//scanf("%d", &np->age);
    	scanf("%d", &n.age);
    
    	//printf("\n Your name is %s", np->arr);
    	//printf("\n Your age is %d", np->age);
    
    	printf("\n Your name is %s", (*npp)->arr);
    	printf("\n Your age is %d", (*npp)->age);
    
    	return 0;
    }
    Now if i enter more than 20 chars in the arr what does it do? Does it overwrite the memory adjacent to those 20 spaces that the arr has in its possession. Also if i do so at the end of the program completion i get the message

    "Stack aroudn the variable n was corrupted"

    So is this an indication of the fact that i have done a buffer overrun. Also how do i implement bound checking so that these mistakes can be avoided.

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by roaan View Post
    Now if i enter more than 20 chars in the arr what does it do? Does it overwrite the memory adjacent to those 20 spaces that the arr has in its possession.
    Yes. It's sometimes possible to find bits of one string in another when you do this.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Jun 2009
    Location
    US of A
    Posts
    305
    But is there any hack around so that i can implement run time check on my own without letting the user play havoc with my program ?

  4. #4
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    scanf_s, _scanf_s_l, wscanf_s, _wscanf_s_l (CRT)

    Security enhanced versions of CRT functions are available to address this very issue.

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by roaan View Post
    But is there any hack around so that i can implement run time check on my own without letting the user play havoc with my program ?
    Yeah, that would be normal to do in C. You can limit the amount taken by scanf() for example:
    Code:
    scanf("%49s",input);
    Basically, it's like leaving your lover in that Paul Simon song -- there's a thousand ways...except abachler's, which are non-standard.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > But is there any hack around so that i can implement run time check on my own without letting the user play havoc with my program ?
    Yes, you always use fgets() to read into a temporary buffer first. You then analyse and validate the input (say it's length) before copying it to it's final destination.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. stack and pointer problem
    By ramaadhitia in forum C Programming
    Replies: 2
    Last Post: 09-11-2006, 11:41 PM
  2. infix evaluation using stack
    By lewissi in forum C++ Programming
    Replies: 0
    Last Post: 11-03-2005, 02:56 AM
  3. Question about a stack using array of pointers
    By Ricochet in forum C++ Programming
    Replies: 6
    Last Post: 11-17-2003, 10:12 PM
  4. error trying to compile stack program
    By KristTlove in forum C++ Programming
    Replies: 2
    Last Post: 11-03-2003, 06:27 PM
  5. Stack Program Here
    By Troll_King in forum C Programming
    Replies: 7
    Last Post: 10-15-2001, 05:36 PM