help, please

This is a discussion on help, please within the C Programming forums, part of the General Programming Boards category; Hi, l'm getting an error when running the code below. the error seem to be in the Bold line ("while.."). ...

  1. #1
    Registered User
    Join Date
    Aug 2010
    Posts
    7

    help, please

    Hi,

    l'm getting an error when running the code below.
    the error seem to be in the Bold line ("while..").

    Code:
    userIPF = (char *)malloc(NAME_MAX_LENGTH * sizeof(char));
        if (userIPF == NULL)
        {
            fprintf(stderr, "Error: Memory allocation error\n");
            if (crypt_func != NULL)
                free(crypt_func);
            if (crypt_func_str != NULL)
                free(crypt_func_str);
            fclose(ipf);
                return -1;
        }
     
        passwordIPF = (char *)malloc(PASSWORD_MAX_LENGTH * sizeof(char));
        if (passwordIPF == NULL)
        {
            fprintf(stderr, "Error: Memory allocation error\n");
            if (crypt_func != NULL)
                free(crypt_func);
            if (crypt_func_str != NULL)
                free(crypt_func_str);
            if (userIPF != NULL)
                free(userIPF);
            fclose(ipf);
                return -1;
        }
    
    while (fscanf(ipf,"%s\t%s", userIPF, passwordIPF) == 2)
                {               
     
                            if ((strcmp(userIPF,user)==0) && (strcmp(passwordIPF, crypt_func_str)==0))
                    {
                        found = 1;
                        break;
                    }
                       
                    if (userIPF != NULL)
                        free(userIPF);
                    if (passwordIPF != NULL)
                        free(passwordIPF);
    
                    userIPF = (char *)malloc(NAME_MAX_LENGTH * sizeof(char));
                    if (userIPF == NULL)
                    {
                        fprintf(stderr, "Error: Memory allocation error\n");
                        if (crypt_func != NULL)
                            free(crypt_func);
                        if (crypt_func_str != NULL)
                            free(crypt_func_str);
                        fclose(ipf);
                            return -1;
                    }
                    passwordIPF = (char *)malloc(PASSWORD_MAX_LENGTH * sizeof(char));
                    if (passwordIPF == NULL)
                    {
                        fprintf(stderr, "Error: Memory allocation error\n");
                        if (crypt_func != NULL)
                            free(crypt_func);
                        if (crypt_func_str != NULL)
                            free(crypt_func_str);
                        if (userIPF != NULL)
                            free(userIPF);
                        fclose(ipf);
                            return -1;
                    }
     
                }
    this is what Valgrind is giving me:

    Code:
    ==28689== Invalid write of size 1
    ==28689==    at 0x510831F: _IO_vfscanf (in /lib/libc-2.9.so)
    ==28689==    by 0x51163E7: fscanf (in /lib/libc-2.9.so)
    ==28689==    by 0x40110B: main (authenticate.c:117)
    ==28689==  Address 0x5423464 is 0 bytes after a block of size 20 alloc'd
    ==28689==    at 0x4C278AE: malloc (vg_replace_malloc.c:207)
    ==28689==    by 0x400DCA: main (authenticate.c:81)
    ==28689==
    ==28689== Invalid write of size 1
    ==28689==    at 0x510BB91: _IO_vfscanf (in /lib/libc-2.9.so)
    ==28689==    by 0x51163E7: fscanf (in /lib/libc-2.9.so)
    ==28689==    by 0x40110B: main (authenticate.c:117)
    ==28689==  Address 0x5423470 is 12 bytes after a block of size 20 alloc'd
    ==28689==    at 0x4C278AE: malloc (vg_replace_malloc.c:207)
    ==28689==    by 0x400DCA: main (authenticate.c:81)
    ==28689==
    ==28689== Invalid read of size 1
    ==28689==    at 0x4C2832A: strcmp (mc_replace_strmem.c:337)
    ==28689==    by 0x400F98: main (authenticate.c:120)
    ==28689==  Address 0x5423464 is 0 bytes after a block of size 20 alloc'd
    ==28689==    at 0x4C278AE: malloc (vg_replace_malloc.c:207)
    ==28689==    by 0x400DCA: main (authenticate.c:81)
    i don't understand why there's an error. Can someone please help me?

    thank you.
    (and sorry for my english)

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You included stdlib.h, right?

    I'd love to see you kick out fscanf(), and use the more robust fgets(buffer, sizeof(buffer), filePointerName);
    Code:
    while((fgets(buffer, sizeof(buffer), filePointer)) != NULL) {
    
    }
    I really like it, and I can't post about fscanf() much, since this is a family oriented forum.
    Last edited by Adak; 08-30-2010 at 04:00 AM.

  3. #3
    Registered User
    Join Date
    Aug 2010
    Posts
    7
    Yes

  4. #4
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Wonder why you can't just do this?
    Code:
    while (fscanf(ipf,"%s\t%s", userIPF, passwordIPF) == 2)
                {               
                     if ((strcmp(userIPF,user)==0) && (strcmp(passwordIPF, crypt_func_str)==0))
                    {
                        found = 1;
                        break;
                    }
                }

  5. #5
    Registered User
    Join Date
    Aug 2010
    Posts
    7
    Because it doesnt work. We get the same error

  6. #6
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Maybe your input overflow userIPF, passwordIPF...
    And your file ipf is it opened successfully? Did you check that?
    If your input are NAME_MAX_LENGTH long, you need to allocate +1 for null character which fscanf() will append.
    Last edited by Bayint Naung; 08-30-2010 at 04:04 AM.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    And yet ANOTHER great reason to like fgets()!! < oh yeah! >

  8. #8
    Registered User
    Join Date
    Aug 2010
    Posts
    7
    in our file, the strings are separated by "tab".
    we tried to use fgets, but it doesn't work (we allocated enough memory to get it):

    Code:
    buff = (char *)malloc((sizeof(func)+NAME_MAX_LENGTH) * sizeof(char));
    
    	if (buff == NULL)
    
    	{
    
    		fprintf(stderr, "Error: Memory allocation error\n");
    
    		if (crypt_func != NULL)
    
    			free(crypt_func);
    
    		if (crypt_func_str != NULL)
    
    			free(crypt_func_str);
    
    		fclose(ipf);
    
            	return -1;
    
    	}
    while (fgets(buff, sizeof(buff), ipf) !=NULL){...}

  9. #9
    Registered User
    Join Date
    Aug 2010
    Posts
    7
    our ipf opened successfully and we allocated enough memory

  10. #10
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Provided that you've allocated enough memory, the only possible cause is because of your file pointer ipf.
    Btw,

    if ((strcmp(userIPF,user)==0) && (strcmp(passwordIPF, crypt_func_str)==0))

    Where is user and crypt_func_str defined?
    Last edited by Bayint Naung; 08-30-2010 at 04:26 AM.

  11. #11
    Registered User
    Join Date
    Aug 2010
    Posts
    7
    Thanks!!! it's working!!!

  12. #12
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    So What's the problem???

  13. #13
    Registered User
    Join Date
    Aug 2010
    Posts
    7

    Red face

    we changed it to fgets and made mistake in sizeof(buff)

  14. #14
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    Code:
    userIPF = (char *)malloc(NAME_MAX_LENGTH * sizeof(char));
    ...
    passwordIPF = (char *)malloc(PASSWORD_MAX_LENGTH * sizeof(char));
    ...
    userIPF = (char *)malloc(NAME_MAX_LENGTH * sizeof(char));
    ...
    passwordIPF = (char *)malloc(PASSWORD_MAX_LENGTH * sizeof(char));
    You should not cast the return value of malloc in C. Also, sizeof(char) is guaranteed to be 1 and so can be eliminated from the malloc call. The following should be sufficient:

    Code:
    userIPF = malloc(NAME_MAX_LENGTH);
    ...
    passwordIPF = malloc(PASSWORD_MAX_LENGTH);
    ...
    userIPF = malloc(NAME_MAX_LENGTH);
    ...
    passwordIPF = malloc(PASSWORD_MAX_LENGTH);
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  15. #15
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,848
    Code:
            if (crypt_func != NULL)
                free(crypt_func);
    Just do
    Code:
                free(crypt_func);
    free will accept a NULL pointer, and do nothing.
    Besides, if crypt_func could be NULL, then you're in trouble when it comes to the strcmp calls later on.

    Is there a reason why you don't do
    Code:
    char userIPF[NAME_MAX_LENGTH];
    For you average length of name, on your average machine, this would significantly simplify the code.
    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

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