Skip to content

Infinity loop when trying to deserialize non existing file #378

@sndth

Description

@sndth

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:

/// @brief A factory method for stream_input_adapter objects with std::istream objects.
/// @param stream An input stream.
/// @return stream_input_adapter A stream_input_adapter object.
inline stream_input_adapter input_adapter(std::istream& stream) noexcept {
utf_encode_t encode_type = detect_encoding_and_skip_bom(stream);
return stream_input_adapter(stream, encode_type);
}

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

/// @brief A factory method for file_input_adapter objects with C-style file handles.
/// @param file A file handle.
/// @return file_input_adapter A file_input_adapter object.
inline file_input_adapter input_adapter(std::FILE* file) {
if (!file) {
throw fkyaml::exception("Invalid FILE object pointer.");
}
utf_encode_t encode_type = detect_encoding_and_skip_bom(file);
return file_input_adapter(file, encode_type);
}

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions