How to remove the artifacts when blur on the edge

When apply blur on the edge of the pic2 because there is a cut-off on the edge, so the blurred edge will have the effect show in pic1.
I wonder how could I remove the artifacts (not sure it’s proper to call it this) in the pic1.
I know pad the pic2’s edge with the value on the edge will prevent this.
I wonder is there a way to do that if I have the mask to tell which part is 255(the white part) and which part is valid data?