检测可写静态数据
我刚刚发现我正在处理的代码的某些部分错误地使用了可写静态数据,而它可以/应该使用常量数据。
缺少对“静态”进行愚蠢的搜索和替换 -> “static const”,有什么方法可以防止所有“静态”数据可写,就像如何使常量字符串数据显式可写一样?
我使用的是GCC工具链,开发目标是x86。
I just discovered that some parts of the code I am working on incorrectly uses writeable static data where it could/should use constant data.
Short of doing a dumb search-and-replace for "static" -> "static const", is there any way to preventing all 'static' data from being writeable, much like how constant string data can be made explicitly writeable?
I am using the GCC toolchain, development target is x86.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您使用的某些库中可能存在可写的静态数据。 (例如标准 C 和 C++ 库)。制作这个常量会很糟糕。
最好检查一下代码并手动更改内容。
您可以使用
nm
获取.o
文件中的符号列表。在 nm 输出中,第一列给出了符号的类型;字母B
、C
、D
、G
或S
表示可写数据。最后一列给出了(损坏的)变量名称。可以编写一个小脚本来解析 nm 输出并查找这些输出。There's probably writable static data in some of the libraries you use. (Such as the standard C and C++ libraries). Making that const would be bad.
It's probably better to go through your code and change things manually.
You can use
nm
to get a list of symbols in your.o
files. In thenm
output, the first column gives the type of symbol; the lettersB
,C
,D
,G
orS
indicate writable data. The last column gives the (mangled) variable name. It's possible to write a little script to parse thenm
output and look for these.我想更好的方法是将“const”添加到您拥有的所有变量中。您可以使用“#define static static const”(请注意,无论您已经更改它,它都会中断),但我不建议这样做(将使您的代码可读性大大降低,并且可能会破坏一些东西,并且您不会'不能声明静态函数)。
I guess the better way is adding the "const" to all variables you have. You could use a "#define static static const" (note that it would break wherever you already changed it) but I don't recommend doing so (will make your code much less readable and possibly will break some things, and you won't be able to declare static functions).