It looks like this is also a bug in ../gatchat/gatio.c ( and doesn't look like it's been fixed upstream either ).

As this is unrelated to the rildev code, perhaps you could submit this separately?  Did you find this running valgrind?