Invalid read of size 1

R

Robert Gamble

Roman said:
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
 
R

Roman Mashak

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: (e-mail address removed)
 
R

Roman Mashak

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 address removed)
 
M

Mark McIntyre

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
 
M

Mark McIntyre

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.
 
R

Roman Mashak

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 address removed)
 
R

Robert Gamble

Roman said:
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
 
R

Robert Gamble

Mark said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top