Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Invalid read of size 1

Reply
Thread Tools

Invalid read of size 1

 
 
A.A
Guest
Posts: n/a
 
      12-10-2005
int db_is_fld_empty(char *fld)
{
return (fld && *fld)?0:1;
}

 
Reply With Quote
 
 
 
 
Robert Gamble
Guest
Posts: n/a
 
      12-10-2005
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

 
Reply With Quote
 
 
 
 
Roman Mashak
Guest
Posts: n/a
 
      12-10-2005
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: http://www.velocityreviews.com/forums/(E-Mail Removed)


 
Reply With Quote
 
Roman Mashak
Guest
Posts: n/a
 
      12-10-2005
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: (E-Mail Removed)


 
Reply With Quote
 
Mark McIntyre
Guest
Posts: n/a
 
      12-10-2005
On Sat, 10 Dec 2005 12:12:07 +0700, in comp.lang.c , "Roman Mashak"
<(E-Mail Removed)> 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 =----
 
Reply With Quote
 
Mark McIntyre
Guest
Posts: n/a
 
      12-10-2005
On Sat, 10 Dec 2005 20:12:16 +0700, in comp.lang.c , "Roman Mashak"
<(E-Mail Removed)> 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 =----
 
Reply With Quote
 
Roman Mashak
Guest
Posts: n/a
 
      12-10-2005
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: (E-Mail Removed)


 
Reply With Quote
 
Robert Gamble
Guest
Posts: n/a
 
      12-10-2005
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

 
Reply With Quote
 
Robert Gamble
Guest
Posts: n/a
 
      12-10-2005
Mark McIntyre wrote:
> On Sat, 10 Dec 2005 12:12:07 +0700, in comp.lang.c , "Roman Mashak"
> <(E-Mail Removed)> 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

 
Reply With Quote
 
Mark McIntyre
Guest
Posts: n/a
 
      12-10-2005
On 10 Dec 2005 07:00:48 -0800, in comp.lang.c , "Robert Gamble"
<(E-Mail Removed)> 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 =----
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Preferred Size, Minimum Size, Size Jason Cavett Java 5 05-25-2008 08:32 AM
mega pixels, file size, image size, and print size - Adobe Evangelists Frank ess Digital Photography 0 11-14-2006 05:08 PM
Invalid URI: There is an invalid sequence in the string. Error!! Patrick.O.Ige ASP .Net 1 07-02-2006 11:21 AM
Invalid read of size 1 [valgrind warning] Flash Gordon C Programming 5 12-13-2005 02:54 PM
Valgrind says "Invalid read of size 4". What's the problem? hvaisane@cc.joensuu.fi C++ 2 02-17-2005 12:45 PM



Advertisments