borrow clamp function from node since buffer.slice in v0.8 doesn't have such logic.