This requires both a mechanical malfunction of the gun, and violation of the four rules of gun safety. There are various wordings of the four rules, but they are basically the same:
When handling a gun:
All guns are always loaded
This means that the other rules always apply--Never say "Don't worry, it isn't loaded" as an excuse to do something that wouldn't be safe with a loaded gun. If someone checks to see that a gun is not loaded, then hands it to you, you should check to make sure it is unloaded, then follow the rest of the rules anyhow.Never point a gun at anything you are not willing to destroy
This doesn't mean you must want to destroy everything you point at, but rather if you cannot live with the consequences of destroying something, don't point a gun at it.Know your target and what is beyond
You must know where the bullet is going to wind up, and what it may destroy on the way there. Something that blocks your view, but will not completely stop a bullet does not relieve you of this responsibility.Keep your finger off the trigger until your sights are on target
If you are pulling the trigger of an unloaded gun, you still must choose a safe target and aim at that target. The target can be "the ground" rather than "that particular spot of ground".
Guns don't "just go off". Guns go off when handled. In almost all cases, they go off only when their triggers are pulled, in a few extremely rare cases they will go off when dropped (Almost all guns less than 30 years old are designed to be drop safe) or when decockers or other controls are used. They don't go off in a proper holster, or when sitting on a table or shelf. For this reason, I believe that guns that will be loaded often should be left loaded, but in a locked container--Less handling, less risk.