Fix a possible buffer overflow.

It is not optimal, it can be done in better way.