While linking an epic to a defect, the Sprint field of the defect should take the value of the sprint field of the epic. It should also take care of dynamically updating the sprint field of the defect as and when the sprint of the epic is modified. Let's not touch the existing defects with this solution.
At the time of reopening a closed defect, if the linked epic is closed as well i.e. the status of the linked epic is invalidated/release ready/done state then DON'T allow linking of DEFECT to the EPIC (Info message can be displayed here stating- "Linked EPIC is closed. Please reopen the EPIC before linking or link it to a different but related Open EPIC."). Since we are not allowing the linking with EPIC, it is obvious that Sprint field needs to be edited manually.
If the EPIC is closed i.e. when Epic Status= Done/Release Ready/Validated, then DONT allows linking of DEFECT to the EPIC (Info message can be displayed here stating- " EPIC is closed. Please reopen EPIC before linking").
If EPIC is open, the defect should copy the EPIC's sprint on linking the defect to the EPIC.