behrang / YamlSwift

Load YAML and JSON documents using Swift
MIT License
404 stars 97 forks source link

Stack overflow when parsing larger yaml structures #26

Open jpsim opened 8 years ago

jpsim commented 8 years ago

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!

norio-nomura commented 8 years ago

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

behrang commented 8 years ago

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.

jpsim commented 8 years ago

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.

behrang commented 8 years ago

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.

jpsim commented 8 years ago

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.

behrang commented 8 years ago

Can you please test again with parsec branch?

jpsim commented 8 years ago

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

seivan commented 7 years ago

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