Invalid read of size 1

Discussion in 'C Programming' started by A.A, Dec 10, 2005.

  1. A.A

    A.A Guest

    int db_is_fld_empty(char *fld)
    {
    return (fld && *fld)?0:1;
    }
    A.A, Dec 10, 2005
    #1
    1. Advertising

  2. Roman Mashak wrote:
    > Hello, All!
    >
    > I assume my post isn't offtopic here.
    >
    > I used 'valgrind' utility (guess plenty if you know and use it) for my
    > application and I got the message "Invalid read of size 1" regarding this
    > pieice of code:
    >
    > int db_is_fld_empty(char *fld)
    > {
    > return ( !strcmp(fld, "") ? 1 : 0 );
    > }
    >
    > Seems like it doesn't like rmpty string I compare with or it's something
    > more serious?


    <OT> You are calling this function with a null pointer which then gets
    passed to strcmp. It is undefined behavior to pass such a pointer to
    strcmp and valgrind caught this. The empty string is okay. </OT>

    Robert Gamble
    Robert Gamble, Dec 10, 2005
    #2
    1. Advertising

  3. A.A

    Roman Mashak Guest

    Hello, All!

    I assume my post isn't offtopic here.

    I used 'valgrind' utility (guess plenty if you know and use it) for my
    application and I got the message "Invalid read of size 1" regarding this
    pieice of code:

    int db_is_fld_empty(char *fld)
    {
    return ( !strcmp(fld, "") ? 1 : 0 );
    }

    Seems like it doesn't like rmpty string I compare with or it's something
    more serious?

    TIA.

    With best regards, Roman Mashak. E-mail:
    Roman Mashak, Dec 10, 2005
    #3
  4. A.A

    Roman Mashak Guest

    Hello, Robert!
    You wrote on 9 Dec 2005 19:39:13 -0800:

    ??>> this pieice of code: int db_is_fld_empty(char *fld) { return (
    ??>> !strcmp(fld, "") ? 1 : 0 ); } Seems like it doesn't like rmpty string
    ??>> I compare with or it's something more serious?

    RG> <OT> You are calling this function with a null pointer which then gets
    RG> passed to strcmp. It is undefined behavior to pass such a pointer to
    RG> strcmp and valgrind caught this. The empty string is okay. </OT>
    What is way to bypass this? Is it enough to include checking:

    if ( fld != NULL)
    return ( !strcmp(fld, "") ? 1 : 0 );
    else
    return 0;

    With best regards, Roman Mashak. E-mail:
    Roman Mashak, Dec 10, 2005
    #4
  5. On Sat, 10 Dec 2005 12:12:07 +0700, in comp.lang.c , "Roman Mashak"
    <> wrote:

    >int db_is_fld_empty(char *fld)
    >{
    > return ( !strcmp(fld, "") ? 1 : 0 );
    >}


    this is virtually a macro. Bear in mind that all you care about is if
    the first character of fld is non null

    #define DB_IS_FLD_EMPTY(x) (((x)&&(*x))?0:1)

    >Seems like it doesn't like rmpty string I compare with or it's something
    >more serious?


    you need to make sure that fld isn't NULL

    ----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
    http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
    ----= East and West-Coast Server Farms - Total Privacy via Encryption =----
    Mark McIntyre, Dec 10, 2005
    #5
  6. On Sat, 10 Dec 2005 20:12:16 +0700, in comp.lang.c , "Roman Mashak"
    <> wrote:

    >I tried to wrap it out in 'if-then' statement, but still get same warnings
    >from valgrind:
    >
    >if (fld != NULL)
    > return ((x && *x) ? 0 : 1);
    >else
    > return 0;


    Post your EXACT code (the above is not it...) and the error message.
    Its possible that valgrind is producing a spurious message.

    ----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
    http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
    ----= East and West-Coast Server Farms - Total Privacy via Encryption =----
    Mark McIntyre, Dec 10, 2005
    #6
  7. A.A

    Roman Mashak Guest

    Hello, Mark!
    You wrote on Sat, 10 Dec 2005 11:00:41 +0000:

    MM> this is virtually a macro. Bear in mind that all you care about is if
    MM> the first character of fld is non null

    MM> #define DB_IS_FLD_EMPTY(x) (((x)&&(*x))?0:1)

    ??>> Seems like it doesn't like rmpty string I compare with or it's
    ??>> something more serious?

    MM> you need to make sure that fld isn't NULL
    I tried to wrap it out in 'if-then' statement, but still get same warnings
    from valgrind:

    if (fld != NULL)
    return ((x && *x) ? 0 : 1);
    else
    return 0;

    With best regards, Roman Mashak. E-mail:
    Roman Mashak, Dec 10, 2005
    #7
  8. Roman Mashak wrote:
    > Hello, Robert!
    > You wrote on 9 Dec 2005 19:39:13 -0800:
    >
    > ??>> this pieice of code: int db_is_fld_empty(char *fld) { return (
    > ??>> !strcmp(fld, "") ? 1 : 0 ); } Seems like it doesn't like rmpty string
    > ??>> I compare with or it's something more serious?
    >
    > RG> <OT> You are calling this function with a null pointer which then gets
    > RG> passed to strcmp. It is undefined behavior to pass such a pointer to
    > RG> strcmp and valgrind caught this. The empty string is okay. </OT>
    > What is way to bypass this? Is it enough to include checking:
    >
    > if ( fld != NULL)
    > return ( !strcmp(fld, "") ? 1 : 0 );
    > else
    > return 0;


    That should do it. Mark and A.A already gave you shorter solutions,
    here is another:

    #define DB_IS_FLD_EMPTY(p) !((p)&&*(p))

    Robert Gamble
    Robert Gamble, Dec 10, 2005
    #8
  9. Mark McIntyre wrote:
    > On Sat, 10 Dec 2005 12:12:07 +0700, in comp.lang.c , "Roman Mashak"
    > <> wrote:
    >
    > >int db_is_fld_empty(char *fld)
    > >{
    > > return ( !strcmp(fld, "") ? 1 : 0 );
    > >}

    >
    > this is virtually a macro. Bear in mind that all you care about is if
    > the first character of fld is non null
    >
    > #define DB_IS_FLD_EMPTY(x) (((x)&&(*x))?0:1)


    This won't work properly with something like this:

    DB_IS_FLD_EMPTY("abc"+3);

    The indirection operator should be outside of the parenthesis:

    #define DB_IS_FLD_EMPTY(x) (((x)&&*(x))?0:1)

    Robert Gamble
    Robert Gamble, Dec 10, 2005
    #9
  10. On 10 Dec 2005 07:00:48 -0800, in comp.lang.c , "Robert Gamble"
    <> wrote:

    >The indirection operator should be outside of the parenthesis:


    Yeah, I always get that kind of thing wrong. Fortunately IRL my test
    cases catch it.

    ----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
    http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
    ----= East and West-Coast Server Farms - Total Privacy via Encryption =----
    Mark McIntyre, Dec 10, 2005
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    2
    Views:
    34,062
    Victor Bazarov
    Feb 17, 2005
  2. Patrick.O.Ige
    Replies:
    1
    Views:
    1,959
    Patrick.O.Ige
    Jul 2, 2006
  3. Flash Gordon

    Invalid read of size 1 [valgrind warning]

    Flash Gordon, Dec 12, 2005, in forum: C Programming
    Replies:
    5
    Views:
    1,912
    Robert Gamble
    Dec 13, 2005
  4. Jason Cavett

    Preferred Size, Minimum Size, Size

    Jason Cavett, May 23, 2008, in forum: Java
    Replies:
    5
    Views:
    12,568
    Michael Jung
    May 25, 2008
  5. Alex Dowad
    Replies:
    4
    Views:
    271
    Michel Demazure
    May 1, 2010
Loading...

Share This Page