They don't live to 40 years in the wild. There is little information as to the lifespan in the wild, but in many cases fish won't live as long in the wild as in captivity. The oldest Corydoras that Ian Fuller reported seeing in captivity was a 42 years old Corydoras aeneus, but that was an exceptional case. The oldest Corydoras that Ian Fuller has personally owned were Corydoras sterbai that lived into their 20s. Other than that, there are reports of many living into their twenties, and some as high as 34 years. I've only seen the one claim from Ian as to Cordydoras living over 40.
Any Corydoras that has lived into their teens and twenties is a Corydoras that has been cared for properly. Most won't live into their twenties whether they have been cared for correctly or not.