News:

Simutrans Tools
Know our tools that can help you to create add-ons, install and customize Simutrans.

Infinite loop in makeobj.

Started by inkelyad, September 18, 2010, 11:15:42 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

inkelyad

Something wrong with folowing code (besch/writer/vehicle_writer.cc)

        uint8 upgrades = 0;
        do {
                char buf[40];
                sprintf(buf, "upgrade[%d]", upgrades);
                str = obj.get(buf);
                found = str.size() > 0;
                if (found)
                {
                        if (upgrades == 0 && !STRICMP(str.c_str(), "none"))
                        {
                                if (!STRICMP(str.c_str(), "none"))
                                {
                                        str = "";
                                }
                                xref_writer_t::instance()->write_obj(fp, node, obj_vehicle, str.c_str(), false);
                                upgrades++;
                        }
                }
        } while (found);

!STRICMP(str.c_str(), "none") == false for first iteration so upgrades never incremented and we have infinite loop.

jamespetts

Thank you for pointing this out. Would this be an appropriate fix, do you think?


// Upgrades: these are the vehicle types to which this vehicle
// can be upgraded. "None" means that it cannot be upgraded.
// @author: jamespetts
uint8 upgrades = 0;
do {
char buf[40];
sprintf(buf, "upgrade[%d]", upgrades);
str = obj.get(buf);
found = str.size() > 0;
if (found)
{
if (upgrades == 0 && !STRICMP(str.c_str(), "none"))
{
if (!STRICMP(str.c_str(), "none"))
{
str = "";
}
xref_writer_t::instance()->write_obj(fp, node, obj_vehicle, str.c_str(), false);
}
upgrades++;
}
} while (found);
Download Simutrans-Extended.

Want to help with development? See here for things to do for coding, and here for information on how to make graphics/objects.

Follow Simutrans-Extended on Facebook.

inkelyad

It is merge error, i think.
I don't really understand why we need check for upgrades == 0 and why !STRICMP(str.c_str(), "none") checked twice.

just

        uint8 upgrades = 0;
        do {
                char buf[40];
                sprintf(buf, "upgrade[%d]", upgrades);
                str = obj.get(buf);
                found = str.size() > 0;
                if (found)
                {
                        if (!STRICMP(str.c_str(), "none"))
                        {
                                str = "";
                        }
                        else
                        {   
                                xref_writer_t::instance()->write_obj(fp, node, obj_vehicle, str.c_str(), false);
                                upgrades++;
                        }
                }
        } while (found);

will be fine?

jamespetts

That looks very different - I must confess, I can't remember precisely how the code was intended now (and, yes, I think that you're right that it was a merge error). Have you tested your version?
Download Simutrans-Extended.

Want to help with development? See here for things to do for coding, and here for information on how to make graphics/objects.

Follow Simutrans-Extended on Facebook.

inkelyad


jamespetts

Download Simutrans-Extended.

Want to help with development? See here for things to do for coding, and here for information on how to make graphics/objects.

Follow Simutrans-Extended on Facebook.

jamespetts

This fix is incorporated into 9.0.
Download Simutrans-Extended.

Want to help with development? See here for things to do for coding, and here for information on how to make graphics/objects.

Follow Simutrans-Extended on Facebook.