goto out;
                }
 
+               if (max >= bus->busn_res.end) {
+                       dev_warn(&dev->dev, "can't allocate child bus %02x from %pR\n",
+                                max, &bus->busn_res);
+                       goto out;
+               }
+
                /* Clear errors */
                pci_write_config_word(dev, PCI_STATUS, 0xffff);
 
-               /* Prevent assigning a bus number that already exists.
-                * This can happen when a bridge is hot-plugged, so in
-                * this case we only re-scan this bus. */
+               /* The bus will already exist if we are rescanning */
                child = pci_find_bus(pci_domain_nr(bus), max+1);
                if (!child) {
                        child = pci_add_new_bus(bus, dev, max+1);