Re: [newbie] strcpy, strtok and strcat problem...
- From: Michael Mair <Michael.Mair@xxxxxxxxxxxxxxx>
- Date: Thu, 31 Aug 2006 20:31:58 +0200
Une bévue schrieb:
in order not to change an input string i strcpy it to be able to use
strtok and strcat with it, for another reason, i need a second copy of
this string, however, after strtok and strcat with the first, the second
does have exactly the same value, here is the small piece of code for
that part :
--- part of RAliasRecord.c --------------------------------------------
VALUE m_raliasrecord_init(VALUE self, VALUE from_path, VALUE
target_path)
{
char *target_path_cpy, *target_path_cpy2;
You have uninitialised pointers, which may contain a random
valid address or a null pointer or something not a valid pointer
representation at all.
target_path_cpy = strcpy(target_path_cpy,
StringValuePtr(target_path));
target_path_cpy2 = strcpy(target_path_cpy2,
StringValuePtr(target_path));
Now you try to copy something to a storage location you
_cannot_ own.
There are non-Standard-C extensions like strdup() which
duplicate a string and provide the necessary memory; these
can be used instead of the above.
Or you can get the memory yourself previous to the call to
strcpy():
char *target_path_cpy, *target_path_cpy2;
char *temp = StringValuePtr(target_path);
size_t size = strlen(temp) + 1;
target_path_cpy = malloc(size);
target_path_cpy2 = malloc(size);
if (NULL == target_path_cpy
|| NULL == target_path_cpy2) {
free(target_path_cpy);
free(target_path_cpy2);
return INVALID_VALUE; /* or whatever you do to indicate failure */
}
strcpy(target_path_cpy, temp);
strcpy(target_path_cpy2, temp);
There you go...
And at the end of m_raliasrecord_init(), you call
free(target_path_cpy);
free(target_path_cpy2);
before returning the VALUE return value.
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
.
- Follow-Ups:
- Re: [newbie] strcpy, strtok and strcat problem...
- From: Une bévue
- Re: [newbie] strcpy, strtok and strcat problem...
- References:
- [newbie] strcpy, strtok and strcat problem...
- From: Une bévue
- [newbie] strcpy, strtok and strcat problem...
- Prev by Date: Re: c code reusability
- Next by Date: Re: strncpy not that easy to use
- Previous by thread: Re: [newbie] strcpy, strtok and strcat problem...
- Next by thread: Re: [newbie] strcpy, strtok and strcat problem...
- Index(es):
Relevant Pages
|