Fix buffer extraction inside an if() and add a unit test