|
From: Ethan A M. <merritt@u.washington.edu> - 2006-07-13 15:14:13
|
On Thursday 13 July 2006 12:43 am, Juergen Wieferink wrote:
>
> While in it, the snippet:
>
> v = add_udv_by_name("GPVAL_TERM");
> if (v) {
> v->udv_undef = FALSE;
> Gstring(&v->udv_value,(char*)term->name); /* this can be pointer */
> }
>
> looks dangerous to me. The string a variable points to may be freed
> if the variable is reset.
I take it that you are worried some other bit of code might call
gpfree_string(add_udv_by_name("GPVAL_TERM")), thus triggering
an error when it tries to free the static allocation of term->name?
The GPVAL_* variables cannot be over-written by the user,
and this one bit of code is the only place that gnuplot itself
sets this variable. So it is safe.
> Would an additional function Gstring_copy() be useful, which could be called
> in places like this?
I don't think that is necessary. You can already do this easily:
Gstring(&v->udv_value, gp_strdup(term->name));
Then you would have to modify the snippet of code above to deal with the
opposite problem, that unless the previous name is explicitly freed there
will be a memory leak.
--
Ethan A Merritt
Biomolecular Structure Center
University of Washington, Seattle 98195-7742
|