Improved robustness of packet handling code to malformed packets