 |
BorlandTalk.com Borland discussion newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Stephane Baillargeon Guest
|
Posted: Wed Jun 29, 2005 10:10 pm Post subject: strcmp not working???? |
|
|
#define GDF_HEADER "GDF"
unsined char ucHeader[3];
fread(ucHeader, sizeof(ucHeader), 1, fp);
Why does the first condition resolve to true but the second one to false?
if(ucHeader[0] == 'G' && ucHeader[1] == 'D' && ucHeader[2] == 'F')
{
// true
......
}
if(ucHeader == GDF_HEADER)
{
false
......
}
When I envoke the watch, both values of ucHeader & GDF_HEADER are identical
|
|
| Back to top |
|
 |
Remy Lebeau (TeamB) Guest
|
Posted: Wed Jun 29, 2005 10:31 pm Post subject: Re: strcmp not working???? |
|
|
"salamander1965" <salamander1 (AT) verizon (DOT) net> wrote
| Quote: | if (strcmp(ucHeader, GDF_HEADER)==0)
|
Like you said, ucHeader is not null-terminated, so you should use strncmp()
instead (which you should use anyway since strcmp() is not safe in general):
if( strcmp(ucHeader, GDF_HEADER, 3) ==0 )
Gambit
|
|
| Back to top |
|
 |
salamander1965 Guest
|
Posted: Wed Jun 29, 2005 10:37 pm Post subject: Re: strcmp not working???? |
|
|
| Quote: | #define GDF_HEADER "GDF"
unsined char ucHeader[3];
fread(ucHeader, sizeof(ucHeader), 1, fp);
Why does the first condition resolve to true but the second one to false?
if(ucHeader[0] == 'G' && ucHeader[1] == 'D' && ucHeader[2] == 'F')
{
// true
......
}
if(ucHeader == GDF_HEADER)
{
false
......
}
When I envoke the watch, both values of ucHeader & GDF_HEADER are identical
|
First, your ucHeader is not zero-terminated so you can't compare it as a
string with "GDF". Second, by using ucHeader==GDF_HEADER, your actually
comparing a pointer to the ucHeader array with a pointer to the string
constant "GDF". You should use strcmp() to do the comparison in the
second if block:
if (strcmp(ucHeader, GDF_HEADER)==0)
{
...
}
But, you will need to have ucHeader zero-terminated:
#define GDF_HEADER "GDF"
unsigned char ucHeader[4];
fread(ucHeader, sizeof(ucHeader)-1, 1, fp);
ucHeader[3]=NULL;
etc...
If ucHeader is global, I think it will be initialized to zero, but I'm
not sure on this. Better to be safe and set ucHeader[3] yourself.
-- salamander
|
|
| Back to top |
|
 |
Giuliano Guest
|
Posted: Wed Jun 29, 2005 10:42 pm Post subject: Re: strcmp not working???? |
|
|
On Wed, 29 Jun 2005 18:10:05 -0400, "Stephane Baillargeon"
<solutionsgestico (AT) sympatico (DOT) ca> wrote:
| Quote: | #define GDF_HEADER "GDF"
unsined char ucHeader[3];
fread(ucHeader, sizeof(ucHeader), 1, fp);
[snip]
if(ucHeader == GDF_HEADER)
{
false
......
}
When I envoke the watch, both values of ucHeader & GDF_HEADER are identical
|
Hi,
You are trying to compare pointers, not the strings.
Try to change
if(ucHeader == GDF_HEADER)
...
with
if ( !strcmp( ucHeader, GDF_HEADER ) )
...
Ciao
|
|
| Back to top |
|
 |
Michael Gillen Guest
|
Posted: Wed Jun 29, 2005 10:42 pm Post subject: Re: strcmp not working???? |
|
|
Stephane Baillargeon wrote:
| Quote: | #define GDF_HEADER "GDF"
unsined char ucHeader[3];
fread(ucHeader, sizeof(ucHeader), 1, fp);
Why does the first condition resolve to true but the second one to false?
if(ucHeader[0] == 'G' && ucHeader[1] == 'D' && ucHeader[2] == 'F')
{
// true
......
}
if(ucHeader == GDF_HEADER)
{
false
......
}
When I envoke the watch, both values of ucHeader & GDF_HEADER are identical
|
When acessing a string of char's, the variable name is the address of the first element. So, you
are comparing two addresses which are no the same.
try :
if(strcmp(ucHeader, GDF_HEADER))
ShowMessage("They are different");
else
ShowMessage("They are the same");
Also be careful here:
fread(ucHeader, sizeof(ucHeader), 1, fp); // dangerous if it is a string, ok if not
fread(ucHeader, sizeof(ucHeader)-1, 1, fp); // safe
ucHeader[sizeof(ucHeader)-1] = 0; // terminate the string or it could get ugly
or
memset(ucHeader, 0, sizeof(ucHeader)); // fill it with NULL
fread(ucHeader, sizeof(ucHeader)-1, 1, fp); // Leave at least the NULL on the end
--
-Michael Gillen
|
|
| Back to top |
|
 |
salamander1965 Guest
|
Posted: Wed Jun 29, 2005 11:07 pm Post subject: Re: strcmp not working???? |
|
|
| Quote: | "salamander1965" <salamander1 (AT) verizon (DOT) net> wrote in message
news:42c32265$1 (AT) newsgroups (DOT) borland.com...
if (strcmp(ucHeader, GDF_HEADER)==0)
Like you said, ucHeader is not null-terminated, so you should use strncmp()
instead (which you should use anyway since strcmp() is not safe in general):
if( strcmp(ucHeader, GDF_HEADER, 3) ==0 )
|
Your solution is clearly better than mine if for no other reason than
you don't have to manually put in a terminating null, and as you point
out, there are other reasons. I'm curious, though, about strcmp() not
being safe -- could you explain?
-- salamander
|
|
| Back to top |
|
 |
Remy Lebeau (TeamB) Guest
|
Posted: Wed Jun 29, 2005 11:24 pm Post subject: Re: strcmp not working???? |
|
|
"salamander1965" <salamander1 (AT) verizon (DOT) net> wrote
| Quote: | I'm curious, though, about strcmp() not being safe -- could you explain?
|
My bad, strcpy() is the bad function to use. It is subject to buffer
overflowing, which is a common attack that hackers like to exploit.
Although, strcmp() is only not a very good function compared to strncmp(),
since strcmp() does not do any bounds checking, which is strcpy()'s weeknees
as well. If you have a buffer of N characters and compare it to a string of
M characters, where M > N, then the function can go outside the bounds of
the buffer and examine surrounding memory, which may or may not cause
problems.
Gambit
|
|
| Back to top |
|
 |
Bob Gonder Guest
|
Posted: Wed Jun 29, 2005 11:39 pm Post subject: Re: strcmp not working???? |
|
|
Stephane Baillargeon wrote:
| Quote: | #define GDF_HEADER "GDF"
unsined char ucHeader[3];
fread(ucHeader, sizeof(ucHeader), 1, fp);
if(ucHeader == GDF_HEADER)
|
What everyone else said about why it doesn't work.....
They're right.
Just thought I'd drop in yet another way to compare the values, which
is faster, but I'm sure, frowned upon by the better-trained C/C++
programmers out there.
First, make the array 4 bytes instead of three
unsinged char ucHeader[4];
Then, after you have read 3 chars and set the 4th to zero.....
if( *(DWORD*)ucHeader == 'GDF' )
Notice that 'GDF' is not a string. It is a character constant.
No calls, a single hardware compare. Doesn't get much faster than
that.
|
|
| Back to top |
|
 |
Bob Gonder Guest
|
Posted: Wed Jun 29, 2005 11:44 pm Post subject: Re: strcmp not working???? |
|
|
Remy Lebeau (TeamB) wrote:
| Quote: | Although, strcmp() is only not a very good function compared to strncmp(),
since strcmp() does not do any bounds checking, which is strcpy()'s weeknees
as well. If you have a buffer of N characters and compare it to a string of
M characters, where M > N, then the function can go outside the bounds of
the buffer and examine surrounding memory, which may or may not cause
problems.
|
If M and N are both zero-terminated, how can that be?
It is supposed to stop at the first null in either string.
The danger is if the logically shorter string isn't null terminated.
|
|
| Back to top |
|
 |
salamander1965 Guest
|
Posted: Wed Jun 29, 2005 11:58 pm Post subject: Re: strcmp not working???? |
|
|
| Quote: | If you have a buffer of N characters and compare it to a string of
M characters, where M > N, then the function can go outside the bounds of
the buffer ...
|
....unless the buffer of length N is zero-terminated, right? So the
problem with strcmp() comes about if one of the two char arrays being
compared is not a zero-terminated string (and also is the shorter of the
two).
I'm not trying to argue, just making sure I understand the problem. I
have some inline assembly that started off as a customized strcmp(), but
actually now more closely resembles strncmp() (although it's still
customized to my needs). After your comments, I want to go back through
it to make sure I have sufficient bounds checking.
-- salamander
|
|
| Back to top |
|
 |
Gillmer J. Derge [TeamB] Guest
|
Posted: Thu Jun 30, 2005 12:04 am Post subject: Re: strcmp not working???? |
|
|
salamander1965 wrote:
| Quote: | ...unless the buffer of length N is zero-terminated, right? So the
problem with strcmp() comes about if one of the two char arrays being
compared is not a zero-terminated string (and also is the shorter of the
two).
|
Right. Which IMHO isn't a problem at all. I don't think any of the
string functions that expect zero-terminated strings will work on a non
zero-terminated string. Is that a surprise to anyone? sin and cos
won't work on char*'s. Should they be avoided?
--
Gillmer J. Derge [TeamB]
|
|
| Back to top |
|
 |
Liz Albin Guest
|
Posted: Thu Jun 30, 2005 1:17 am Post subject: Re: strcmp not working???? |
|
|
On Wed, 29 Jun 2005 18:10:05 -0400, Stephane Baillargeon wrote:
| Quote: | if(ucHeader == GDF_HEADER)
{
false
......
}
|
Of course that's false.... you're comparing two different pointers
and btw, ucHeader should be of length 4
--
Good luck,
liz
|
|
| Back to top |
|
 |
Hans Galema Guest
|
Posted: Thu Jun 30, 2005 9:11 am Post subject: Re: strcmp not working???? |
|
|
Bob Gonder wrote:
| Quote: | unsinged char ucHeader[4];
Then, after you have read 3 chars and set the 4th to zero.....
if( *(DWORD*)ucHeader == 'GDF' )
Notice that 'GDF' is not a string. It is a character constant.
|
But three characters are implicitly converted to 4 bytes. The
statement
if( *(DWORD*)ucHeader == 'GDF ' )
behaves the same. But now the position of the zero is
clearly seen.
I do not understand that the zero is implicitly added as fourth byte.
Why not like ' GDF' ?
Hans.
|
|
| Back to top |
|
 |
Hendrik Schober Guest
|
Posted: Thu Jun 30, 2005 9:35 am Post subject: Re: strcmp not working???? |
|
|
Gillmer J. Derge [TeamB] <spam (AT) gillmerderge (DOT) com> wrote:
| Quote: | salamander1965 wrote:
...unless the buffer of length N is zero-terminated, right? So the
problem with strcmp() comes about if one of the two char arrays being
compared is not a zero-terminated string (and also is the shorter of the
two).
Right. Which IMHO isn't a problem at all. I don't think any of the
string functions that expect zero-terminated strings will work on a non
zero-terminated string. Is that a surprise to anyone? sin and cos
won't work on char*'s. Should they be avoided?
|
You have to try hard to manage to pass a 'char*'
to 'sin()'. It's relatively easy to pass a string
without a terminating ' ' to the C lib string
functions. Also, with passing garbage to 'sin()',
I think the worst that could happen is that you'll
get garbage results. With passing non-terminated
strings to the string functions, you might mess
things up badly.
Schobi
--
[email]SpamTrap (AT) gmx (DOT) de[/email] is never read
I'm Schobi at suespammers dot org
"Coming back to where you started is not the same as never leaving"
Terry Pratchett
|
|
| Back to top |
|
 |
Bob Gonder Guest
|
Posted: Thu Jun 30, 2005 2:04 pm Post subject: Re: strcmp not working???? |
|
|
Hans Galema wrote:
| Quote: | But three characters are implicitly converted to 4 bytes. The
statement
if( *(DWORD*)ucHeader == 'GDF ' )
behaves the same. But now the position of the zero is
clearly seen.
I do not understand that the zero is implicitly added as fourth byte.
Why not like ' GDF' ?
|
Because Intel is little endian?
Leading would be multiplying the constant by 16.
'GDF' is 0x00464347
F D G
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|