Re: [newbie] strcpy, strtok and strcat problem...
- From: pere.noel@xxxxxxxxxxxxxxxxxxx (Une bévue)
- Date: Thu, 31 Aug 2006 21:02:59 +0200
Michael Mair <Michael.Mair@xxxxxxxxxxxxxxx> wrote:
You have uninitialised pointers, which may contain a random
valid address or a null pointer or something not a valid pointer
representation at all.
right ! i've found that in between...
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;
i've put a constant size 512 for a full path and 256 for a relative one,
even if target_path_cpy does have the same length, at start, than
target_path it might be longer (or shorter afterwards.
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.
yes, thanks a lot, however i've a question about freeing memory the
skeleton of my prog being :
VALUE m_raliasrecord_init(VALUE self, VALUE from_path, VALUE
target_path)
{
computes all the internal values
rb_iv_set(self, "@from_path", from_path);
//<the same for all the values>
return self;
}
BUT the values as from_path are used elsewhere like that :
VALUE m_from_path(VALUE self) {
return rb_iv_get(self, "@from_path");
}
(rb_iv_set/get are specific to ruby.h)
part of ruby.h :
VALUE rb_iv_get(VALUE, const char*);
VALUE rb_iv_set(VALUE, const char*, VALUE);
am i sure that, after deallocating memory for from_path in
"m_raliasrecord_init" , for example, i'll get the correct value in
"m_from_path" ???
--
une bévue
.
- Follow-Ups:
- Re: [newbie] strcpy, strtok and strcat problem...
- From: Michael Mair
- Re: [newbie] strcpy, strtok and strcat problem...
- References:
- [newbie] strcpy, strtok and strcat problem...
- From: Une bévue
- Re: [newbie] strcpy, strtok and strcat problem...
- From: Michael Mair
- [newbie] strcpy, strtok and strcat problem...
- Prev by Date: Re: [Newbie] changing allocated memory
- Next by Date: Re: [newbie] strcpy, strtok and strcat problem...
- Previous by thread: Re: [newbie] strcpy, strtok and strcat problem...
- Next by thread: Re: [newbie] strcpy, strtok and strcat problem...
- Index(es):
Relevant Pages
|