Fix potential issues caused by implicit narrowing conversion.

Might fix issue #8596.
I figured out that it might be the cause, so I turned on gcc's ```-Wconversion``` to look for other places this might be an issue.