How to protect your investment in such situations?
If a company whose stock you own goes bankrupt, and it's a problem for you, your stock portfolio is not diversified well enough.
In fact, I would argue you should be prepared for as many as 2-3 companies whose stocks you own going bankrupt.
By understanding into what you invest.
If you invest in a company that has been the market leader for 30 years, chances are it doesn't drop much in value, assuming the valuation at which you purchased the stock seemed reasonable.
The largest investments in a well-diversified portfolio should be just like that: major companies with large market cap. Your investment into them is unlikely to vanish.
By buying more.
If you diversify well, and invest into good companies at a reasonable valuation, and suddenly all of your investments have dropped 50% or so because the market index has dropped, it's a very good chance to move more money from bonds to stocks. It's also a very good chance to reduce your spending so that you have more money for investing.