Thursday, March 09, 2023

CONFIGFILE

I've spent all day programming. There is an annoying crash bug in Radioactive that affects Andrew's PC. It's probably a tiny memory infringement somewhere, but I am at a loss where. My idea today is that the text file class I use, CONFIGFILE, is at fault because that code is very old and complex and full of possible ins and outs and possibilities. I use it in every program, from Flatspace to Prometheus, so one would imagine it is stable... but it occasionally flags up (so called) errors in the Code Analysis.

// Create new text file as a buffer (text+possibleendofprevlinezero+value+finalendoflinezero)
    newtext=new (std::nothrow) char[textsize+strlen(value)+2];
    if (newtext)
    {
        if (textsize>0)
        {
// Copy file into newtext and append zero if none at the end
            for (i=0; i<textsize; newtext[i]=text[i++]);
            if (newtext[i-1])
                newtext[i++]=0;
        }
        else
        {
            newtext[0]=0;
            i=1;
        }
// Copy entry to newtext
        for (j=0; j<strlen(value); newtext[i++]=value[j++]);
        newtext[i]=0;
        i++;
    }

The bold line there is flagged with a possible buffer overrun when I'm sure it can't! The variable 'i', at that point must be textsize+1+strlen(value) at most. After that line, i should = textsize+2+strlen(value), which, true beyond the maximum, but I don't use 'i' after that; it's there for neatness, to show that the value should be incremented. Yet, the compiler flags it as a memory infrinement.

Occasionally today I've had bizarre happenings, instant crashes for simple loads and saves that should be fine and had worked earlier in the day. They resolved themselves without any major changes. The whole situation started from a neat and ordered re-ordering of my code, going through each routine in turn, checking, neatening, and, just when it was all working, everything started to go wrong for no reason!

Sigh. Well, computers teach us all patience. I hve no time for painting today and am eagerly waiting for Andrew's report.