Add inventory length checks

As inspired by a recent IRC bug report.