Fix the coverity issues

This PR is to resolve issues raised by coverity.

BUG=XWALK-2928
You have to think about the design here: if `GetString()` fails, do you want to abort the loop, write an empty string or not write anything at all? In any of those cases, there's no point in calling `SetString()` twice like you did here.