Fix bug in buffer decoding that failed to trim length