fktn-k / fkYAML

A C++ header-only YAML library
MIT License
67 stars 7 forks source link

Infinity loop when trying to deserialize non existing file #378

Closed sndth closed 1 month ago

sndth commented 1 month ago

Description

When you try to load a file that doesn't exist (using std::ifstream), InputType will be null and from what I saw, this isn't checked anywhere. I suppose this should be checked inside input_adapter function:

https://github.com/fktn-k/fkYAML/blob/93b0e43abecbfb8025aeeb835eba57358c604f47/include/fkYAML/detail/input/input_adapter.hpp#L926-L932

this issue doesn't occur when std::FILE* is used, because:

https://github.com/fktn-k/fkYAML/blob/93b0e43abecbfb8025aeeb835eba57358c604f47/include/fkYAML/detail/input/input_adapter.hpp#L915-L924

we have condition to check file pointer inside input_adapter for std::FILE*

Reproduction steps

  1. Use code from tutorial (https://fktn-k.github.io/fkYAML/tutorials/#__tabbed_1_2)
  2. Make sure file doesn't exist
  3. Compile and test = infinity loop because stream is empty

Expected vs. actual results

Expected result is throw fkyaml::exception, currently we have infinity loop

Minimal code example

No response

Error messages

No response

Compiler and operating system

Windows: C++latest, MSVC latest

Library version

0.3.9/0.3.10

Validation

fktn-k commented 1 month ago

@sndth
Thanks for filing the issue.
You're right.
The input_adapter for std::istream should behave like that for FILE* if a file isn't opened by a library user, i.e., throw an fkyaml::exception.
That's much much better and reasonable as an error handling behavior as well.

By the way, do you want to fix it (because you've already found a way to resolve it)?
Or I can do it tonight.
Either is fine to me.

sndth commented 1 month ago

I prepared PR which fixes the problem

fktn-k commented 1 month ago

@sndth
Thanks for the fix!
I've merged the PR since everything your work looks fine (some unrelated CI issues happened though).