Changeset 32 for cpp/f8-to-f1


Ignore:
Timestamp:
10/30/09 20:23:42 (14 years ago)
Author:
Maciej Komosinski
Message:

safer parsing and freeing memory

File:
1 edited

Legend:

Unmodified
Added
Removed
  • cpp/f8-to-f1/conv_f8tof1.cpp

    r26 r32  
    556556        const char* src = in;
    557557       
    558         if (in.len() < 1 && !this->checkSyntax(src)) {
     558        if (in.len() < 1 || !this->checkSyntax(src)) {
    559559                return SString();
    560560        }
     
    565565        }
    566566        if (lsystem->firstProductionName.empty()) {
     567                delete lsystem;
    567568                return SString();
    568569        }
     
    598599        //set parameters for start production
    599600        Production *firstProduction = lsystem->productions[lsystem->firstProductionName];
     601        if (firstProduction==NULL) {
     602                delete lsystem;
     603                return SString(""); //should never happen because of syntax validation
     604        }
     605
    600606        vector<double> params;
    601607        params.assign(lsystem->startParams.size(), 0.0);
    602608        //cout << "startParams->size: " << lsystem->startParams.size() << endl;
    603         for (map<string, double>::iterator iter = lsystem->startParams.begin();
    604                  iter != lsystem->startParams.end(); iter++) {
     609        for (map<string, double>::iterator iter = lsystem->startParams.begin();
     610                 iter != lsystem->startParams.end(); iter++)
     611        {
    605612                int position = firstProduction->parameters.getParameterPosition(stringToSString(iter->first));
    606613                //cout << "position of " << iter->first << ": " << position << endl;
    607                 params.insert(params.begin() + (position - 1), iter->second);
    608                 //params[position - 1] = iter->second;
     614                //params.insert(params.begin() + (position - 1), iter->second); //no need because the vector has required length (assign above)
     615                if (position>params.size()) {
     616                        delete lsystem;
     617                        return SString("");
     618                }
     619                params[position - 1] = iter->second;
    609620        }
    610621       
     
    651662                        dst += a->getF1Genotype(p);
    652663                        if (dst.len() > maxF1Length) {
     664                                delete lsystem;
    653665                                return SString(); //genotype becomes too long so we abort conversion
    654666                        }
     
    670682#endif
    671683        Lsystem *lsys = new Lsystem();
    672        
     684
    673685        //read production names and create objects for them
    674686        vector<SString> names = this->readProductionNames(in);
Note: See TracChangeset for help on using the changeset viewer.