# Segmentation fault when extending occursin to handle missing values

**URL:** <https://discourse.julialang.org/t/segmentation-fault-when-extending-occursin-to-handle-missing-values/23421>\
**Category:** General Usage\
**Created:** [April 22, 2019, 11:09pm UTC](https://discourse.julialang.org/t/segmentation-fault-when-extending-occursin-to-handle-missing-values/23421 "2019-04-22T23:09:10Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![kmundnic](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@kmundnic](https://discourse.julialang.org/u/kmundnic)\
**Post date:** [April 22, 2019, 11:09pm UTC](https://discourse.julialang.org/t/segmentation-fault-when-extending-occursin-to-handle-missing-values/23421/1 "2019-04-22T23:09:10Z")

</div>

Minimal working example (for both v1.0.3 and v1.1.0):

```julia
import Base: occursin

function occursin(needle::String, haystack::Union{String,Missing})
  if !ismissing(haystack)
    return occursin(needle, haystack)
  else
    return false
  end
end

@show occursin("foo", missing) # false
@show occursin("foo", "foobar") # segmentation fault:11 in v1.0.3, bus error: 10 in v1.1.0

```

Seems to me that this is a bug. However, I’m wondering if there is something wrong as well when trying to extend the function `occursin` in this way. If I extend the function in the following way, there is no segmentation fault:

```julia
import Base: occursin

function occursin(needle::Union{AbstractString,Regex,AbstractChar}, haystack::Union{AbstractString,Missing})
  if !ismissing(haystack)
    return occursin(needle, haystack)
  else
    return false 
  end
end

@show occursin("foo", missing) # false
@show occursin("foo", "foobar") # true

```

---

<div class="post-metadata">

**Author:** ![tomaklutfu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomaklutfu/32/2411_2.png) [@tomaklutfu](https://discourse.julialang.org/u/tomaklutfu)\
**Post date:** [April 23, 2019, 2:01am UTC](https://discourse.julialang.org/t/segmentation-fault-when-extending-occursin-to-handle-missing-values/23421/2 "2019-04-23T02:01:27Z")

</div>

I think it should be stack overflow in the first one because it calls itself indefinetly. The second one is a fallback definition for `AbstractString` in the first argument. It is just called for `haystack::Missing` but not for `haystack::String` since there is already a more specific method for that. Having said that, the second one also cause stack overflow when used as a fallback method for `haystack isa AbstractString`.
