Subject Re: [firebird-support] UDF + FREE_IT + DISCONNECT
Author unordained
---------- Original Message -----------
From: Antoine Chevrette <antoine.chevrette@...>
> *_declspec(dllexport) char* fn_ltrim(char* s){
> char *c;
> c = ib_util_malloc(strlen(s)+ 1);
> c = s;
> if ( *s == VIDE ) return c;*
> **
> * while (*c && *c == BLANC) c++;
> *
> * return c;
> }*
>
> Antoine
------- End of Original Message -------

There are LTRIM functions available in FB already, no? Check ib_udf.sql for
existing definitions.

But, looking at your code: you alloc 'c', but then use 'c = s' instead of strcpy
(), blowing away the pointer to the memory you just allocated, and the follow-up
free_it() will be applied not to the memory you allocated, but to memory firebird
had previously allocated & passed to you, and is already freeing for other
reasons (double-freeing.) Also, if you move the 'c' pointer along the string to
return a pointer to the beginning of the 'useful' text, free_it() will receive a
pointer not to the originally-allocated memory, but to some sub-chunk thereof,
which will likely break it. What you should do is walk the 's' string until you
find a valid character, find how many characters you actually need to copy,
allocate enough for that, copy, then return; or you can initially allocate as
much as is in 's' (as you currently do), then only copy the relevant portion, set
the null-terminator before the end of the allocated memory, and return that.
Regardless, what you return needs to stay equal to what you got back from
ib_util_malloc().

-Philip