其他分享
首页 > 其他分享> > C Libzip删除=核心转储

C Libzip删除=核心转储

作者:互联网

使用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\0";

    // 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并没有改变任何东西
>然后我认为这是因为删除任务尚未完成,然后可能会遇到冲突.所以我在每个函数调用后都放置了sleep(180)(以秒为单位).它没有改变任何东西
>我还尝试仅将一个文件放入存档.没有改变任何东西
>我运行gdb以查看发生了什么.当zip存档已经存在并且不存在时,我都尝试了.

>如果归档文件还不存在:一切顺利,直到返回0,然后我看到程序重新定义了fileToZip和fileToZip2,然后又返回了0,然后将停止.
>如果存档已经存在:它执行相同的操作,但随后说找不到当前函数的边界. (我读过here,这意味着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对象,并将其地址传递给该函数.

标签:linux,c-4,libzip
来源: https://codeday.me/bug/20191029/1957108.html