jeudi 26 janvier 2017

C++11 std::regex crash on assigning pattern

OK, I'm brand new to C++, but have experience in PHP and some in JS. I'm working on an HTML form processor, using the standard regex library. I'm getting an intermittent program crash when I assign a pattern to a basic_regex object, whether it's at instantiation or with the assign() method. I am working in Orwell Dev C++, using g++ 4.9.2 with Windows 8.1 on a Dell Optiplex 7010. I've reduced the program to the bare minimum necessary to produce the crash, though I've left in all the #includes I'm using. When I compile this and run it, it crashes about half the time, and runs OK the rest. If I remove so much as the second bracket in the last Form.setV(), it no longer crashes. The pattern is irrelevant; it crashes with any pattern.

#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string.h>
#include <regex>
#include <sys/stat.h>

using namespace std;

struct formField {
    char *Name;
    char *Val;
};

// Holds the contents of the HTML form
class HTMLForm {
    public:
        formField *Fields;
        int       numFields;

        void dim(int size) {
            this->Fields = new formField[size];
            this->numFields = size;
        }

        void setV(const char *var, const char *val) {
            static int fdx = 0;

            if (fdx < this->numFields) {
                this->Fields[fdx].Name = new char[strlen(var)];
                this->Fields[fdx].Val  = new char[strlen(val)];
                strcpy(this->Fields[fdx].Name, var);
                strcpy(this->Fields[fdx].Val,  val);
                fdx++;
            }
        }

        char *getV(const char *var) {
            char *ptr = NULL;
            for (int i=0; i<this->numFields; i++) {
                if (strcmp(var, this->Fields[i].Name) == 0) {
                    ptr = Fields[i].Val;
                    break;
                }
            }
            return ptr;
        }

        char *getV(int fIdx) {
            char *ptr = NULL;
            if (fIdx >=0 && fIdx < this->numFields) ptr = this->Fields[fIdx].Val;
            return ptr;
        }
};

int main() {
    HTMLForm Form;

    Form.dim(13);

    Form.setV("FORM_NAME", "TestForm");
    Form.setV("TestBug", "1");
    Form.setV("JoeMama", "Wassup");
    Form.setV("MAX_FILE_SIZE", "500000");
    Form.setV("FORM_FILE01", "");
    Form.setV("FORM_FILE02", "");
    Form.setV("Text01", "Nutn' Hunney");
    Form.setV("Text02", "Wassuuuuuuup");
    Form.setV("Text03", "Hello Whirled");
    Form.setV("Qty[1][abc][xyz]", "");
    Form.setV("Item[1][]", "Now is the time");
    Form.setV("Cost[1][]", "");
    Form.setV("Qty[2][]", "");

    regex  varPat("<<([A-Za-z€-ÿ_][A-Za-z€-ÿ0-9_]*)>>");
    cout << "set\n";
}

Aucun commentaire:

Enregistrer un commentaire