The International Simutrans Forum

Simutrans Extended => Simutrans-Extended bug reports => Simutrans-Extended development => Simutrans-Extended closed bug reports => Topic started by: inkelyad on September 18, 2010, 11:15:42 AM

Title: Infinite loop in makeobj.
Post by: inkelyad on September 18, 2010, 11:15:42 AM
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.
Title: Re: Infinite loop in makeobj.
Post by: jamespetts on September 18, 2010, 09:18:48 PM
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);
Title: Re: Infinite loop in makeobj.
Post by: inkelyad on September 18, 2010, 09:27:07 PM
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?
Title: Re: Infinite loop in makeobj.
Post by: jamespetts on September 18, 2010, 09:34:20 PM
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?
Title: Re: Infinite loop in makeobj.
Post by: inkelyad on September 18, 2010, 09:35:41 PM
Yes. No problem so far.
Title: Re: Infinite loop in makeobj.
Post by: jamespetts on September 18, 2010, 10:15:15 PM
Lovely - thank you very much!
Title: Re: Infinite loop in makeobj.
Post by: jamespetts on December 19, 2010, 12:14:51 AM
This fix is incorporated into 9.0.