remove redundant casts
move buffer allocation into 'if' scope