Skip to content

Commit

Permalink
Fix instantiation of QByteArray and counting of bytes. [closes #249]
Browse files Browse the repository at this point in the history
  • Loading branch information
lohedges committed Oct 28, 2024
1 parent 4fcc147 commit 926f082
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 23 deletions.
46 changes: 23 additions & 23 deletions corelib/src/libs/SireMol/trajectory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1380,9 +1380,9 @@ QByteArray Frame::toByteArray() const
ds << spc << t.to(picosecond) << props;

nbytes += sizeof(quint32);
nbytes += extra.count();
nbytes += extra.size();

QByteArray data("\0", nbytes);
QByteArray data(nbytes, '\0');

auto data_ptr = data.data();

Expand Down Expand Up @@ -1425,7 +1425,7 @@ QByteArray Frame::toByteArray() const
data_ptr += val * sizeof(Force3D);
}

val = extra.count();
val = extra.size();
std::memcpy(data_ptr, &val, sizeof(quint32));
data_ptr += sizeof(quint32);

Expand All @@ -1435,12 +1435,12 @@ QByteArray Frame::toByteArray() const
data_ptr += val;
}

if (data_ptr - data.constData() != data.count())
if (data_ptr - data.constData() != data.size())
{
throw SireError::program_bug(QObject::tr(
"Memory corruption? %1 versus %2")
.arg(data_ptr - data.constData())
.arg(data.count()),
.arg(data.size()),
CODELOC);
}

Expand All @@ -1449,9 +1449,9 @@ QByteArray Frame::toByteArray() const

Frame Frame::fromByteArray(const QByteArray &data)
{
if (data.count() < 4)
if (data.size() < 4)
{
throw SireError::incompatible_error(QObject::tr("The data is too short to be a frame! %1").arg(data.count()),
throw SireError::incompatible_error(QObject::tr("The data is too short to be a frame! %1").arg(data.size()),
CODELOC);
}

Expand All @@ -1474,9 +1474,9 @@ Frame Frame::fromByteArray(const QByteArray &data)
throw SireStream::version_error(val, "1", r_frame, CODELOC);
}

if (data_ptr + sizeof(quint32) > data.constData() + data.count())
if (data_ptr + sizeof(quint32) > data.constData() + data.size())
{
throw SireError::incompatible_error(QObject::tr("The data is too short to be a frame! %1").arg(data.count()),
throw SireError::incompatible_error(QObject::tr("The data is too short to be a frame! %1").arg(data.size()),
CODELOC);
}

Expand All @@ -1487,9 +1487,9 @@ Frame Frame::fromByteArray(const QByteArray &data)

if (val != 0)
{
if (data_ptr + val * sizeof(Vector) > data.constData() + data.count())
if (data_ptr + val * sizeof(Vector) > data.constData() + data.size())
{
throw SireError::incompatible_error(QObject::tr("The data is too short to be a frame! %1").arg(data.count()),
throw SireError::incompatible_error(QObject::tr("The data is too short to be a frame! %1").arg(data.size()),
CODELOC);
}

Expand All @@ -1498,9 +1498,9 @@ Frame Frame::fromByteArray(const QByteArray &data)
data_ptr += val * sizeof(Vector);
}

if (data_ptr + sizeof(quint32) > data.constData() + data.count())
if (data_ptr + sizeof(quint32) > data.constData() + data.size())
{
throw SireError::incompatible_error(QObject::tr("The data is too short to be a frame! %1").arg(data.count()),
throw SireError::incompatible_error(QObject::tr("The data is too short to be a frame! %1").arg(data.size()),
CODELOC);
}

Expand All @@ -1511,9 +1511,9 @@ Frame Frame::fromByteArray(const QByteArray &data)

if (val != 0)
{
if (data_ptr + val * sizeof(Velocity3D) > data.constData() + data.count())
if (data_ptr + val * sizeof(Velocity3D) > data.constData() + data.size())
{
throw SireError::incompatible_error(QObject::tr("The data is too short to be a frame! %1").arg(data.count()),
throw SireError::incompatible_error(QObject::tr("The data is too short to be a frame! %1").arg(data.size()),
CODELOC);
}

Expand All @@ -1522,9 +1522,9 @@ Frame Frame::fromByteArray(const QByteArray &data)
data_ptr += val * sizeof(Velocity3D);
}

if (data_ptr + sizeof(quint32) > data.constData() + data.count())
if (data_ptr + sizeof(quint32) > data.constData() + data.size())
{
throw SireError::incompatible_error(QObject::tr("The data is too short to be a frame! %1").arg(data.count()),
throw SireError::incompatible_error(QObject::tr("The data is too short to be a frame! %1").arg(data.size()),
CODELOC);
}

Expand All @@ -1535,9 +1535,9 @@ Frame Frame::fromByteArray(const QByteArray &data)

if (val != 0)
{
if (data_ptr + val * sizeof(Force3D) > data.constData() + data.count())
if (data_ptr + val * sizeof(Force3D) > data.constData() + data.size())
{
throw SireError::incompatible_error(QObject::tr("The data is too short to be a frame! %1").arg(data.count()),
throw SireError::incompatible_error(QObject::tr("The data is too short to be a frame! %1").arg(data.size()),
CODELOC);
}

Expand All @@ -1546,9 +1546,9 @@ Frame Frame::fromByteArray(const QByteArray &data)
data_ptr += val * sizeof(Force3D);
}

if (data_ptr + sizeof(quint32) > data.constData() + data.count())
if (data_ptr + sizeof(quint32) > data.constData() + data.size())
{
throw SireError::incompatible_error(QObject::tr("The data is too short to be a frame! %1").arg(data.count()),
throw SireError::incompatible_error(QObject::tr("The data is too short to be a frame! %1").arg(data.size()),
CODELOC);
}

Expand All @@ -1561,9 +1561,9 @@ Frame Frame::fromByteArray(const QByteArray &data)

if (val != 0)
{
if (data_ptr + val > data.constData() + data.count())
if (data_ptr + val > data.constData() + data.size())
{
throw SireError::incompatible_error(QObject::tr("The data is too short to be a frame! %1").arg(data.count()),
throw SireError::incompatible_error(QObject::tr("The data is too short to be a frame! %1").arg(data.size()),
CODELOC);
}

Expand Down
6 changes: 6 additions & 0 deletions doc/source/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ Development was migrated into the
`OpenBioSim <https://github.com/openbiosim>`__
organisation on `GitHub <https://github.com/openbiosim/sire>`__.

`2024.4.0 <https://github.com/openbiosim/sire/compare/2024.3.0...2024.4.0>`__ - December 2024
---------------------------------------------------------------------------------------------

* Please add an item to this CHANGELOG for any new features or bug fixes when creating a PR.
* Fixed instantiaton of ``QByteArray`` in ``Sire::Mol::Frame::toByteArray`` and count bytes with ``QByteArray::size``.

`2024.3.0 <https://github.com/openbiosim/sire/compare/2024.2.0...2024.3.0>`__ - October 2024
--------------------------------------------------------------------------------------------

Expand Down

0 comments on commit 926f082

Please sign in to comment.