I think your analysis is correct, and your suggestions good. Go ahead and work on a patch if you want. FYI the code was done the way it was for speed; it avoids calling malloc/free more than needed.