zdzhaoyong / Svar

A Tiny Modern C++ Header Brings Unified Interface for Different Languages
Other
149 stars 17 forks source link

unsafe_as seems lead to segment fault. #8

Closed zdzhaoyong closed 2 years ago

zdzhaoyong commented 2 years ago

run the following command lead to segment fault

svar hello world

The reason seems as below:

const std::vector<Svar>& vec=unsafe_as<std::vector<Svar>>();
        const auto N = vec.size();
        if(N==0)
            return o<<"[]";
        o<<'[';
        std::string indent_front,ident_back("]");
        int new_indent=current_indent+indent_step;
        if(indent_step>=0){
            indent_front = std::string(new_indent+1,' ');
            indent_front[0]='\n';
            ident_back = std::string(current_indent+2,' ');
            ident_back[0]='\n'; ident_back.back()=']';
        }
        for (auto it=vec.begin();it != vec.end();++it)
        {
            if(indent_step>=0)
                o << indent_front;
            it->dump(o,indent_step,new_indent);
            if(it==vec.end()-1)
                o<<ident_back;
            else
                o<<',';
        }
        return o;