qtsigabrtqvectorqdatastream

Reading from QDataStream to QVector: application crashes with SIGABRT signal


A crash with this message:

The inferior stopped because it received a signal from the operating system.

Signal name : SIGABRT

Signal meaning : Aborted

happens at this line of code:

// data is QByteArray ...
QByteArray pos0  = data.mid( index, length );
/*
 * Get float from byte-array
 */
QDataStream streamPos0(pos0);
QVector<float> floatPos0;
streamPos0 >> floatPos0; // Crash happens exactly at this line

I couldn't figure out why the crash happens. I wonder if anybody can give me a hint ...


UPDATE

I stepped through the code and captured the local values when the code is exactly at the crash line:

data    "fý¾¾PY\001À\216\224\033ÁµÏ4½\020\233µ½±½~¿f?ÿ¾@Q\001Àî}\033ÁµÏ4½\020\233µ½±½~¿f?ÿ¾ì\007`¿\010 \035ÁµÏ4½\020\233µ½±½~¿z\n~¾\017?`¿´F\035ÁØÙ\000½Ó±µ½\003Ý~¿z\n~¾"... (846288)   QByteArray
index   0   int
length  12  quint64
pos0    "fý¾¾PY\001À\216\224\033Á"  QByteArray
streamPos0  @0x7fffffff7bb0 QDataStream
        byteorder   QDataStream::BigEndian (0x0000) QDataStream::ByteOrder
        d   (null)  QScopedPointer<QDataStreamPrivate>
        dev @0x12f0d60  QIODevice
        noswap  false   bool
        owndev  true    bool
        q_status    QDataStream::Ok (0x0000)    QDataStream::Status
        ver 17  int
floatPos0   <0 items>

The exact location of crash is at this line in the file qdatastream.h:

template <typename Container>
QDataStream &readArrayBasedContainer(QDataStream &s, Container &c)
{
    StreamStateSaver stateSaver(&s);

    c.clear();
    quint32 n;
    s >> n;
    c.reserve(n); // crash happens exactly here
    for (quint32 i = 0; i < n; ++i) {
        typename Container::value_type t;
        s >> t;
        if (s.status() != QDataStream::Ok) {
            c.clear();
            break;
        }
        c.append(t);
    }

    return s;
}

I tried assigning a size to my QVector<float> floatPos0 by .reserve() and .resize() methods but it didn't help.


I'm using Qt 5.9.4


Solution

  • I avoided the SIGABRT crash by changing the procedure by which the byte-array is converted to float numbers. I replaced the QDataStream method which was less verbose, but was throwing SIGABRT signal, with the following method. Now the code runs fine:

    // Extracting chunks of float out of byte-array
    QByteArray pos0x = data.mid( index                      , sizeof(float) );
    QByteArray pos0y = data.mid( index + 1 * sizeof(float)  , sizeof(float) );
    QByteArray pos0z = data.mid( index + 2 * sizeof(float)  , sizeof(float) );
    
    // Converting to float
    float floatPos0x;
    if ( pos0x.size() >= sizeof(floatPos0x) ) {
        floatPos0x = *reinterpret_cast<const float *>( pos0x.data() );
    }
    float floatPos0y;
    if ( pos0y.size() >= sizeof(floatPos0y) ) {
        floatPos0y = *reinterpret_cast<const float *>( pos0y.data() );
    }
    float floatPos0z;
    if ( pos0z.size() >= sizeof(floatPos0z) ) {
        floatPos0z = *reinterpret_cast<const float *>( pos0z.data() );
    }
    

    This post is helpful.