64位Linux系统上可疑strftime的内存损坏 < >我有C++代码库,它使用Solaris 10使用 G++< /Colle>工具链V4.92.此代码库正在移植到64位Linux。我在执行看似无辜的调用时,遇到了一个奇怪的运行时内核转储,这是由SIGSEGV引起的。APIstrftime具有以下签名: size_t strftime(char *s, size_t max, const char *format, const struct tm *tm);
众所周知,64位可执行文件上的64位Linux系统上可疑strftime的内存损坏 < >我有C++代码库,它使用Solaris 10使用 G++< /Colle>工具链V4.92.此代码库正在移植到64位Linux。我在执行看似无辜的调用时,遇到了一个奇怪的运行时内核转储,这是由SIGSEGV引起的。APIstrftime具有以下签名: size_t strftime(char *s, size_t max, const char *format, const struct tm *tm);,c++,linux,64-bit,32bit-64bit,strftime,C++,Linux,64 Bit,32bit 64bit,Strftime,众所周知,64位可执行文件上的size\u t是64位(无符号长),而32位系统上是32位。 我有以下一些功能: void somefunc(X *& p) { p = new X[20000]; // p gets allocated and have a decent pointer value char s[30]; time_t t = time(NULL); struct tm *tmp = localtime(&t); strftime(s,
size\u t
是64位(无符号长)
,而32位系统上是32位。
我有以下一些功能:
void somefunc(X *& p)
{
p = new X[20000];
// p gets allocated and have a decent pointer value
char s[30];
time_t t = time(NULL);
struct tm *tmp = localtime(&t);
strftime(s, 30, "%Y %m %d %T", tmp); // apparently innocent call
// after this, p gets garbled and access to p cause crash
}
相同的代码在32位Solaris系统上运行正常,但在Linux-64位系统上会导致运行时内存损坏。
只是一个嫌疑犯,大小是8字节还是4字节可能是个问题?例如,打个比方,在执行简单的printf
时,32位size\u t
需要%d
作为格式说明符,而64位size\u t
需要%ld
作为格式说明符。strftime格式是否也存在类似的原因?
另一个线索是,如果我注释掉strftime语句,代码在64位上运行正常。
听起来怪怪的。请思考。您确定30个字符的缓冲区足够大吗?
strftime
的返回值是多少?另一件事:size\u t
不必是unsigned long
,它可以是unsigned long long
或满足能够表示任何对象大小要求的任何其他类型。我并不真正理解你关于%d
和size\u t size的句子,因为在这种格式中,string%d
表示月日(这不是printf
)。这里的第一个可疑之处是使用非可重入版本的localtime
。如果这是多线程应用程序,*tmp
的值可能会随机更改。此调用可确保互斥,关于大小,我刚才提到了%d,以与printf格式类似,我猜strftime一定使用了类似的方法mechanism@NathanOliver从文件上看,我认为30不是太小,除了strftime之外,您是否还发现其他可能导致内存损坏的原因?注释掉strftime可以消除腐败