After looking at that some more, I confirm, no bug here, or at least no other bug than the fact that it saved only changed inputs. I looked at your example with kof97, you look at button 1 & 2 with the joystick, except these 2 are NOT saved in your games.cfg, it's readable, you can check it, you have :
kof97:keyconfig]
use_custom_keys = 1
Def_P1_Up_joystick = 2049
Def_P1_Down_joystick = 2305
Def_P1_Left_joystick = 1537
Def_P1_Right_joystick = 1793
Def_P1_Button_3_joystick = 262145
Def_P1_Button_4_joystick = 327681
Player1_B1_B2_B3_joystick = 393217
Def_P1_Start_joystick = 655361
Def_Coin_A_joystick = 589825
So nothing specific about button1 or button 2, so they were just overwritten when you changed the default config, normal.
It required someone making a heavy use of custom inputs to realize this, and even for you it took you quite some time. I even remember why I chose to do that this way the 1st time : because it was creating smaller config files ! I didn't realize that it would create problems !
Anyway now all the inputs are saved when you use custom inputs, so you won't see that again, you might want to reload all these games so that they update their config in games.cfg and after that you'll be fine.
But by looking at that there is a weird config saved for you : [$ascii_controller:keyconfig], this one doesn't make any sense, but I don't know how it came here, nor when. I had something similar in mine, it looked like [$6_buttons_sf2:keyconfig], weird too, normally the 1st part before the : should only contain the short name. So I don't know if it's an old bug which was fixed a long time ago and just left some traces in games.cfg, or if it's a bug which is still somewhere now...
I guess we'll need to wait and see for this one. I deleted this weird config in mine.
I also had a dipswitches config saved in the middle of a key config, this should never happen too normally. You don't have this in yours, here again it might be something very old, and I even found a part specific to the dos version in mine, so this file was used in dos too... oh well I deleted this part too, I guess I'll have to check it from time to time then...