Make a scratch dir somewhere, cd into it, and run it. It will download 9.6.7 and 10.2, build them, create a test db in 9.6.7, modify it a bit here and there to get to the desired state, pg_upgrade it to 10.2 and vacuum.
Might even be a good idea to create a regression test with that, but AFAIK there's nothing remotely like that in the current tests.
The patch you propose makes the test succeed. It may be true that there might be other similar issues lurking in that code, but it looks like it fixes this particular issue.
Is it possible, that bug #14852 is of the same nature as the issue at hand here?