简体   繁体   English

确定 const char* 是字符串文字还是变量

[英]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结构的StateProtect成员以查看内存是否可访问以及是否具有您需要的访问权限。

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.

相关问题 文字符号和字符串变量之间的串联,然后返回const char * - A concatenation between a literal symbol and a string variable then return const char* 字符串文字vs const char *函数重载 - String literal vs const char* function overload 是否可以合法地重载字符串文字和const char *? - Is it possible to legally overload a string literal and const char*? const char *和literal string之间有什么区别? - What is the difference between const char * and literal string? 将字符串文字指定给指向const char的指针 - Assigning string literal to pointer to const char 如何使用字符串文字初始化 const char [] - How to initialize a const char [] with a string literal 将字符串文字常量定义为 char const* 和 wchar const* - Defining string literal constants once both as char const* and wchar const* 从C ++的构造函数中向成员const char *变量分配字符串文字时会发生什么? - What happens when assigning a string literal to a member const char* variable from within a constructor in C++? C++ - 确定 const char* 是否指向字符串文字或动态对象 - C++ - Determine if const char* points to string literal or dynamic object 使用const引用和const变量声明常量文字字符串 - Declaring a constant literal string with const reference versus const variable
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM