Stack Smashing error

This is a discussion on Stack Smashing error within the C Programming forums, part of the General Programming Boards category; Code: int Formulate_StaticURL(char *finalstring,char *inputstring,int len) { int i=0; char *fileextension = inputstring+len-1,fileextn[10]; memset(fileextn,'

Thread: Stack Smashing error

',10); printf("lenbfr:%d\n",len); while(len > 0) { while((*fileextension!='/')&& ...

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    139

    Stack Smashing error

    Code:
    int Formulate_StaticURL(char *finalstring,char *inputstring,int len)
    {
    	int i=0;
    	char *fileextension = inputstring+len-1,fileextn[10]; 
    		
    	memset(fileextn,'\0',10);	
    	printf("lenbfr:%d\n",len);	
    	while(len > 0)
    	{
    		while((*fileextension!='/')&& ((len--) >0))
    		{
    			fileextn[i++] = *fileextension--;
    			//printf("len:%d\n",len);
    		}
    		
    		if((len <= 0)||( i==0 ))
    		{
    	         	printf("mylen:%d\n",len);
    		        finalstring = NULL;
    			break;
    		}
    		else
    		{
    			strncpy(finalstring,inputstring,len);
    			finalstring += len;
    			strcpy(finalstring,"something/something.");
    			finalstring += strlen("something/something.");
    			strcpy(finalstring,fileextn);
    			break;
    		}
    		
    	}
    			printf("mylen2:%d\n",len);
    	return 1;
    }
    //************************************************************//
    // Main Program Starts here
    //************************************************************//
    int main()
    {
    	//char *StaticCfg = "192.168.5.59/mydirectoryname/php";
    	char *StaticCfg = "192.168.5.59mydirectorynamehp";
    	char *OutpuCfg =NULL,ch;
    	OutpuCfg =(char *)malloc(URL_SZ);	
    	memset(OutpuCfg,'0',URL_SZ);
    	printf("My inoput :%s\n",StaticCfg);
    	if(OutpuCfg != NULL)
    		Formulate_StaticURL(OutpuCfg,StaticCfg,strlen(StaticCfg));
    	if(OutpuCfg!=NULL)
    	printf("My output:%s\n",OutpuCfg);
    	//getch();
    	//scanf("%c",&ch);
    	//system("PAUSE");
    	return 0;
    }

    What mistake am i doing in the above code to get a stack smashing error?


    Thanks in advance

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,459
    In the future, please copy-paste your code as plain text so that the forum's line numbering and syntax highlighting scripts can do their jobs.
    Code:
    fileextn[10];
    ...
    while((*fileextension!='/')&& ((len--) >0))
    {
        fileextn[i++] = *fileextension--;
        //printf("len:%d\n",len);
    }
    fileextn can hold a string of length 9 (remember 1 for the null terminator). Your while loop pays no mind to that length limitation, so in this case, where inputstring has no '/' character, and is longer than 9 characters, you copy past the end of fileextn, and smash the stack.

    Note: I have no idea what this code is trying to accomplish, but I suspect your Formulate_StaticURL function has other bugs in it as well.

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    139
    Ah!!! i didnt see the limit 10......Thanks for the quick reply.....I will not paste the plain txt nxt time.....


    Quote Originally Posted by anduril462 View Post
    Note: I have no idea what this code is trying to accomplish, but I suspect your Formulate_StaticURL function has other bugs in it as well.


    Its just replaces :

    For Input :192.168.5.59/mydirectoryname/php

    with
    Output :192.168.5.59/mydirectoryname/something/something.php

    Please let me know the other bugs as well

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Stack Smashing
    By dtow1 in forum Linux Programming
    Replies: 7
    Last Post: 11-18-2012, 07:54 PM
  2. stack smashing error
    By dudeomanodude in forum C++ Programming
    Replies: 4
    Last Post: 01-29-2008, 02:37 PM
  3. stack smashing error
    By spank in forum Linux Programming
    Replies: 2
    Last Post: 05-24-2007, 02:07 PM
  4. smashing the stack
    By rohit in forum C Programming
    Replies: 3
    Last Post: 10-07-2002, 07:06 AM
  5. smashing the stack
    By rohit in forum Linux Programming
    Replies: 1
    Last Post: 10-07-2002, 02:47 AM

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