mercredi 7 novembre 2018

Syscall param open(filename) points to unaddressable byte(s) - checking for existence of a file

I've followed the advice of many for checking if a file exists (this and this and this).

I've tried this:

int checkExists(const char *f) {
    struct stat buf;
    if (stat(f, &buf) == -1) {
        std::cout << "File not found!";
        return -1;
    } else {
        return 0;
    }
}

I've also tried this one:

int checkExists(const char *f) {
    std::ifstream infile(f);
    return infile.good();
}

Both methods, when run with valgrind, result in the following message:

==28091== Syscall param open(filename) points to unaddressable byte(s)
==28091==    at 0x5744A30: __open_nocancel (in /usr/lib64/libc-2.17.so)
==28091==    by 0x56D02EF: _IO_file_fopen@@GLIBC_2.2.5 (in /usr/lib64/libc-2.17.so)
==28091==    by 0x56C3AA3: __fopen_internal (in /usr/lib64/libc-2.17.so)
==28091==    by 0x4EB220F: std::__basic_file<char>::open(char const*, std::_Ios_Openmode, int) (in /usr/lib64/libstdc++.so.6.0.19)
==28091==    by 0x4EED5B9: std::basic_filebuf<char, std::char_traits<char> >::open(char const*, std::_Ios_Openmode) (in /usr/lib64/libstdc++.so.6.0.19)
==28091==    by 0x4EEE73F: std::basic_ifstream<char, std::char_traits<char> >::basic_ifstream(char const*, std::_Ios_Openmode) (in /usr/lib64/libstdc++.so.6.0.19)
==28091==    by 0x40270B: checkExists(char const*) (main.cpp:122)
==28091==    by 0x402794: addToInventory(char const*, char const*, std::string, std::string) (main.cpp:128)
==28091==    by 0x402086: runProgram() (main.cpp:94)
==28091==    by 0x4026D8: main (main.cpp:110)

The strange thing is I've gotten this to work before in the same environment but something about this particular setup isn't working. I've tried changing the filename to be sure there isn't something odd about that file.

My file is named george and it's located in the same location from which I'm running this script. I've tried ./george and george as the filename, same errors. The file is definitely there:

$ ls -1 george dothething.bin
george
dothething.bin

Any ideas?

OS: Linux

Distro: CentOS 7.5

Valgrind: Version 3.13

GCC: Version 4.8.5

Aucun commentaire:

Enregistrer un commentaire