C++Libzip+remove=核心转储

C++ Libzip + remove = core dump

本文关键字:转储 核心 C++Libzip+remove      更新时间:2023-10-16

我在使用libzip时遇到问题。我在linux上,并且使用sudo apt-get install libzip2 libzip-dev安装了库(因此它是而不是最新版本)。

这是我的代码:

#include <iostream>
#include <zip.h>
#include <unistd.h>
#include <sys/stat.h>
#define ZIP_ERROR 2
using namespace std;
bool isFilePresent(string const& path)
{
    struct stat *buf;
    return(stat(path.c_str(), buf)==0);
}
int main(void)
{
    struct zip *zip;
    struct zip_source *zip_source;
    int err(0);
    string zipFile("filesZip/zipTest");
    string fileToZip("filesToZip/test1");
    string fileToZip2("filesToZip/test2");
    char tmp[] = "filesZip/zipTest";
    // Test if the file is present
    if(isFilePresent(zipFile))
    {
        // if(remove(tmp) != 0)
        if(remove(zipFile.c_str()) != 0)
        {
            return ZIP_ERROR;
        }
    }
    // Open a zip archive
    zip = zip_open(zipFile.c_str(), ZIP_CREATE, &err);
    // if there is an error on the opening
    if(err != ZIP_ER_OK)
    {
        cout << "error when opening" << endl;
        return ZIP_ERROR;
    }
    // If the zip file is not open
    if(zip == NULL)
    {
        zip_close(zip);
        cout << "error when zip opens" << endl;
        return ZIP_ERROR;
    }
    // zip_source_file zip a file so that it can be added to the zip
    if((zip_source = zip_source_file(zip, fileToZip.c_str(), (off_t)0, (off_t)0))== NULL)
    {
        zip_close(zip);
        zip_source_free(zip_source);
        cout << "pb when zipping file1" << endl;
        return ZIP_ERROR;
    }
    // Add the zipped file to the zip  
    if(zip_add(zip, fileToZip.c_str(), zip_source)==-1)
    {
        zip_close(zip);
        zip_source_free(zip_source);
        cout << "pb when adding file1" << endl;
        return ZIP_ERROR;
    }
    // zip_source_file zip a file so that it can be added to the zip
    if((zip_source = zip_source_file(zip, fileToZip2.c_str(), (off_t)0, (off_t)0))== NULL)
    {
        zip_close(zip);
        zip_source_free(zip_source);
        cout << "pb when zipping file2" << endl;
        return ZIP_ERROR;
    }
    if(zip_add(zip, fileToZip2.c_str(), zip_source)==-1)
    {
        zip_close(zip);
        zip_source_free(zip_source);
        cout << "pb when adding file2" << endl;
        return ZIP_ERROR;
    }
    // sleep(180);
    // Closing the archive
    zip_close(zip);
    return 0;
}

这段代码应该将filesToZip文件夹中的两个文件压缩到filesZip文件夹的zipTest文件中。

为此,它首先检查zipTest文件是否已经存在。如果是,那么它会删除它。然后它会打开一个zip档案,压缩要添加的文件,并在关闭档案之前将其添加到档案中。

所以我的问题是:

当zip存档文件zip/zipTest不存在时,它就可以正常工作。当zip存档文件zip/zipTest确实存在时,我得到了一个转储的核心。

到目前为止,我已经尝试过:

  • 我以为这是因为我使用字符串作为文件名。我尝试了使用char,但它没有改变任何东西
  • 然后我想这是因为删除任务没有完成,然后可能会遇到冲突。所以我在每次函数调用后都会进入睡眠(180)(以秒为单位)。它没有改变任何事情
  • 我还试着在档案中只放一个文件。没有改变任何东西
  • 我运行gdb查看发生了什么。当zip存档已经存在但没有时,我尝试了这两种方法。
    • 如果存档还不存在:一切都很顺利,直到返回0,然后我看到程序重新定义了fileToZip和fileToZip2,然后再返回0,就会停止
    • 如果档案已经存在:它做同样的事情,但说找不到当前函数的边界。(我在这里读到,这意味着gdb没有调试信息,对此感到不满。)

有人知道我的问题是什么吗?

这很危险:

bool isFilePresent(string const& path)
{
    struct stat *buf;
    return(stat(path.c_str(), buf)==0);
}

您没有为struct stat*分配任何内存,因此在调用函数时会写入随机内存,这可能会导致崩溃。

试试这个:

bool isFilePresent(string const& path)
{
    struct stat buf; // memory is allocated on the stack for this object
    return(stat(path.c_str(), &buf)==0); // pass its address to the function
}

它创建一个本地struct stat对象,并将其地址传递给函数。