Thread: Stack Smashing Detected

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    3

    Stack Smashing Detected

    My program reads IP address from files labeled 1 to 1000 ( no extensions) and then prints out the IP addresses read. Currently I have the following:


    Code:
    void clear_string(char word[50]) {
    	int x;
    	for ( x = strlen(word); x != -1; x-- ) word[x] = '\0';
    } 	
    
    int main () {
    	int i, j, k, z, t, g;
    	FILE *f;
    	char line[1000], filenumber[2], begin[15], IP[15], domain[50];
    	dlist *current, *curIP, *list, *listIP, *temp;
    
    
    	for ( i = 1; i < 1001 ;i++) {
    		sprintf(filenumber, "%d", i);
    		f = fopen(filenumber, "r"); 				// Open File
    		if ( f == NULL ) break;
    		printf("\nFile: %d\n", i);
    		listIP = NULL;
    		while ( fgets(line, 1000, f) ) { 			
    			if ( line[1] == '\n' ) break;	
    			strncpy(begin, line, 14);
    			if ( begin[sizeof(begin)-1] != '\0' ) begin[sizeof(begin)-1] = '\0';		
    			if ( strcmp( "Received: from", begin ) == 0 ) { // See if IP Adress is in this line
    				for ( j = 0, k = 0; j < strlen(line) ; j++ ) {
    					if ( line[j] == '.' || isdigit(line[j]) != 0 )  {
    						IP[k] = line[j];
    						k++;
    					} else if ( k < 7 ) {
    						k = 0;
    						clear_string(IP);
    					} else if ( k >= 7 && k <= 15 && correct_IP_format(IP) == 1 ) {
    						for ( t = 0, temp = listIP; temp != NULL; temp = temp->next) {
    							if ( strcmp(temp->val, IP) == 0 ) t = 1;
    						}
    						if ( t == 0 ) {
    							curIP = (dlist *)malloc(sizeof(dlist));
    							strcpy(curIP->val, IP);
    							curIP->next = listIP;
    							listIP = curIP;
    						}
    						break;
    					}
    				}
    		//	}
    
    		}
    		for ( curIP = listIP; curIP != NULL; curIP = curIP->next ) {
    			printf("IP: %s\n", curIP->val);
    		}
    		fclose(f);
    	}
    }
    After I enter the while loop that extracts each line from the file, I extract the first 14 characters from that line to see if it begins with "Recieved: from". This is because I noticed a pattern that the IP addresses are located within the same line. That was a while ago and I have since noticed this is not the case and that I need to check every line for an IP address regardless of what it starts with.

    I should note the above code works and there are no issues with it. Once I remove the lines of code that check to see if the current line of the file begins with "Recieved: from" is when problems begin. Those lines being:
    Code:
    strncpy(begin, line, 14);
    if ( begin[sizeof(begin)-1] != '\0' ) begin[sizeof(begin)-1] = '\0';		
    if ( strcmp( "Received: from", begin ) == 0 ) { // See if IP Adress is in this line
    and their respective closing brackets.

    So the final piece of code that is giving me stack smashing issues is the following:


    Code:
    void clear_string(char word[50]) {
    	int x;
    	for ( x = strlen(word); x != -1; x-- ) word[x] = '\0';
    } 	
    
    int main () {
    	int i, j, k, z, t, g;
    	FILE *f;
    	char line[1000], filenumber[2], begin[15], IP[15], domain[50];
    	dlist *current, *curIP, *list, *listIP, *temp;
    
    
    	for ( i = 1; i < 1001 ;i++) {
    		sprintf(filenumber, "%d", i);
    		f = fopen(filenumber, "r"); 				// Open File
    		if ( f == NULL ) break;
    		printf("\nFile: %d\n", i);
    		listIP = NULL;
    		while ( fgets(line, 1000, f) ) { 			
    			if ( line[1] == '\n' ) break;	
    			for ( j = 0, k = 0; j < strlen(line) ; j++ ) {
    				if ( line[j] == '.' || isdigit(line[j]) != 0 )  {
    					IP[k] = line[j];
    					k++;
    				} else if ( k < 7 ) {
    					k = 0;
    					clear_string(IP);
    				} else if ( k >= 7 && k <= 15 && correct_IP_format(IP) == 1 ) {
    					for ( t = 0, temp = listIP; temp != NULL; temp = temp->next) {
    						if ( strcmp(temp->val, IP) == 0 ) t = 1;
    					}
    					if ( t == 0 ) {
    						curIP = (dlist *)malloc(sizeof(dlist));
    						strcpy(curIP->val, IP);
    						curIP->next = listIP;
    						listIP = curIP;
    					}
    					break;
    				}
    			}
    		}
    		for ( curIP = listIP; curIP != NULL; curIP = curIP->next ) {
    			printf("IP: %s\n", curIP->val);
    		}
    		fclose(f);
    	}
    	
    }
    I should also note that my program executes perfectly fine and everything works all the way to the end at which point it gives me the stack smashing error. It doesnt make much sense to me because those 3 lines are just extracting the first 14 characters of the line variable to the begin variable and then checking if the begin variable equals "Recieved: from". I do not use the begin variable anywhere else so I dont see what the issue is.

    Any ideas or tips on how to debug it?

    Thank you
    Last edited by halexh; 05-18-2010 at 10:12 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. *** stack smashing detected ***
    By chakra in forum C Programming
    Replies: 2
    Last Post: 06-09-2009, 09:12 PM
  2. *** stack smashing detected ***
    By Martin_HS in forum C Programming
    Replies: 9
    Last Post: 05-29-2009, 04:01 AM
  3. stack and pointer problem
    By ramaadhitia in forum C Programming
    Replies: 2
    Last Post: 09-11-2006, 11:41 PM
  4. Question about a stack using array of pointers
    By Ricochet in forum C++ Programming
    Replies: 6
    Last Post: 11-17-2003, 10:12 PM
  5. error trying to compile stack program
    By KristTlove in forum C++ Programming
    Replies: 2
    Last Post: 11-03-2003, 06:27 PM