icnhoukdsiih / testlib

Automatically exported from code.google.com/p/testlib
0 stars 0 forks source link

Unitialized "file" field for InStream constructed from string #34

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
testlib.h, lines 1804..1821:
...
InStream::InStream()
{
    file = NULL;
    name = "";
    mode = _input;
    strict = false;
    stdfile = false;
    wordReserveSize = 4;
}

InStream::InStream(const InStream& baseStream, std::string content)
{
    reader = new StringInputStreamReader(content);
    opened = true;
    strict = baseStream.strict;
    mode = baseStream.mode;
    name = "based on " + baseStream.name;
}
...
testlib.h, lines 2579..2598
...
double InStream::readStrictReal(double minv, double maxv,
        int minAfterPointDigitCount, int maxAfterPointDigitCount,
        const std::string& variableName)
{
    if (!strict && seekEof())
        quit(_unexpected_eof, "Unexpected end of file - strict double expected");

    double result = stringToStrictDouble(*this, readWord().c_str(),
            minAfterPointDigitCount, maxAfterPointDigitCount);

    if (result < minv || result > maxv)
    {
        if (variableName.empty())
            quit(_wa, ("Strict double " + vtos(result) + " violates the range [" + vtos(minv) + ", " + vtos(maxv) + "]").c_str());
        else
            quit(_wa, ("Strict double parameter [name=" + variableName + "] equals to " + vtos(result) + ", violates the range [" + vtos(minv) + ", " + vtos(maxv) + "]").c_str());
    }

    return result;
}
...

"file" field is not initialized in constructor for InStreams constructed from 
string. Every call of readAnything (readStrictDouble for example, lile in 
sample checker in the attachment) will cause undefined behavior because of "if 
(!strict && seekEof())" at the beginning of each such method. 

Sample program that runs ok without optimization and fails with -O2 
optimization settings is in attachment.

Technical information:

Testlib 0.9.3-SNAPSHOT

g++ (Ubuntu/Linaro 4.7.3-1ubuntu1) 4.7.3
Copyright (C) 2012 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Original issue reported on code.google.com by A.Ma...@gmail.com on 28 Nov 2013 at 6:23

Attachments:

GoogleCodeExporter commented 8 years ago
I've forgotten to mention that in "seekEof()" file is explicitly used ("if 
(NULL == file)"), and at this point happens UB.

Original comment by A.Ma...@gmail.com on 29 Nov 2013 at 3:33

GoogleCodeExporter commented 8 years ago
After revision 101 validation is broken under Linux. Every run produces 
"Expected EOF" outcome.

Original comment by A.Ma...@gmail.com on 5 Jan 2014 at 6:24