By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,379 Members | 1,646 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,379 IT Pros & Developers. It's quick & easy.

Invalid read of size 1

P: n/a
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: mr*@tusur.ru
Dec 10 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
A.A
int db_is_fld_empty(char *fld)
{
return (fld && *fld)?0:1;
}

Dec 10 '05 #2

P: n/a
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

Dec 10 '05 #3

P: n/a
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: mr*@tusur.ru
Dec 10 '05 #4

P: n/a
On Sat, 10 Dec 2005 12:12:07 +0700, in comp.lang.c , "Roman Mashak"
<mr*@tusur.ru> 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 =----
Dec 10 '05 #5

P: n/a
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: mr*@tusur.ru
Dec 10 '05 #6

P: n/a
On Sat, 10 Dec 2005 20:12:16 +0700, in comp.lang.c , "Roman Mashak"
<mr*@tusur.ru> 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 =----
Dec 10 '05 #7

P: n/a
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

Dec 10 '05 #8

P: n/a
Mark McIntyre wrote:
On Sat, 10 Dec 2005 12:12:07 +0700, in comp.lang.c , "Roman Mashak"
<mr*@tusur.ru> 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

Dec 10 '05 #9

P: n/a
On 10 Dec 2005 07:00:48 -0800, in comp.lang.c , "Robert Gamble"
<rg*******@gmail.com> 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 =----
Dec 10 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.