Fix potential out-of-bound memory access

Issue discovered by Coverity scan, CID 43165.