Fix a buffer handling bug when adding lots of registry keys