Added check to avoid buffer overflow. am: 821e838a51
am: 8e777da519

Change-Id: Ib8e0dde3dd131069592fda4b5e19926e6b5f596a