Mingw 调试的奇怪行为

发布于 2025-01-08 01:41:24 字数 1540 浏览 1 评论 0原文

我在运行和调试这段代码时遇到问题:

bool readSectionHeaders(char* path, int numOfSections, int peSectionsOff, IMAGE_SECTION_HEADER* out) {
    bool retr = false; //return value

    //open file
    FILE* file;
    file = fopen (path, "rb");
    if(file == NULL) {
        perror("WRG"); //TODO
        return false;
    }

    do { //do while(false) only for easier error correction

        //seek to first section
        fseek(file, peSectionsOff, SEEK_SET);

        //read all sections
        unsigned int count;
        IMAGE_SECTION_HEADER sectionHeaders[numOfSections];
        count = fread(sectionHeaders, sizeof(IMAGE_SECTION_HEADER), numOfSections, file);

        //check Bytes count
        if(count != sizeof(IMAGE_SECTION_HEADER)*numOfSections) {
            break;
        }

        //copy sections
        memcpy(out, sectionHeaders, count);

        //exit successfully
        retr = true;
    } while(false);

    //exit
    fclose(file);

    return retr;
}

奇怪的是,即使它读取文件,它也会返回 false。我尝试调试它,这是最奇怪的部分。

我逐行进行,直到这一行

if(file == NULL) {

然后,即使 file 不为 NULL,它也会跳过 perror 并移动到

return false;

But 根本不返回。

我再次逐行进行操作,直到

retr = true;

它似乎执行某些操作,但 retr 仍然是错误的。

然后它关闭文件并返回 false。

我从来没有遇到过这样的事情。 我尝试清理项目、重建、甚至删除文件并从 subversion 重新下载它们。在使用此功能之前,我使用类似的功能 - 我读取 PE 标头。所以我认为问题可能出在读取文件上,但它并不能解释调试行为。

从函数返回后,我使用 perror 并且它写没有错误。

我将 mingw 与 QtCreator 一起使用。

提前致谢。

I have problem running and debugging this piece of code:

bool readSectionHeaders(char* path, int numOfSections, int peSectionsOff, IMAGE_SECTION_HEADER* out) {
    bool retr = false; //return value

    //open file
    FILE* file;
    file = fopen (path, "rb");
    if(file == NULL) {
        perror("WRG"); //TODO
        return false;
    }

    do { //do while(false) only for easier error correction

        //seek to first section
        fseek(file, peSectionsOff, SEEK_SET);

        //read all sections
        unsigned int count;
        IMAGE_SECTION_HEADER sectionHeaders[numOfSections];
        count = fread(sectionHeaders, sizeof(IMAGE_SECTION_HEADER), numOfSections, file);

        //check Bytes count
        if(count != sizeof(IMAGE_SECTION_HEADER)*numOfSections) {
            break;
        }

        //copy sections
        memcpy(out, sectionHeaders, count);

        //exit successfully
        retr = true;
    } while(false);

    //exit
    fclose(file);

    return retr;
}

What is strange is that it returns false even when it reads the file. I tried to debug it and here is the strangest part.

I go line by line until this one

if(file == NULL) {

Then even though file is not NULL it skips perror and moves to

return false;

But does not return at all.

I again go line by line until

retr = true;

where it seems to do something, however retr remains false.

Then it closes file and returns with false.

I have never come across something like this.
I tried cleaning project, rebuilding, even deleting files and redownloading them from subversion. Before using this function, I use similar one - I read PE headers. So I though a problem could be with reading the file but it doesn§t explain debug behavior.

After returning from function, I use perror and it writes No error.

I use mingw with QtCreator.

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

紫罗兰の梦幻 2025-01-15 01:41:24

我会做更多类似的事情,如果它能够加载整个数组那么它将返回 true。

std::ofstream file(path, std::ios::binary);

if(!file) {
    std::cerr << "failed to load file" << std::endl;
    return false;
}   

file.seekg (peSectionsOff, ios::beg);

IMAGE_SECTION_HEADER sectionHeaders[numOfSections];
size_t size=sizeof(sectionHeaders)*numOfSections;

return  //return true if the whole buffer is filled
    file.readsome(static_cast<char*>(sectionHeaders), size) == size;

未经测试

这可能会有所帮助
http://en.cppreference.com/w/cpp/io

I would do something more like this, if it is able to load the whole array then it will return true.

std::ofstream file(path, std::ios::binary);

if(!file) {
    std::cerr << "failed to load file" << std::endl;
    return false;
}   

file.seekg (peSectionsOff, ios::beg);

IMAGE_SECTION_HEADER sectionHeaders[numOfSections];
size_t size=sizeof(sectionHeaders)*numOfSections;

return  //return true if the whole buffer is filled
    file.readsome(static_cast<char*>(sectionHeaders), size) == size;

UNTESTED

this might be helpful
http://en.cppreference.com/w/cpp/io

香橙ぽ 2025-01-15 01:41:24

好吧,这是我和 mingw 的问题。我已经重新安装了整个QtSDK,但没有效果。然后我安装了不同版本的 mingw 并设置 qt Creator 来使用它。现在调试器可以正常工作了。我不知道发生了什么,但是 cerr << “测试”;也停止与旧的 mingw 合作,这是 100% 正确的。

正如 111111 所建议的,问题出在 if break 子句上。我认为 read 返回读取的字节数,但这根本不是真的:)。

现在可以了,感谢111111的建议:)。

Ok, this was both my and mingw's problem. I've re-installed whole QtSDK with no effect. Then I installed different version of mingw and set qt creator up to use it. Now debugger works without problem. I'm not sure what happened but cerr << "TEST"; also stopped working with the old mingw and this is 100% correct.

As 111111 suggested, problem was with if break clause. I thought read returns number of bytes read and this was simply not true :).

Now it is working, thanks to 111111 for his suggestion :).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文