Skip to content

Don't use std::string_view with APIs expecting null-terminated strings #444

@ZXShady

Description

@ZXShady
template <class T, class... Ps>
Result<T> read(const std::string_view _xml_str) {
  pugi::xml_document doc;
  const auto result = doc.load_string(_xml_str.data());
  if (!result) {
    return error("XML string could not be parsed: " +
                 std::string(result.description()));
  }
  const auto var = InputVarType(doc.first_child());
  return read<T, Ps...>(var);
}

https://github.com/getml/reflect-cpp/blob/main/include%2Frfl%2Fxml%2Fread.hpp#L32-L42

doc.load_string expects a null terminated string so it knows the length, this could easily lead to security vulnerabilities with non-null yerminated inputs

Solutions:

  • Take const std::string& as parameter instead. (cheap if user already passes a std::string but limits the user from passing pther string types implicitly.

  • Keep std::string_view in parameter but construct a std::string inside the function (more expensive)

  • Use another function internally that accepts a const char* str,std::size_t len pair

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions