For the program I am writing, it is useful for me to calculate the sizes of the files that I calculate using the iostream tellg and seekg functions, but this leads to a warning from -Wstack-protector. The following code reproduces the "problem":
#include <iostream> std::streamsize get_file_size(std::ifstream& ifs) { // line 12 (in warning, below) const std::streamsize start = ifs.tellg(); ifs.seekg(0,std::ios::end); const std::streamsize end = ifs.tellg(); ifs.seekg(start); return (end-start); }
g ++ (flags: -fstack-protector -Wstack-protector, compiler version: 4.4.3 (Ubuntu 4.4.3-4ubuntu5), system: Ubuntu 10.04 x86_64 ) gives a warning:
<i> f.cc: In the function 'std :: streamsize get_file_size (std :: ifstream &):
f.cc:12: warning: non-protective function: buffer at least 8 bytes
(I get the same results when I use GCC 4.5.2, downloaded and compiled from GNU directly.)
Is it expected how stack break protection works (generally or using GCC) and / or how ifstream and seekg / tellg work? If so, is it possible to ignore this warning, or is there something better I can do?
Edit
Actually, some of the above code are redundant. Just to clarify what happens:
#include <iostream> void f1(std::ifstream& ifs) { // line 6 ifs.tellg(); } void f2(std::ifstream& ifs) { // line 10 // call seekg(std::streampos) ifs.seekg(0); } void f3(std::ifstream& ifs) { // call seekg(std::streamoff, std::ios_base::seekdir) ifs.seekg(0,std::ios::beg); }
leads to g ++ (same characteristics as above):
<i> main.cc: In the function 'void f1 (std :: ifstream &):
main.cc:6: warning: non-protective function: without a buffer of at least 8 bytes in length
main.cc: In the function 'void f2 (std :: ifstream &):
main.cc:10: warning: non-protective function: no buffer at least 8 bytes long
Interestingly, f3 does not cause a warning.