diag: Fix possible underflow/overflow issues

Add check in order to fix possible integer underflow
during HDLC encoding which may lead to buffer
overflow. Also added check for packet length to
avoid buffer overflow.

Change-Id: I72858e7625764652571aee3154e3c2eb61655168
CRs-Fixed: 483400
CRs-Fixed: 483408
Signed-off-by: Mohit Aggarwal <maggarwa@codeaurora.org>
Merge "diag: Fix possible underflow/overflow issues"