Skip to content

Sequence parser overwrites std::u32string #327

Description

@slavaandrejev

Consider the following example.

#include <print>
#include <string>

#include <boost/parser/parser.hpp>

namespace bp = boost::parser;

int main(int argc, char *argv[]) {
    auto input = std::string{"ab"};
    auto res   = std::string();
    const auto result = bp::parse(
        input
      , (bp::char_ >> bp::char_)
      , res
      );
    if (result) {
        std::print("Parse successful\n");
        std::print("{}\n", res);
    } else {
        std::print("Parse failed\n");
    }
}

It outputs

Parse successful
ab

Now consider this change

#include <print>
#include <ranges>
#include <string>

#include <boost/parser/parser.hpp>
#include <boost/parser/transcode_view.hpp>

namespace bp = boost::parser;
namespace rs = std::ranges;

int main(int argc, char *argv[]) {
    auto input = std::u32string{U"ab"};
    auto res   = std::u32string();
    const auto result = bp::parse(
        input
      , (bp::char_ >> bp::char_)
      , res
      );
    if (result) {
        std::print("Parse successful\n");
        std::print("{}\n", res | bp::as_utf8 | rs::to<std::string>());
    } else {
        std::print("Parse failed\n");
    }
}

It outputs

Parse successful
b

The issue is here:

                    if constexpr (detail::is_nope_v<attr_t>) {
                        // nothing to do
                    } if constexpr (
                        (!out_container ||
                         !std::is_same_v<just_x, just_out>) &&
                        std::is_assignable_v<just_out &, just_x &&> &&
                        (!std::is_same_v<just_out, std::string> ||
                         !std::is_integral_v<just_x>)) {
                        detail::assign(out, std::move(x));
                    } else {
                        detail::move_back(
                            out, std::move(x), detail::gen_attrs(flags));
                    }

This code adds to a string only if it is an std::string. Otherwise, it assigns every character to the same string.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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