This principle was established when Nazis were convicted for war crimes at Nuremburg for violating treaties that their predecessor state the Weimar Republic signed, even after the Nazi's repudiated those treaties and claimed they were signed by an illegitimate state, and that they were a new Reich, not like the Wiemar Republic.
Basically if territory changes hand to an existing state that state will obviously still have obligations, and if a new state is formed, then generally it is assumed to still carry the obligations of the previous state. There is no "one weird trick" to avoid international law. I assure you that the diplomats and lawyers 80 years ago thought of these possibilities. They saw what resulted from the Soviet and Nazi mutual POW slaughters, and set up international law so no one could ignore it.
And more critically - they have successor states.
The Russian Federation is treated as the successor to the USSR in most cases (much to the chagrin of the rest of the CIS) and Serbia is treated as the successor to Yugoslavia (much to the chagrin of the rest)