为了好玩,我写了一个尝试根据其他文字的地址进行猜测。它通过将潜在文字的地址与另一个已知文字、堆栈地址和堆地址进行比较来工作。如果潜在地址比其他地址更接近文字,则假定潜在地址是文字。在实践中可能不可靠。这是它的简单版本:
int is_literal_simplistic(char *s) {
char *literal = "literal";
char stack[] = "stack";
char *heap = malloc(1);
free(heap);
unsigned long literal_delta = labs(literal - s);
unsigned long stack_delta = labs(stack - s);
unsigned long heap_delta = labs(heap - s);
return (literal_delta < stack_delta && literal_delta < heap_delta);
}
这是更简洁的版本。它可能更简单:
int is_literal(char *s) {
char *heap = malloc(1);
free(heap);
unsigned long literal_delta = labs("literal" - s);
unsigned long stack_delta = labs((char *)&s - s);
unsigned long heap_delta = labs(heap - s);
return (literal_delta < stack_delta && literal_delta < heap_delta);
}
完整的可运行测试:
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
//#define DEBUG
#ifdef DEBUG
void debug_literal(unsigned long literal,
unsigned long stack,
unsigned long heap,
unsigned long literal_delta,
unsigned long stack_delta,
unsigned long heap_delta) {
printf("literal(%lx) stack(%lx) heap(%lx)\n", literal, stack, heap);
printf("literal(%lu) stack(%lu) heap(%lu)\n", literal_delta, stack_delta, heap_delta);
int answer = (literal_delta < stack_delta && literal_delta < heap_delta);
printf("\t%s\n", answer ? "literal" : "other");
}
#else
void debug_literal(unsigned long literal,
unsigned long stack,
unsigned long heap,
unsigned long literal_delta,
unsigned long stack_delta,
unsigned long heap_delta) {
}
#endif
int is_literal_simplistic(char *s) {
char *literal = "literal";
char stack[] = "stack";
char *heap = malloc(1);
free(heap);
unsigned long literal_delta = labs(literal - s);
unsigned long stack_delta = labs(stack - s);
unsigned long heap_delta = labs(heap - s);
debug_literal((unsigned long)literal, (unsigned long)stack, (unsigned long)heap,
literal_delta, stack_delta, heap_delta);
return (literal_delta < stack_delta && literal_delta < heap_delta);
}
int is_literal(char *s) {
char *heap = malloc(1);
free(heap);
unsigned long literal_delta = labs("literal" - s);
unsigned long stack_delta = labs((char *)&s - s);
unsigned long heap_delta = labs(heap - s);
debug_literal(0,0,0, literal_delta, stack_delta, heap_delta);
return (literal_delta < stack_delta && literal_delta < heap_delta);
}
void test_literal_function(int(*liternal_fn)(char *)) {
char *literal = "literal_test";
char stack[] = "stack_test";
char *heap = malloc(40);
printf("\t%s\n", liternal_fn(literal) ? "literal" : "other");
printf("\t%s\n", liternal_fn(stack) ? "literal" : "other");
printf("\t%s\n", liternal_fn(heap) ? "literal" : "other");
printf("\n");
free(heap);
}
int main() {
test_literal_function(is_literal_simplistic);
test_literal_function(is_literal);
return 0;
}