|
Home > Archive > Unix Programming > July 2005 > strange strcpy() behavior
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
strange strcpy() behavior
|
|
| Roman Mashak 2005-07-20, 2:49 am |
| Hello, All!
I've come across an interesting problem. In my application using sockets, I
was unable to use strcpy() in a 'fork()ed' child.
The code scheme follows:
sockfd=socket(...);
bind(sockfd,...);
listen(sockfd,...);
for (;;) {
newsock=accept(sockfd,...);
if (fork()==0) //new child's born
{
// child process doesn't need it
close(sockfd);
...
recv(newsock,...);
...
strcpy(msg->id, '\0'); // XXX
...
close(newsock);
exit(0); //exit status
}
else // here is parent
{
close(newsock);
}
}
At 'XXX' label I'm filling string fields of my structure and it seems like
strcpy() is hanging and rpogram flow stops at that point. So I changed code
slightly and zero out structure with a memset().
I wonder is it normal behavior of strcpy()?
With best regards, Roman Mashak. E-mail: mrv@tusur.ru
| |
| Rainer Temme 2005-07-20, 7:53 am |
| Roman Mashak wrote:
> Hello, All!
> recv(newsock,...);
> ...
> strcpy(msg->id, '\0'); // XXX
Roman,
- msg might point to illegal memory.
make sure that pointer is either obtained from malloc()
or by an assignment like msg=&my_structure;
- msg->id might be an uninitialized variable...for the case that
this variable is of pointer type as well. It might just point
somewhere. again .. make sure it is from malloc() or from &variable,
or is id[something].
- the second argument in strcpy() is plain wrong.
This shouldn't even compile. If it's a typo ... and should read "\0"
ok, but '\0' will never work. That would be a "char" where strcpy
expects a "char *".
Regards ... Rainer
| |
| Ulrich Hobelmann 2005-07-20, 7:53 am |
| Roman Mashak wrote:
> strcpy(msg->id, '\0'); // XXX
[...]
> At 'XXX' label I'm filling string fields of my structure and it seems like
> strcpy() is hanging and rpogram flow stops at that point. So I changed code
> slightly and zero out structure with a memset().
If you're trying to memset all bytes in msg->id to 0, NULL, or
'\0', memset is right (or bzero), and strcpy isn't.
strcpy copies one string to another, memset sets all bytes to the
same 'character' (or byte).
--
XML is a prime example of retarded innovation.
-- Erik Meijer and Peter Drayton, Microsoft Corporation
| |
| Floyd L. Davidson 2005-07-20, 7:53 am |
| "Roman Mashak" <mrv@tusur.ru> wrote:
> strcpy(msg->id, '\0'); // XXX
....
>At 'XXX' label I'm filling string fields of my structure and it seems like
>strcpy() is hanging and rpogram flow stops at that point. So I changed code
>slightly and zero out structure with a memset().
>
>I wonder is it normal behavior of strcpy()?
Yep. Every time try to give it an integer value (e.g., '\0')
where it expects to see a pointer to char, it will segfault. :-)
The question is, have you included the string.h header? And
do you have warnings enabled on your compiler? Obviously not,
or you would have been warned about an invalid parameter...
Try this:
strcpy(msg->id, "");
or better still, either of
*msg->id = '\0';
or
msg->id[0] = '\0';
Those of course assume that msg->id is either a pointer to valid
memory or an array.
--
Floyd L. Davidson <http://web.newsguy.com/floyd_davidson>
Ukpeagvik (Barrow, Alaska) floyd@apaflo.com
| |
| Casper H.S. Dik 2005-07-20, 7:53 am |
| "Roman Mashak" <mrv@tusur.ru> writes:
>Hello, All!
>I've come across an interesting problem. In my application using sockets, I
>was unable to use strcpy() in a 'fork()ed' child.
>The code scheme follows:
>sockfd=socket(...);
>bind(sockfd,...);
>listen(sockfd,...);
>for (;;) {
> newsock=accept(sockfd,...);
> if (fork()==0) //new child's born
> {
> // child process doesn't need it
> close(sockfd);
> ...
> recv(newsock,...);
> ...
> strcpy(msg->id, '\0'); // XXX
You're passing a NULL pointer to strcpy; this causes a segmentation
fault. (A '\0' is a NUL character constant; as this is an integer 0 constant,
this is promoted to a NULL pointer through the strcpy() prototype)
If you want to clear the whole msgid, you will need to use memset
(or strncpy).
Casper
--
Expressed in this posting are my opinions. They are in no way related
to opinions held by my employer, Sun Microsystems.
Statements on Sun products included here are not gospel and may
be fiction rather than truth.
| |
| Casper H.S. Dik 2005-07-20, 7:53 am |
| Rainer Temme <Rainer.Temme@NoSpam.Siemens.Com> writes:
>- the second argument in strcpy() is plain wrong.
> This shouldn't even compile. If it's a typo ... and should read "\0"
> ok, but '\0' will never work. That would be a "char" where strcpy
> expects a "char *".
No, it compiles alright, it's a NULL pointer constant.
Casper
--
Expressed in this posting are my opinions. They are in no way related
to opinions held by my employer, Sun Microsystems.
Statements on Sun products included here are not gospel and may
be fiction rather than truth.
|
|
|
|
|