Don't you also return the number of produced chars in the buffer?