Gcc/Glibc bug?

Bob Snyder bob.snyder at cox.net
Tue Jan 6 20:37:18 PST 2004


johan wrote:

>Joel Miller wrote:
>
>  
>
>>On Tue, 06 Jan 2004 01:26:48 +0100, johan <johan at gvd.dyn.dhs.org> wrote:
>>
>>    
>>
>>>>/* dumpcore.c
>>>> * Intentionally produces a segmentation violation
>>>> */
>>>>
>>>>int main()
>>>>{
>>>>    int *p;
>>>>    *p = 0;
>>>>}
>>>>
>>>>it compiles fine.
>>>>
>>>>
>>>>When I try to run the resulting executable, however, I get this:
>>>>
>>>>Inconsistency detected by ld.so: dl-fini.c: 66: _dl_fini: Assertion `i
>>>>==
>>>>_rtld_local._dl_nloaded' failed!
>>>>        
>>>>
>>>the pointer p is pointing to an undefined memory location. You try to
>>>assign
>>>a value to that undefined memory location. Personally I'm not supprised
>>>this causes a segfault. Are you sure this is what you want or did you
>>>mean:
>>>
>>>int main {
>>>        int *p;
>>>        p = 0;
>>>}
>>>      
>>>
>>you miss the point of his post. did you even read the comments on his
>>code. this is bit of code is meat to cause a core dump. that is its
>>purpose.
>>    
>>
>
>Indeed, seems that I missed the point. Causing segfaults is not one of my
>hobbys :)
>  
>

Yes, but then again I think your analysis is right on and the true 
reason why this code cannot be relied on to always create a segfault.

int main()
{
     int *p;
     *p = 0;
}
 
The variable p is never assigned, only dereferenced. Since it is an automatic variable (i.e. allocated on the stack) it's value cannot be predicted - it's whatever happens to be at that location in memory. If it happens to point outside the user's address space, segfault. If it happens to fall inside the user's space you may get nothing (or break something). The point is that unless you assign a value to p, you can't predict what p points to, so you can't predict that this program will (or won't) segfault.

As to why it won't compile under certain circumstances as explained in the original post in this thread I have no clue :-)

Bob S,








More information about the lfs-support mailing list