Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
454 views
in Technique[技术] by (71.8m points)

c - malloc() seg fault in localtime()?

Here's ny stack

malloc() at 0xb7dfd333  
strdup() at 0xb7e01866  
tzset_internal() at 0xb7e2ef68  
__tz_convert() at 0xb7e2f26a    
localtime() at 0xb7e2d901   
Send_Trace() at my_trace.c:265 0x8053373    

and here's the offending code ..

void Send_Trace(const char const *Trace_Text, ...)
{
   time_t time_now = time(NULL);
   tm = *localtime(&time_now);

It is generally working fine, but occassionally throws the seg fault shown above.

Any ideas?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Any ideas?

Any crash inside malloc or free is in 99.999% of cases the result of earlier heap corruption elsewhere.

Examples of heap corruption which could lead to subsequent crash in malloc: calling free on a non-allocated memory, calling free on some pointer twice, overflowing or underflowing a heap-allocated buffer, etc. etc.

The fastest way to find such bugs are: valgrind (if available on your platform), or AddressSanitizer (implemented in recent versions of Clang and GCC).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...