“偶尔”细分错误

1 c++ mingw endianness

问题是关于GRIB解析器(链接到GRIB文件https://github.com/Gifciak/GRIB),当我执行我的代码时(尽管代码块或通过控制台在linux上g++ main.cpp -pedantic),我遇到了错误,分段错误,但它没有并非总是会发生。

例如,当我编译10次时,将有8次错误,而2次则将一切正常,这将为我提供控制台输出和信息。

正如我研究过的那样,问题在于std::copy,因为它可能正在尝试一个不再存在的复制迭代器。

有人可以解释为什么会发生吗?为什么它不总是崩溃或成功?

#include <iostream>
#include <vector>
#include <fstream>
#include <iterator>
#include <algorithm>
using ByteVec = std::vector<uint8_t>;


template<typename T, size_t size = sizeof(T)>
auto getReverseEndianValue(const auto & iter) {
    union {
        T result;
        char tmp[size];
    } buffer;

    auto reverseIter = std::make_reverse_iterator(std::next(iter, size));
    std::copy(reverseIter, std::next(reverseIter, size), buffer.tmp);

    return buffer.result;
}

enum Edition {
    Edition_Unknown = -1,
    Edition_GRIB1 = 1,
};

namespace section {
    class IS {
    public:
        uint32_t magicFlag;
        uint32_t size;
        Edition edition;

        static IS read(const auto & iter) {
            IS result;
            result.magicFlag = getReverseEndianValue<uint32_t>(iter);
            result.size = getReverseEndianValue<uint32_t, 3>(iter + 4);
            result.edition = (*(iter + 7) == 1 ? Edition_GRIB1 : Edition_Unknown);
            return result;
        }
    };

    class PDS {
    public:
        uint32_t size;
        uint8_t tableVersion;
        uint8_t indentificatorOfCenter;
        uint8_t numProcessID;
        uint8_t gridIndentification;
        uint8_t flagForGDSorBMS;
        uint8_t indParamAndUnit;
        uint8_t indTypeOfLevelOrLayer;
        uint16_t levelOrLayer;
        uint8_t year;
        uint8_t month;
        uint8_t day;
        uint8_t hour;
        uint8_t minute;
        uint8_t forecastTimeUnit;
        uint8_t p1;
        uint8_t p2;
        uint8_t indTimeRange;
        uint16_t averageOrAccumulate;
        uint8_t missing;
        uint8_t century;
        uint8_t subcenterId;
        uint16_t decimalScale;
        ByteVec data;

        static PDS read(const auto& iter) {
            PDS result;
            result.size = getReverseEndianValue<uint32_t, 3>(iter);
            result.tableVersion = getReverseEndianValue<uint8_t>(iter + 3);
            result.indentificatorOfCenter = getReverseEndianValue<uint8_t>(iter + 4);
            result.numProcessID = getReverseEndianValue<uint8_t>(iter + 5);
            result.gridIndentification = getReverseEndianValue<uint8_t>(iter + 6);
            result.flagForGDSorBMS = getReverseEndianValue<uint8_t>(iter + 7);
            result.indParamAndUnit = getReverseEndianValue<uint8_t>(iter + 8);
            result.indTypeOfLevelOrLayer = getReverseEndianValue<uint8_t>(iter + 9);
            result.levelOrLayer = getReverseEndianValue<uint16_t>(iter + 10);
            result.year = getReverseEndianValue<uint8_t>(iter + 12);
            result.month = getReverseEndianValue<uint8_t>(iter + 13);
            result.day = getReverseEndianValue<uint8_t>(iter + 14);
            result.hour = getReverseEndianValue<uint8_t>(iter + 15);
            result.minute = getReverseEndianValue<uint8_t>(iter + 16);
            result.forecastTimeUnit = getReverseEndianValue<uint8_t>(iter + 17);
            result.p1 = getReverseEndianValue<uint8_t>(iter + 18);
            result.p2 = getReverseEndianValue<uint8_t>(iter + 19);
            result.indTimeRange = getReverseEndianValue<uint8_t>(iter + 20);
            result.averageOrAccumulate = getReverseEndianValue<uint16_t>(iter + 21);
            result.missing = getReverseEndianValue<uint8_t>(iter + 23);
            result.century = getReverseEndianValue<uint8_t>(iter + 24);
            result.subcenterId = getReverseEndianValue<uint8_t>(iter + 25);
            result.decimalScale = getReverseEndianValue<uint16_t>(iter + 26);
            return result;
        }
    };
}

class GribData {
private:
    section::IS secIS;
    section::PDS secPDS;

public:
    void print() {
        std::cout
            << "### Section IS ###\n"
            << "magicFlag: " << +secIS.magicFlag << "\n"
            << "size: " << +secIS.size << "\n"
            << "edition: " << +secIS.edition << "\n"

            << "\n### Section PDS ###\n"
            << "size: " << +secPDS.size << "\n"
            << "tableVersion: " << +secPDS.tableVersion << "\n"
            << "indentificatorOfCenter: " << +secPDS.indentificatorOfCenter << "\n"
            << "numProcessID: " << +secPDS.numProcessID << "\n"
            << "gridIndentification: " << +secPDS.gridIndentification << "\n"
            << "flagForGDSorBMS: " << +secPDS.flagForGDSorBMS << "\n"
            << "indParamAndUnit: " << +secPDS.indParamAndUnit << "\n"
            << "indTypeOfLevelOrLayer: " << +secPDS.indTypeOfLevelOrLayer << "\n"
            << "levelOrLayer: " << +secPDS.levelOrLayer << "\n"
            << "year: " << +secPDS.year << "\n"
            << "month: " << +secPDS.month << "\n"
            << "day: " << +secPDS.day << "\n"
            << "hour: " << +secPDS.hour << "\n"
            << "minute: " << +secPDS.minute << "\n"
            << "forecastTimeUnit: " << +secPDS.forecastTimeUnit << "\n"
            << "p1: " << +secPDS.p1 << "\n"
            << "p2: " << +secPDS.p2 << "\n"
            << "indTimeRange: " << +secPDS.indTimeRange << "\n"
            << "averageOrAccumulate: " << +secPDS.averageOrAccumulate << "\n"
            << "missing: " << +secPDS.missing << "\n"
            << "century: " << +secPDS.century << "\n"
            << "subcenterId: " << +secPDS.subcenterId << "\n"
            << "decimalScale: " << +secPDS.decimalScale << "\n";

    }

    static GribData loadData(const ByteVec& rawdata) {
        GribData result;

        constexpr char MAGIC_START[4] = { 'G', 'R', 'I', 'B' };
        constexpr char MAGIC_END[4] = { '7', '7', '7', '7' };

        auto start = std::search(rawdata.cbegin(),
            rawdata.cend(),
            std::begin(MAGIC_START),
            std::end(MAGIC_START));

        auto end = std::search(rawdata.cbegin(),
            rawdata.cend(),
            std::begin(MAGIC_END),
            std::end(MAGIC_END));

        ByteVec data(start, end + sizeof(MAGIC_END));

        result.secIS = section::IS::read(data.cbegin());
        result.secPDS = section::PDS::read(data.cbegin() + 8);

        auto size = getReverseEndianValue<uint32_t, 3>(data.cbegin() + 4);

        auto sec1 = getReverseEndianValue<uint32_t, 3>(data.cbegin() + 8);
        auto sec2 = getReverseEndianValue<uint32_t, 3>(data.cbegin() + 8 + sec1);
        auto sec3 = getReverseEndianValue<uint32_t, 3>(data.cbegin() + 8 + sec1 + sec2);

        std::cout
            << "size: " << size << "\n"
            << "sec0: " << 8 << "\n"
            << "sec1: " << sec1 << "\n"
            << "sec2: " << sec2 << "\n"
            << "sec3: " << sec3 << "\n"
            << "end flag: " << sizeof(MAGIC_END) << "\n"
            << "sum: " << 8 + sec1 + sec2 + sec3 + sizeof(MAGIC_END) << "\n\n";

        return result;
    }

    static GribData loadDataFromFile(const std::string& path) {
        std::ifstream file(path, std::ios::binary);

        ByteVec data;
        std::copy(std::istreambuf_iterator<char>(file),
            {},
            std::back_inserter(data));

        return loadData(data);
    }
};


int main() {
    auto grib = GribData::loadDataFromFile("message_2_G1.grib");
    grib.print();
}
Run Code Online (Sandbox Code Playgroud)

这是预期的结果,因为我已经从控制台复制了它

大小:4538
sec0:8
sec1:28
sec2:178
sec3:4320
结束标记:4
总和:4538

###部分为###
MagicFlag:1196575042
大小:1191186874
版本:1

###部分PDS ###
大小:28
tableVersion:2
indentificatorOfCenter:7
numProcessID:81
gridIndentification:37
flagForGDSorBMS:128
indParamAndUnit:33
indTypeOfLevelOrLayer:100
等级或层数:850
年:15
月:3
一天:10
小时:0
分钟:0
ForecastTimeUnit:1
p1:0
p2:0
indTimeRange:10
averageOrAccumulate:0
缺少:0
世纪:21
subcenterId:0
小数位数:1

Jon*_*ely 5

首先,使用它不是很有用,g++ main.cpp -pedantic因为您尚未启用任何警告。添加-Wall -Wextra到您的编译器标志,也-g可以对其进行调试。

编译为会-fsanitize=undefined显示由于在需要有效指针的情况下使用空指针而导致的运行时错误:

/usr/include/c++/8/bits/stl_algobase.h:368:23: runtime error: null pointer passed as argument 2, which is declared to never be null
Segmentation fault (core dumped)
Run Code Online (Sandbox Code Playgroud)

这意味着您的程序存在错误。

编译-D_GLIBCXX_DEBUG将向添加更多检查,std::vector并告诉您问题所在:

/usr/include/c++/8/debug/safe_iterator.h:374:
Error: attempt to advance a past-the-end iterator 4 steps, which falls 
outside its valid range.

Objects involved in the operation:
    iterator @ 0x0x7fffb09ceb90 {
      type = __gnu_debug::_Safe_iterator<__gnu_cxx::__normal_iterator<unsigned char const*, std::__cxx1998::vector<unsigned char, std::allocator<unsigned char> > >, std::__debug::vector<unsigned char, std::allocator<unsigned char> > > (constant iterator);
      state = past-the-end;
      references sequence with type 'std::__debug::vector<unsigned char, std::allocator<unsigned char> >' @ 0x0x7fffb09cf050
    }
Aborted (core dumped)
Run Code Online (Sandbox Code Playgroud)

您应该在调试器下运行该程序,以查看此无效的迭代器增量发生在何处。在GDB中运行该程序,然后使用其up命令向上移动堆栈,显示错误来自此处,位于loadData

    constexpr char MAGIC_START[4] = { 'G', 'R', 'I', 'B' };
    constexpr char MAGIC_END[4] = { '7', '7', '7', '7' };

    auto start = std::search(rawdata.cbegin(),
        rawdata.cend(),
        std::begin(MAGIC_START),
        std::end(MAGIC_START));

    auto end = std::search(rawdata.cbegin(),
        rawdata.cend(),
        std::begin(MAGIC_END),
        std::end(MAGIC_END));

    ByteVec data(start, end + sizeof(MAGIC_END));
                        ^^^^^^^^^^^^^^^^^^^^^^^
Run Code Online (Sandbox Code Playgroud)

考虑rawdata不包含MAGIC_START字符但包含MAGIC_END字符的情况。将startend形成有效的迭代范围是多少?

考虑rawdata不包含MAGIC_END字符时会发生什么情况。会end + sizeof(MAGIC_END)有效吗?

您不应该假定这两个调用可以std::search正常工作。你应该添加一些错误检查,通过测试是否start == rawdata.end()还是end == rawdata.end()。如果其中任何一个为真,则出了点问题(可能是rawdata字符串输入错误)。

您还应该学习如何使用调试器,并了解编译器提供的用于检测错误的其他工具(例如-fsanitize=undefined-D_GLIBCXX_DEBUG应使用GCC 和选项来帮助确认错误的存在,并应使用GDB查找这些错误的发生位置) 。