Pay attention to buffer relocation on encoding (Bug#9318; backport from trunk).