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.

Bug: 28767796
Change-Id: Ifbac719a7db73aab121cb00c2090edf1bf1094bb
Signed-off-by: Yuan Lin <yualin@google.com>
(cherry picked from commit a0b6ec49e137c57390adfaa4eb74c95473b3aa53)
diff --git a/drivers/char/diag/diagfwd.h b/drivers/char/diag/diagfwd.h
index 8ca8955..b87ab55 100644
--- a/drivers/char/diag/diagfwd.h
+++ b/drivers/char/diag/diagfwd.h
@@ -20,7 +20,7 @@
 #define RESET_AND_QUEUE 1
 
 #define CHK_OVERFLOW(bufStart, start, end, length) \
-	((((bufStart) <= (start)) && ((end) - (start) >= (length))) ? 1 : 0)
+  ((((bufStart) <= (start)) && ((end) - (start) >= (length)) && ((length) > 0)) ? 1 : 0)
 
 void diagfwd_init(void);
 void diagfwd_exit(void);