YamlSwift icon indicating copy to clipboard operation
YamlSwift copied to clipboard

Stack overflow when parsing larger yaml structures

Open jpsim opened this issue 8 years ago • 8 comments

Looks like the recursive nature of the parser makes it quite susceptible to stack overflows when parsing larger yaml structures. Not that ~500 lines is that large for a yaml file...

You can reproduce this by Yaml.loading this file, which causes a stack overflow on my mac (see this backtrace of ~900 YamlSwift stack frames).

I'm not familiar enough with the algorithm used here to avoid the recursion, which seems to be the only way to truly avoid this issue. Quite interested in hearing your thoughts @behrang!

jpsim avatar May 23 '16 00:05 jpsim

As I tested, debug build of sourcekitten crashed. But release build of sourcekitten did not crash.

norio-nomura avatar May 23 '16 05:05 norio-nomura

I tested the file you provided, and interestingly, it worked. However, I have to make it a lot better as it is now used in many good projects like yours. I'm working on something a lot better.

behrang avatar May 23 '16 06:05 behrang

As with most stack overflow issues, this will depend on your environment, so it's entirely possible the exact file linked to above didn't trigger one for you. You might need to experiment with larger structures to hit this.

jpsim avatar May 23 '16 15:05 jpsim

I replicated your file 4 times under another keys to make it bigger and it loaded successfully, although it took a long time.

As I said, I'm working on something completely different. Hope I can put enough time on it.

behrang avatar May 24 '16 08:05 behrang

Sounds great, though as we discovered in jpsim/SourceKitten#211, I can't reproduce this when compiling Swift with optimizations, so it's not a pressing issue on my end.

jpsim avatar May 24 '16 15:05 jpsim

Can you please test again with parsec branch?

behrang avatar Nov 20 '16 08:11 behrang

I can't speak to the stack overflow yet, I think that's resolved. But the performance is both considerably better on Linux and considerably worse on macOS: jpsim/SourceKitten#289

jpsim avatar Nov 20 '16 17:11 jpsim

@jpsim Does this still happen on the Swift 4 branch?

seivan avatar Sep 20 '17 11:09 seivan