Re: [newbie] strcpy, strtok and strcat problem...



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



Relevant Pages

  • Re: [newbie] strcpy, strtok and strcat problem...
    ... use strtok and strcat with it, for another reason, i need a second ... copy of this string, however, after strtok and strcat with the ... ISO9899:1999 "7.21.5.8 The strtok function" ...
    (comp.lang.c)
  • Re: Does strtok require a non-null token?
    ... In other words, if the string has 2 colons in a row, it ... Just one more reason to avoid strtok(). ... That is, for the majority of the reasons I need to tokenized a string, this default behaviour is exactly what I want. ... For those requirements that strtokdoes not fit we have our own internal tokenizing routines. ...
    (comp.lang.c)
  • Re: strtok ( ) help
    ... > splitCommandssomehow modifying the pointer, but I HAVE to call that ... Here's an idea of how to use the strtok() function. ... don't mind trashing the contents of a string s, ... will give you a loop that extracts the tokens one at a time from s. ...
    (comp.lang.c)
  • Re: Does strtok require a non-null token?
    ... In other words, if the string has 2 colons in a row, it ... Just one more reason to avoid strtok(). ... tokenized a string to store in my own array of buffers, ... But I also think that once you understand the limitations and caveats that go along with it, there is no reason not to use it for those cases where it is a good fit. ...
    (comp.lang.c)
  • Re: Does strtok require a non-null token?
    ... In other words, if the string has 2 colons in a row, it ... Just one more reason to avoid strtok. ... For those requirements that strtok() does not fit we have our own ... internal tokenizing routines. ...
    (comp.lang.c)