stack getting corrupted....

This is a discussion on stack getting corrupted.... within the C Programming forums, part of the General Programming Boards category; Okay here is one of the questions that i wanted to figure out. C does not have any array bounds ...

  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,189
    scanf_s, _scanf_s_l, wscanf_s, _wscanf_s_l (CRT)

    Security enhanced versions of CRT functions are available to address this very issue.
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,541
    > 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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, 01:56 AM
  3. Question about a stack using array of pointers
    By Ricochet in forum C++ Programming
    Replies: 6
    Last Post: 11-17-2003, 09:12 PM
  4. error trying to compile stack program
    By KristTlove in forum C++ Programming
    Replies: 2
    Last Post: 11-03-2003, 05:27 PM
  5. Stack Program Here
    By Troll_King in forum C Programming
    Replies: 7
    Last Post: 10-15-2001, 05:36 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21