Thread: malloc problems

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    3

    malloc problems

    I have made a function to check the user's password but I got a strange error when trying to run it:
    *** glibc detected *** malloc(): memory corruption: 0x0805b280 ***
    Aborted

    here is the function:

    Code:
    int auth_passwd(const char *user, const char *password)
    {
    	char *sys_pass, *user_pass, *salt;
    	struct spwd *sp;
    
    	sp=getspnam(user);
    	sys_pass=strdup(sp->sp_pwdp);
    
    	if(sys_pass==NULL) return -1;
    
    	if(!strncmp(sys_pass, "$1", 2)) {
    		/* the password is encrypted with MD5 algorithm */
    		if((salt=malloc(12))==NULL) {
    			perror("malloc");
    			exit(1);
    		}
    		strncpy(salt, sys_pass, 11);
    		salt[12]='\0';
    	} else {
    		/* the password is encrypted with DES algorithm */
    		if((salt=malloc(3))==NULL) {
    			perror("malloc");
    			exit(1);
    		}
    		strncpy(salt, sys_pass, 2);
    		salt[3]='\0';
    	}
    
    	user_pass=(char *)crypt(password, salt);
    
    	if(!strcmp(sys_pass, user_pass)) {
    		xfree(salt);
    		return=1;
    	} else {
    		xfree(salt);
    		result -1;
    	}
    }
    Can you tell me where is the problem ?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > if((salt=malloc(12))==NULL) {
    > salt[12]='\0';
    You stepped off the end of your allocation.

    And why are you calling xfree() when you called malloc().
    It should be free()

  3. #3
    Registered User
    Join Date
    Dec 2005
    Posts
    3
    my fault, xfree(); is a function just like free(); but with error checking. Sorry, I forgot to edit that one.
    Thank you for the help, eventhough that was a stupid mistake

  4. #4
    Rabble Rouser Slacker's Avatar
    Join Date
    Dec 2005
    Posts
    116
    >eventhough that was a stupid mistake
    I haven't encountered a smart mistake, have you?

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    return=1;
    Is that supposed to be like this?
    Code:
    return 1;
    [edit]
    And this? What does it do?
    Code:
    result -1;
    [/edit]
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You also accessed memory outside your allocated space here:
    Code:
    		if((salt=malloc(3))==NULL) {
    			perror("malloc");
    			exit(1);
    		}
    		strncpy(salt, sys_pass, 2);
    		salt[3]='\0';
    It's just like an array. If you allocate 3 bytes, the last addressable element is [2].
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using malloc
    By ulillillia in forum C Programming
    Replies: 34
    Last Post: 02-20-2008, 06:41 PM
  2. Problems with malloc
    By Zachary Lewis in forum C Programming
    Replies: 7
    Last Post: 02-18-2008, 06:45 AM
  3. Problems with pointers and malloc()
    By Deirdre in forum C Programming
    Replies: 3
    Last Post: 10-28-2007, 04:20 PM
  4. C Pointers Problems
    By mhelal in forum C Programming
    Replies: 8
    Last Post: 01-10-2007, 06:35 AM
  5. Is there a limit on the number of malloc calls ?
    By krissy in forum Windows Programming
    Replies: 3
    Last Post: 03-19-2006, 12:26 PM