[英]Decide if const char* is a string literal or a variable
Is there any simple method to detect, if the parameter passed to a function(const char *argument) was a constant literal or a variable?是否有任何简单的方法可以检测传递给函数(const char *argument)的参数是常量文字还是变量?
I'm trying to fix errors in some code, which is filled with IsBadWritePtr calls, which throw access violation exceptions if the parameter was a constant literal.我正在尝试修复某些代码中的错误,这些代码充满了 IsBadWritePtr 调用,如果参数是常量文字,则会抛出访问冲突异常。
This was a terrible design stupidity but now I'm not allowed to change the awkward behavior.这是一个可怕的设计愚蠢,但现在我不允许改变尴尬的行为。
You can add a different overload that will be a better match for string literals.您可以添加一个不同的重载,这将更好地匹配字符串文字。 This is not really science but just heuristics:
这不是真正的科学,而只是启发式:
void f(const char* p); // potential literal
void f(char *p); // pointer to non-const
Another idea would be taking advantage that literals are really arrays :另一个想法是利用文字实际上是数组:
template <int N>
void f(const char (&_)[N]); // potential literal
Note that they don't quite detect literal vs. not literal , but rather some of the other features.请注意,它们并没有完全检测到文字与不是文字,而是检测其他一些功能。
const char* p = createANewString(); f(p);
will resolve to f(const char*)
, and const char x[] = { 'A', 'b', 'c', '\\0' };
将解析为
f(const char*)
和const char x[] = { 'A', 'b', 'c', '\\0' };
will resolve to the template.将解析为模板。 Neither of them are literals , but you probably don't want to modify either.
它们都不是literals ,但您可能也不想修改。
Once you make that change, is should be simple to find out where each of the overloads is called.一旦进行了更改,就应该很容易找出每个重载的调用位置。
This all works on the premise that the main function should not take the argument as const char*
if it modifies it internally, and that the issue you are facing is because for backwards compatibility your compiler is allowing the call to a function that takes a pointer to non-const with a literal...这一切的前提是,如果主函数在内部修改它,则它不应将参数作为
const char*
使用,并且您面临的问题是因为为了向后兼容,您的编译器允许调用带有指针的函数与文字非常量...
I don't think there is an way to detect that not at-least without using some hackery.
我不认为有一种方法可以在不使用一些黑客的情况下至少检测到这一点。
Since the interface takes a const char *
the responsibility of the function is to not modify the passed string anyways.由于接口采用
const char *
函数,因此无论如何都不修改传递的字符串。 You need to modify the implementation because it is simply incorrect.您需要修改实现,因为它完全不正确。
VirtualQuery
can be used to detect if the address is writable, read-only, or inaccessible. VirtualQuery
可用于检测地址是否可写、只读或不可访问。 Examine the State
and Protect
members of the returned MEMORY_BASIC_INFORMATION
structure to see if the memory is accessible and has the access you need.检查返回的
MEMORY_BASIC_INFORMATION
结构的State
和Protect
成员以查看内存是否可访问以及是否具有您需要的访问权限。
One VERY hackish way would involve checking if the pointer is in the .rdata segment.一种非常hackish 的方法是检查指针是否在.rdata 段中。
Use dumpbin /headers
after the build to retrieve the offset and length of the .rdata section, or parse the PE headers yourself.在构建后使用
dumpbin /headers
来检索 .rdata 部分的偏移量和长度,或者自己解析 PE 头。 naturally, this is toolchain specific and generally a bad idea.自然,这是特定于工具链的,通常是个坏主意。 Also, if the code needs to interoperate with DLLs, you'd have to check several executables and several .rdata segments.
此外,如果代码需要与 DLL 互操作,则必须检查多个可执行文件和多个 .rdata 段。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.