[property_tree] Issue: erase is corrupting the tree

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

[property_tree] Issue: erase is corrupting the tree

Boost - Dev mailing list
I have following JSON structure loaded into boost::property_tree:
{
    "animals" : ["fox","dog","cat"]
}

I'm trying to delete the second item from the array by:
        auto assoc_it = loadedJSON.find("animals");
        auto it = loadedJSON.to_iterator(assoc_it);
        it = it->second.begin();
        it++; // move to second item
        loadedJSON.erase(it);

After this, when I try to use boost::property_tree::write_json to write the JSON back the app crashes. Do I use the property tree in a wrong way, or is there a bug?

Thanks,
Lukas.

_______________________________________________

This message is for information purposes only, it is not a recommendation, advice, offer or solicitation to buy or sell a product or service nor an official confirmation of any transaction. It is directed at persons who are professionals and is not intended for retail customer use. Intended for recipient only. This message is subject to the terms at: www.barclays.com/emaildisclaimer.

For important disclosures, please see: www.barclays.com/salesandtradingdisclaimer regarding market commentary from Barclays Sales and/or Trading, who are active market participants; and in respect of Barclays Research, including disclosures relating to specific issuers, please see http://publicresearch.barclays.com.

_______________________________________________

_______________________________________________
Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: [property_tree] Issue: erase is corrupting the tree

Boost - Dev mailing list
The minimal code that crashes is:
    boost::property_tree::ptree loadedJSON;
    boost::property_tree::read_json("animalList.json", loadedJSON);
    auto assoc_it = loadedJSON.find("animals");
    auto it = loadedJSON.to_iterator(assoc_it);
    it = it->second.begin();
    it++; // move to second item
    loadedJSON.erase(it);
    boost::property_tree::write_json("shorterList.json", loadedJSON);

Where content of "animalList.json" file is:
{
    "animals" : ["fox","dog","cat"]
}


-----Original Message-----
From: Mert Büyüktuncay [mailto:[hidden email]]
Sent: Tuesday, June 06, 2017 9:22 AM
To: [hidden email]
Cc: Exler, Lukas: IT (PRG)
Subject: RE: [property_tree] Issue: erase is corrupting the tree

This mail originated from outside our organisation

Do you use "it" iterator after the call to erase in that case you shall use it = loadedJSON.erase(it); instead of
it++
If this is not your problem you can share a little more code for us to see in detail what is happening here. May be the code up to the point where "write_json" is being called.

Regards
Mert

-----Original Message-----
From: Boost [mailto:[hidden email]] On Behalf Of Lukas via Boost
Sent: Monday, June 5, 2017 4:08 PM
To: [hidden email]
Cc: [hidden email]
Subject: [boost] [property_tree] Issue: erase is corrupting the tree

I have following JSON structure loaded into boost::property_tree:
{
    "animals" : ["fox","dog","cat"]
}

I'm trying to delete the second item from the array by:
        auto assoc_it = loadedJSON.find("animals");
        auto it = loadedJSON.to_iterator(assoc_it);
        it = it->second.begin();
        it++; // move to second item
        loadedJSON.erase(it);

After this, when I try to use boost::property_tree::write_json to write the JSON back the app crashes. Do I use the property tree in a wrong way, or is there a bug?

Thanks,
Lukas.

_______________________________________________

This message is for information purposes only, it is not a recommendation, advice, offer or solicitation to buy or sell a product or service nor an official confirmation of any transaction. It is directed at persons who are professionals and is not intended for retail customer use. Intended for recipient only. This message is subject to the terms at: www.barclays.com/emaildisclaimer.

For important disclosures, please see: www.barclays.com/salesandtradingdisclaimer regarding market commentary from Barclays Sales and/or Trading, who are active market participants; and in respect of Barclays Research, including disclosures relating to specific issuers, please see http://publicresearch.barclays.com.

_______________________________________________

_______________________________________________
Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
Bu elektronik posta mesajı ve ekleri sadece gönderildiği kişi veya kuruma özeldir ve gizli bilgiler içerebilir. Eğer bu mesajı hataen aldıysanız lütfen bu durumu gönderen kişiye derhal bildiriniz ve mesajı sisteminizden siliniz. Eğer doğru kişiye ulaşmadığını düşünüyorsanız, bu mesajın gizlenmesi, yönlendirilmesi, kopyalanması veya herhangi bir şekilde kullanılması yasaktır. Internet iletişiminde güvenlik ve hatasız gönderim garanti edilemeyeceğinden, mesajın yerine ulaşmaması, geç ulaşması, içeriğinin bozulması ya da mesajın virüs taşıması gibi problemler oluşabilir. Gönderen taraf bu tip sorunlardan sorumlu tutulmaz.
SESTEK Ses ve İletişim Teknolojileri A.Ş.
www.sestek.com<http://www.sestek.com>

This e-mail message and any attachments are confidential and intended solely for the use of the individual or entity to whom they are addressed. If you have received this message in error, please notify the sender immediately and delete it from your system. If you are not the intended recipient you are hereby notified that any dissemination, forwarding, copying or use of any of the information is prohibited. Internet communications cannot be guaranteed to be secure or error-free as information could be intercepted, corrupted, lost, arrive late or contain viruses. The sender therefore does not accept liability for any errors or omissions in the context of this message which arise as a result of Internet transmission SESTEK INC.
www.sestek.com <http://www.sestek.com>
Bu elektronik posta mesajı ve ekleri sadece gönderildiği kişi veya kuruma özeldir ve gizli bilgiler içerebilir. Eğer bu mesajı hataen aldıysanız lütfen bu durumu gönderen kişiye derhal bildiriniz ve mesajı sisteminizden siliniz. Eğer doğru kişiye ulaşmadığını düşünüyorsanız, bu mesajın gizlenmesi, yönlendirilmesi, kopyalanması veya herhangi bir şekilde kullanılması yasaktır. Internet iletişiminde güvenlik ve hatasız gönderim garanti edilemeyeceğinden, mesajın yerine ulaşmaması, geç ulaşması, içeriğinin bozulması ya da mesajın virüs taşıması gibi problemler oluşabilir. Gönderen taraf bu tip sorunlardan sorumlu tutulmaz.
SESTEK Ses ve İletişim Teknolojileri A.Ş.
www.sestek.com<http://www.sestek.com>

This e-mail message and any attachments are confidential and intended solely for the use of the individual or entity to whom they are addressed. If you have received this message in error, please notify the sender immediately and delete it from your system. If you are not the intended recipient you are hereby notified that any dissemination, forwarding, copying or use of any of the information is prohibited. Internet communications cannot be guaranteed to be secure or error-free as information could be intercepted, corrupted, lost, arrive late or contain viruses. The sender therefore does not accept liability for any errors or omissions in the context of this message which arise as a result of Internet transmission SESTEK INC.
www.sestek.com <http://www.sestek.com>

_______________________________________________

This message is for information purposes only, it is not a recommendation, advice, offer or solicitation to buy or sell a product or service nor an official confirmation of any transaction. It is directed at persons who are professionals and is not intended for retail customer use. Intended for recipient only. This message is subject to the terms at: www.barclays.com/emaildisclaimer.

For important disclosures, please see: www.barclays.com/salesandtradingdisclaimer regarding market commentary from Barclays Sales and/or Trading, who are active market participants; and in respect of Barclays Research, including disclosures relating to specific issuers, please see http://publicresearch.barclays.com.

_______________________________________________

_______________________________________________
Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: [property_tree] Issue: erase is corrupting the tree

Boost - Dev mailing list
On 06-06-17 10:32, Lukas via Boost wrote:
>     boost::property_tree::ptree loadedJSON;
>     boost::property_tree::read_json("animalList.json", loadedJSON);
>     auto assoc_it = loadedJSON.find("animals");
>     auto it = loadedJSON.to_iterator(assoc_it);
>     it = it->second.begin();
>     it++; // move to second item
>     loadedJSON.erase(it);
>     boost::property_tree::write_json("shorterList.json", loadedJSON);
You're using erase on the wrong container.

     auto it = loadedJSON.to_iterator(assoc_it);

Here, it points into the root ptree.

     it = it->second.begin();

Now, it points into the nested ptree ("animals")

    loadedJSON.erase(it);

That's undefined behaviour. Fix:

    #include <boost/property_tree/ptree.hpp>
    #include <boost/property_tree/json_parser.hpp>
    #include <iostream>

    using boost::property_tree::ptree;

    int main() {
        ptree loadedJSON;
        read_json("animalList.json", loadedJSON);

        //auto& animals = *loadedJSON.find("animals");
        auto& animals = loadedJSON.get_child("animals");

        auto it = animals.begin();
        it++; // move to second item
        animals.erase(it);

        write_json("shorterList.json", loadedJSON);
    }




_______________________________________________
Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
Loading...