C ++ Libzip + remove = core dump

I have a problem using libzip. I am on linux and installed the library using sudo apt-get install libzip2 libzip-dev

(so this is not the latest version).

Here is my code:

#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;
}

      

This code should take two files in the filesToZip folder and compress them into a zipTest file in the filesZip folder.

To do this, first check if the zipTest file exists. If so, it removes it. And then it opens zip archive, zip files to add and add to archive before closing archive.

So my problem is:

when the zip archive filesZip / zipTest doesn't exist then it works fine. when the filesZip / zipTest zip exists, then I got a dump kernel.

What I've tried so far:

  • I thought it was because I used strings for filenames. I tried with char and didn't change anything
  • then I thought it was because the delete task was not completed and then there might be a conflict. So I put sleep (180) (in seconds) after each function call. It didn't change anything.
  • I also tried to put only one file in the archive. Changed nothing
  • I ran gdb to see what was going on. I've tried both when the zip archive already existed and didn't.
    • if the archive didn’t exist yet: everything went smoothly until it returned 0, and then I saw that the program had overridden fileToZip and fileToZip2, then did another return of 0, and then stopped.
    • if the archive already exists: it does the same, but then says it cannot find the bounds of the current function. (I read here that this means gdb has no debug information and is unhappy with it.)

Anyone can figure out what my problem might be?

+3


source to share


1 answer


Is it dangerous:

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

      

You don't allocate any memory for yours struct stat*

, so random memory gets written when the function is called, which can crash.



Try the following:

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
}

      

It creates a local object struct stat

and passes its address to the function.

+4


source







All Articles