linux - C++ Libzip + remove = core dump -
linux - C++ Libzip + remove = core dump -
i have issue when using libzip. on linux , have installed library using sudo apt-get install libzip2 libzip-dev
(so not latest version).
here 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 file nowadays if(isfilepresent(zipfile)) { // if(remove(tmp) != 0) if(remove(zipfile.c_str()) != 0) { homecoming zip_error; } } // open zip archive zip = zip_open(zipfile.c_str(), zip_create, &err); // if there error on opening if(err != zip_er_ok) { cout << "error when opening" << endl; homecoming zip_error; } // if zip file not open if(zip == null) { zip_close(zip); cout << "error when zip opens" << endl; homecoming zip_error; } // zip_source_file zip file can added 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; homecoming zip_error; } // add together zipped file 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; homecoming zip_error; } // zip_source_file zip file can added 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; homecoming 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; homecoming zip_error; } // sleep(180); // closing archive zip_close(zip); homecoming 0; }
this code supposed take 2 files in filestozip folder , compress them in ziptest file in fileszip folder.
to so, first checks if ziptest file exists. if so, deletes it. , opens zip archive, zip files add together , add together them archive before closing archive.
so problem :
when zip archive fileszip/ziptest doesnt exist works fine. when zip archive fileszip/ziptest exist, got core dumped.
what have tried far :
i thought because using strings names of files. tried char , did not alter anything then thought because remove task not finished , confict might encountered. set sleep(180) (in seconds) after each function call. did not alter anything i tried set 1 file in archive. did not alter anything i ran gdb see happening. tried both when zip archive existed , did not. if archive didn't existed : went smoothly till homecoming 0 , saw programme redefined filetozip , filetozip2, did homecoming 0 stop. if archive exists : same thing says cannot find bounds of current function. (i have read here means gdb doesnt have debugging info , unhappy that..)does have thought problem ?
this dangerous:
bool isfilepresent(string const& path) { struct stat *buf; return(stat(path.c_str(), buf)==0); }
you don't allocate memory struct stat*
random memory gets written when function called - causing crash.
try this:
bool isfilepresent(string const& path) { struct stat buf; // memory allocated on stack object return(stat(path.c_str(), &buf)==0); // pass address function }
it creates local struct stat
object , passes address function.
c++ linux libzip
Comments
Post a Comment