You definitely have memory leak.  because ltrim() and rtrim() will return a copy of the original string, and they didn't free the supplied string, neither did you :-)

If you had add a test, a leak should have been detected right away.