There must be a range after which Ingenuity cannot communicate with Perseverance. I am not sure what that distance is. How Ingenuity ensures it does not fly away out of that distance? If inadvertently Ingenuity flies away, what’s the back up plan to regain connection?
@uhoh notes that its Zigbee communication range exceeds 1 km.
Ingenuity flies not much longer than 120 seconds, at a speed of not much more than 13 km/h, so even a straight-line flight directly away from Perseverance could not be much more than 0.44 km. So it would take several days (well, sols) for comms to be lost. That's slow enough for mission planners to notice and give Ingenuity new orders.
Each time before Ingenuity is allowed to fly, it must pass an elaborate set of preflight tests. Those ensure, among many other things, that a software malfunction won't let it deviate from its preprogrammed flight path, which has been of course chosen to stay within communication range of Perseverance.