Thread: Valgrind - Invalid read of size 1

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    7

    Valgrind - Invalid read of size 1

    I'm getting the next error when running memcheck with valgrind:

    Invalid read of size 1

    ==2511== at 0x4C29852: __GI_strcpy (mc_replace_strmem.c:313)
    ==2511== by 0x401A76: getstring (reader.c:125)
    ==2511== by 0x400A0E: main (reader.c:22)
    ==2511== Address 0x7fefffcfb is just below the stack ptr. To suppress, use: --workaround-gcc296-bugs=yes
    Code:
     void getstring{
    char test[1024];
    ...
    string = readstring(filepointer);
    strcpy(test,string);
    ...
    }
    I already searched on stackoverflow, etc. And i guess it has something to do with strcpy(). Help on how to solve this error woud be appreciated.
    Last edited by Castelmagno; 02-29-2012 at 01:48 PM.

  2. #2
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Is 1024 large enough?

  3. #3
    Registered User
    Join Date
    Jan 2012
    Posts
    7
    Yeah, the returned string is only 10 bytes long.

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Post the complete function and the definition of readstring().
    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

  5. #5
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    What if readstring returned NULL to indicate it reached eof.

  6. #6
    Registered User
    Join Date
    Jan 2012
    Posts
    7
    Even with this modification I'm getting the same error with valgrind.

    Code:
     void getstring{
    char test[1024];
    ...
    string = readstring(filepointer);
    if(string!=NULL)
    strcpy(test,string);
    ...
    }

  7. #7
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by Castelmagno View Post
    Code:
     void getstring{
    This is not a valid function definition, or even valid C. Please post the actual code, copy-pasted from your editor (as plain text), not some junk you typed off the top of your head. Post the smallest, compileable example that demonstrates this problem. Also post the valgrind command you used to test this. Then, we can compile and run it through valgrind on our machines and help you find your problem.

  8. #8
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    It's an invalid READ, so it's not to do with writing to the stack buffer. Such stack errors are impossible for valgrind to check, anyway. What's more likely is that readstring() is not null-terminating the string correctly, causing strcpy() to read past the end.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Valgrind Invalid Read/Write In C Program
    By Alex Richman in forum C Programming
    Replies: 2
    Last Post: 10-02-2011, 03:15 PM
  2. Invalid Read from Valgrind
    By jduro in forum C Programming
    Replies: 1
    Last Post: 10-05-2010, 11:28 AM
  3. realloc(): invalid size:
    By fxtdr79 in forum C Programming
    Replies: 4
    Last Post: 06-03-2010, 09:30 PM
  4. *** glibc detected *** free(): invalid next size
    By kraghavan in forum C++ Programming
    Replies: 3
    Last Post: 05-11-2010, 07:17 AM
  5. *** glibc detected *** free(): invalid next size
    By icebabe in forum C Programming
    Replies: 2
    Last Post: 05-24-2006, 12:09 PM