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)
Code: [Select]
        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?

Code: [Select]
// 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
Code: [Select]
        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!