istream 的tellg/seekg 无法防止堆栈崩溃(g++)?
对于我正在编写的程序,计算文件大小对我很有用,我通过使用 iostream 的tellg 和eekg 函数来计算文件大小,但这会导致-Wstack-protector 发出警告。以下代码重现了“问题”:
#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++(标志:-fstack-protector -Wstack-protector,编译器版本:4.4.3 (Ubuntu 4.4.3-4ubuntu5) ,系统:Ubuntu 10.04 x86_64)给出警告:
f.cc:在函数 'std::streamsize get_file_size(std::ifstream&)' 中:
f.cc:12: 警告:不保护功能:没有至少 8 个字节长的缓冲区
(当我使用直接从 GNU 下载并编译的 GCC 4.5.2 时,我得到了相同的结果。)
这是堆栈粉碎保护的工作方式(一般或通过 GCC)和/或 ifstream 和eekg/的工作方式所预期的吗?告诉工作?如果是这样,这个警告不能被忽略吗?或者我可以做更好的事情吗?
编辑:
实际上,上面的一些代码是多余的。只是为了澄清发生了什么:
#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);
}
导致 g++(与上面相同的规格)警告:
main.cc:在函数“void f1(std::ifstream&)”中:
main.cc:6: 警告:未保护功能:没有至少 8 个字节长的缓冲区
main.cc:在函数“void f2(std::ifstream&)”中:
main.cc:10: 警告:不保护功能:没有至少 8 个字节长的缓冲区
有趣的是,f3
不会触发警告。
For a program that I'm writing, it is useful for me to calculate file sizes, which I calculate by using iostream's tellg and seekg functions, but this leads to a warning by -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 the warning:
f.cc: In function ‘std::streamsize get_file_size(std::ifstream&)’:
f.cc:12: warning: not protecting function: no buffer at least 8 bytes long
(I get the same results when I use GCC 4.5.2, downloaded and compiled from GNU directly.)
Is this expected from how stack smashing protection works (in general or by GCC) and/or how ifstream and seekg/tellg work? If so, can't this warning be ignored or is there something better that I can do?
Edit:
Actually, some of the code above is redundant. Just to clarify what's going on:
#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 specs as above) warning:
main.cc: In function ‘void f1(std::ifstream&)’:
main.cc:6: warning: not protecting function: no buffer at least 8 bytes long
main.cc: In function ‘void f2(std::ifstream&)’:
main.cc:10: warning: not protecting function: no buffer at least 8 bytes long
Interestingly, f3
does not trigger a warning.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能不想看到这个。
一般建议是你真的不应该关心,特别是在你的情况下,当你没有分配任何可以使用的内部缓冲区时执行缓冲区溢出攻击。
You might wan't to see this.
And the general advice is you really shouldn't care, especially in your case, when you don't allocate any internal buffers that can be used to perform buffer overflow attack.