* You're considering any WAL file with a power of 2 as valid. Suppose, the correct WAL seg size is 64mb. For some reason, the server generated a 16mb invalid WAL file(maybe it crashed while creating the WAL file). Your code seems to treat this as a valid file which I think is incorrect. Do you agree with that?
Detecting correct WAL size based on the size of a random WAL file seems like a really bad idea to me.
I also don't see the reason for #2... or is that how initdb writes out the correct control file?
The initdb module reads the size from the option provided and sets the environment variable. This variable is read in src/backend/access/transam/xlog.c and the ControlFile written.
Unlike pg_resetwal and pg_rewind, pg_basebackup cannot access the Control file. It only accesses the wal log folder. So we get the XLogSegSize from the SHOW command using replication connection.
As Kuntal pointed out, I might need to set it from pg_receivewal.c as well.