Hey! I'll try to clarify the things you asked about.
It seems fairly straightforward but somewhat counterintuitive that in bar 22, the natural for C6 is to make sure we don't continue on with the flat one from the previous measure. But since it's a different measure, it should ideally be in parentheses, as a courtesy accidental. Unless the tie across a measure boundary somehow annuls that convention. Notably, while the link and the .png are from a Soviet edition, I found a (seemingly) French edition which did have the parentheses. (Still, in bar 57 of the Soviet edition, there's an accidental with parens as well so it's not like they had a different convention.)
The older the sheet music, the more likely that it doesn't follow modern conventions or might be inconsistent according to modern standards. In this case, the natural sign in front of the c in bar 22 is just to remind you to not play a c flat again, since the first note of the bar is defacto a c flat. So basically a courtesy accidental.
Similarly, for bar 24, I am wondering about the double flats. B5 which is being double-flattened is already pretty flat given des-dur. My understanding has been that the double-flat-on-B5 is played as A5, not A5 flat -- which also seems counterintuitive since there are 2 accidental flats not after a natural + 1 key flat which should theoretically bring it down to A5 flat. (The aforementioned understanding of mine received some justification from measure 56 of the prelude where tripling the flats made zero sense.)
Double flats (and double sharps) are "absolute", so to speak. So if you have a B in the score with a double flat, it ALWAYS becomes an A, regardless of key signature (for example if the key signature contains a B flat and an A flat) or existing accidentals in the bar. The same goes for the E double flat in the left hand in the same bar. It becomes a D natural.
It's a bit like when you put a sharp sign in front of an E in a key signature that includes an F sharp. The E becomes an F natural in this case, and does not get further raised into an F sharp by the key signature. (That kind of defeats the point of writing an E sharp in the first place!)
In this case, the purpose of the double flat is to get a note that sounds like A natural without having to write a natural sign in front of the A to cancel out the A flat in the key signature. This is for music theory/harmony reasons. A short and incomplete answer is that he wants to show that the voices are moving chromatically towards the A flat major chord in bar 25 but haven't arrived at that harmony yet.
Regardless, thanks for looking at this! Also, interestingly, double flats in that fugue are swarming whereas double sharps are hardly any -- which makes sense in terms of consistency and is easier to play, but sort of strange to observe.
You'll most often find double flats in key signatures with many flats, and double sharps in key signatures with many sharps. As you might have guessed by now, this is for music theory/harmony reasons. Basically, the basic chords used to construct music are made from stacks of thirds (look at a C major chord in root position or write one out on a staff to see what I mean), and once you start getting into keys with many sharps or flats, you start needing double sharps or double flats to still be able to spell certain chords as stacks of thirds.
For example, look at a G flat major chord in it's basic form. It contains G flat, B flat, D flat. It looks like a stack of thirds if you write it on a staff. Now, if you want to write a G flat
minor chord, you spell it as G flat, B double flat, D flat - still looks like a stack of thirds. If you write it as G flat, A natural, D flat, it no longer looks like a stack of thirds, and could be confused as some other, incomplete chord.
Hope that helps, and feel free to ask more in depth if you want. I love explaining this sh*t
