Invalid read of size 1

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Roman Mashak

    Invalid read of size 1

    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: mrv@tusur.ru


  • A.A

    #2
    Re: Invalid read of size 1

    int db_is_fld_empty (char *fld)
    {
    return (fld && *fld)?0:1;
    }

    Comment

    • Robert Gamble

      #3
      Re: Invalid read of size 1

      Roman Mashak wrote:[color=blue]
      > 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?[/color]

      <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

      Comment

      • Roman Mashak

        #4
        Re: Invalid read of size 1

        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: mrv@tusur.ru


        Comment

        • Mark McIntyre

          #5
          Re: Invalid read of size 1

          On Sat, 10 Dec 2005 12:12:07 +0700, in comp.lang.c , "Roman Mashak"
          <mrv@tusur.ru > wrote:
          [color=blue]
          >int db_is_fld_empty (char *fld)
          >{
          > return ( !strcmp(fld, "") ? 1 : 0 );
          >}[/color]

          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)
          [color=blue]
          >Seems like it doesn't like rmpty string I compare with or it's something
          >more serious?[/color]

          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 =----

          Comment

          • Roman Mashak

            #6
            Re: Invalid read of size 1

            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: mrv@tusur.ru


            Comment

            • Mark McIntyre

              #7
              Re: Invalid read of size 1

              On Sat, 10 Dec 2005 20:12:16 +0700, in comp.lang.c , "Roman Mashak"
              <mrv@tusur.ru > wrote:
              [color=blue]
              >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;[/color]

              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 =----

              Comment

              • Robert Gamble

                #8
                Re: Invalid read of size 1

                Roman Mashak wrote:[color=blue]
                > 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;[/color]

                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

                Comment

                • Robert Gamble

                  #9
                  Re: Invalid read of size 1

                  Mark McIntyre wrote:[color=blue]
                  > On Sat, 10 Dec 2005 12:12:07 +0700, in comp.lang.c , "Roman Mashak"
                  > <mrv@tusur.ru > wrote:
                  >[color=green]
                  > >int db_is_fld_empty (char *fld)
                  > >{
                  > > return ( !strcmp(fld, "") ? 1 : 0 );
                  > >}[/color]
                  >
                  > 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)[/color]

                  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

                  Comment

                  • Mark McIntyre

                    #10
                    Re: Invalid read of size 1

                    On 10 Dec 2005 07:00:48 -0800, in comp.lang.c , "Robert Gamble"
                    <rgamble99@gmai l.com> wrote:
                    [color=blue]
                    >The indirection operator should be outside of the parenthesis:[/color]

                    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 =----

                    Comment

                    Working...