diff --git a/src/crate-reader.cc b/src/crate-reader.cc index e059d90d..024e86b2 100644 --- a/src/crate-reader.cc +++ b/src/crate-reader.cc @@ -671,6 +671,11 @@ bool CrateReader::ReadDoubleArray(bool is_compressed, std::vector *d) { length = size_t(n); } + if (length == 0) { + d->clear(); + return true; + } + if (length > _config.maxArrayElements) { PUSH_ERROR_AND_RETURN_TAG(kTag, "Too many array elements."); } @@ -760,6 +765,40 @@ bool CrateReader::ReadDoubleArray(bool is_compressed, std::vector *d) { } } +bool CrateReader::ReadDoubleVector(std::vector *d) { + size_t length; + + uint64_t n; + if (!_sr->read8(&n)) { + _err += "Failed to read the number of array elements.\n"; + return false; + } + + length = size_t(n); + + if (length == 0) { + d->clear(); + return true; + } + + if (length > _config.maxArrayElements) { + PUSH_ERROR_AND_RETURN_TAG(kTag, "Too many array elements."); + } + + CHECK_MEMORY_USAGE(length * sizeof(double)); + + d->resize(length); + + // TODO(syoyo): Zero-copy + if (!_sr->read(sizeof(double) * length, sizeof(double) * length, + reinterpret_cast(d->data()))) { + _err += "Failed to read double vector data.\n"; + return false; + } + + return true; +} + bool CrateReader::ReadTimeSamples(value::TimeSamples *d) { // Layout @@ -4113,7 +4152,7 @@ bool CrateReader::UnpackValueRep(const crate::ValueRep &rep, } case crate::CrateDataTypeId::CRATE_DATA_TYPE_DOUBLE_VECTOR: { std::vector v; - if (!ReadDoubleArray(rep.IsCompressed(), &v)) { + if (!ReadDoubleVector(&v)) { _err += "Failed to read DoubleVector value\n"; return false; } diff --git a/src/crate-reader.hh b/src/crate-reader.hh index 50faf067..22679fbb 100644 --- a/src/crate-reader.hh +++ b/src/crate-reader.hh @@ -340,6 +340,8 @@ class CrateReader { bool ReadFloatArray(bool is_compressed, std::vector *d); bool ReadDoubleArray(bool is_compressed, std::vector *d); + bool ReadDoubleVector(std::vector *d); + // template // struct IsIntType { // static const bool value =