Stream: beginners

Topic: ✔ Debugging "segmentation fault"


view this post on Zulip Gerco Dries (Dec 05 2023 at 22:37):

I have a program for AoC day 5 that uses the parser library to grab its input. It works fine on the test input, but fails with "segmentation fault" when running the larger puzzle input. There doesn't seem to be any information associated with this fault. No stack trace, no better error message, nothing I can grab on to and debug.

Through some experimentation I have been able to tell that it's the input parsing causing the error, rather than anything that happens later but that's where I get stuck. Are there any known problems in either "parser" or Roc itself that could explain this behavior?

inputParser : Parser (List U8) Puzzle
inputParser =
    listOfSeeds =
        const \seeds -> seeds
        |> skip (string "seeds:")
        |> skip whitespace
        |> keep listOfNumbers

    almanacMap =
        const
            (\dstStart -> \srcStart -> \length ->
                        range 0 length
                        |> List.walk (Dict.empty {}) \dict, n -> Dict.insert dict (srcStart + n) (dstStart + n))
        |> keep digits
        |> skip whitespace
        |> keep digits
        |> skip whitespace
        |> keep digits
        |> skip whitespace

    combineDicts = \dicts ->
        dicts |> List.walk (Dict.empty {}) Dict.insertAll

    almanac =
        const \s2s -> \s2f -> \f2w -> \w2l -> \l2t -> \t2h -> \h2l -> {
                                    seedToSoil: s2s,
                                    soilToFertilizer: s2f,
                                    fertilizerToWater: f2w,
                                    waterToLight: w2l,
                                    lightToTemperature: l2t,
                                    temperatureToHumidity: t2h,
                                    humidityToLocation: h2l,
                                }
        |> skip (string "seed-to-soil map:")
        |> skip whitespace
        |> keep (map (many almanacMap) combineDicts)
        |> skip (string "soil-to-fertilizer map:")
        |> skip whitespace
        |> keep (map (many almanacMap) combineDicts)
        |> skip (string "fertilizer-to-water map:")
        |> skip whitespace
        |> keep (map (many almanacMap) combineDicts)
        |> skip (string "water-to-light map:")
        |> skip whitespace
        |> keep (map (many almanacMap) combineDicts)
        |> skip (string "light-to-temperature map:")
        |> skip whitespace
        |> keep (map (many almanacMap) combineDicts)
        |> skip (string "temperature-to-humidity map:")
        |> skip whitespace
        |> keep (map (many almanacMap) combineDicts)
        |> skip (string "humidity-to-location map:")
        |> skip whitespace
        |> keep (map (many almanacMap) combineDicts)

    const \se -> \al -> { seeds: se, almanac: al }
    |> keep listOfSeeds
    |> keep almanac

whitespace : Parser (List U8) _
whitespace =
    const \a -> a
    |> skip (many (oneOf [codeunit ' ', codeunit '\t', codeunit '\n']))

listOfNumbers : Parser (List U8) (List Nat)
listOfNumbers =
    const \r -> r
    |> skip whitespace
    |> keep (sepBy digits whitespace)
    |> skip whitespace

view this post on Zulip Luke Boswell (Dec 05 2023 at 22:49):

No issues that I am aware of, this is a compiler bug. I would put my money on List.walk with a Dict

view this post on Zulip Gerco Dries (Dec 05 2023 at 22:49):

I suspect it may have something to do with being out of memory. In the code, I'm expanding dozens of ranges that turn out to be tens millions of elements long in the full test input and I don't have that much memory in my computer. The code compiles and runs just fine with a smaller input.

view this post on Zulip Luke Boswell (Dec 05 2023 at 22:57):

Wouldnt that be a stack overflow though?

view this post on Zulip Luke Boswell (Dec 05 2023 at 22:57):

I dont know

view this post on Zulip Brendan Hansknecht (Dec 05 2023 at 23:08):

my guess. it is out of memory, but instead it returns a bad pionter which roc reads that leads to a segfault...but really need to open it with a debugger

view this post on Zulip Gerco Dries (Dec 05 2023 at 23:11):

Updating the code to not expand the ranges anymore did solve the problem, so I'm thinking out of memory was the correct conclusion.

view this post on Zulip Notification Bot (Dec 06 2023 at 00:34):

Gerco Dries has marked this topic as resolved.


Last updated: Jul 06 2025 at 12:14 UTC