Fix bounds checking errors

These errors were found by differential comparison to the new Go
reference code.