473,466 Members | 1,408 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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: mr*@tusur.ru
Dec 10 '05 #1
9 13962
A.A
int db_is_fld_empty(char *fld)
{
return (fld && *fld)?0:1;
}

Dec 10 '05 #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

Dec 10 '05 #3
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: hvaisane | last post by:
Valgrind says ==11604== Invalid read of size 4 ==11604== at 0x8048ABB: main (foo.cc:36) ==11604== Address 0x1B92415C is 4 bytes inside a block of size 8 free'd ==11604== at 0x1B90514F:...
7
by: Dica | last post by:
i've used the sample code from msdn to create an encyption/decryption assembly as found here: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnnetsec/html/SecNetHT10.asp i'm...
2
by: Schorschi | last post by:
Can't seemd to get ReadFile API to work! Returns invalid handle error? =========================================================================== Ok, the visual basic gurus, help! The...
5
by: Janning Vygen | last post by:
Hi, tonight my database got corruppted. before it worked fine. since two days i do the following tasks every night psql -c 'CLUSTER;' $DBNAME psql -c 'VACUUM FULL ANALYZE;' $DBNAME ...
5
by: Roman Mashak | last post by:
Hello, All! I already posted my question and received valuable feedbacks, I changed my code as was proposed here but still receive the same error of valgrind. SO, the code is: #define...
27
by: Deephay | last post by:
Greetings all, I have a program that used the realloc() function to change the allocated size of a buffer, the program works with some arguments, but with some other arguments, it will show me...
0
by: Hannibal111111 | last post by:
I found this code on a site for doing string encryption/decryption. The string will encrypt fine, but I get this error when I try to decrypt. Any idea why? I posted the code below. The error...
0
by: shrik | last post by:
I have following error : Total giant files in replay configuration file are : File name : /new_file/prob1.rec Given file /new_file/prob1.rec is successfully verified. Splitting for giant file...
4
by: mattehz | last post by:
Hey there, I am trying to upload old source files and came across these errors: Warning: Invalid argument supplied for foreach() in /home/mattehz/public_html/acssr/trunk/inc_html.php on line 59...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.