make sure this is never called with 0 length

Change-Id: Ic7726c1fbfd58ae2e0a78c203e426deedc2c461b
CID#1130378 rearrange a bit

Change-Id: I2e8470ca67b8ee43f843bdd1cdc1ce949e9df05a
CID#1135330 check return values

Change-Id: I21e9621160e72f45d0b1ab39e58216cdd8a743bb