I've attched both fixed-size and dynamic-allocation patch (but simplified without goto).
The dynmaic[sic] version incorrectly passes sizeof(buf), where buf is char *, as the size of buffer in the "happy case" leading to unnecessary hits to the dynamic path.
-- Santtu
