sanitize inputs. verify mallocs.

Change-Id: If5d7a51ca56c4dba6948d2f405f41721a15a16b6