strcmp core dumps


 
Thread Tools Search this Thread
Top Forums Programming strcmp core dumps
# 1  
Old 02-21-2006
strcmp core dumps

hi everyone,

Right now when I do:

strcmp(s1, s2);

i get a core dump because at times s1 or s2 can be nothing so that makes strcmp() core dump.

What is the solution, if at times I expect one of them (or both) to be NULL?
I want to be able to compare that s1 is NULL and s2 is "blah" or whatever.
My point is to know whenever these 2 differ.

any ideas?
# 2  
Old 02-21-2006
Though not pretty (and not fully correct)

Code:
strcmp ( ( s1 ? s1 : "" ), ( s2 ? s2 : "" ) );

Something like this would be a bit more graceful:

Code:
    int             Differ;


    if ( s1 == NULL || s2 == NULL )
    {
        if ( s1 == s2 )
        {
            Differ = 0;
        }
        else
        {
            Differ = 1;
        }
    }
    else
    {
        Differ = strcmp (s1, s2);
    }

    if ( Differ ) ...

# 3  
Old 02-21-2006
Quote:
Originally Posted by hegemaro
Though not pretty (and not fully correct)

Code:
strcmp ( ( s1 ? s1 : "" ), ( s2 ? s2 : "" ) );

Something like this would be a bit more graceful:

Code:
    int             Differ;


    if ( s1 == NULL || s2 == NULL )
    {
        if ( s1 == s2 )
        {
            Differ = 0;
        }
        else
        {
            Differ = 1;
        }
    }
    else
    {
        Differ = strcmp (s1, s2);
    }

    if ( Differ ) ...


thanks, was hoping there would be a more "correct" way to do this.
I really thought this would be a more common problem with a lib function I could use in this case. But thanks much for your solution, if all else fails, I may have to use that.
# 4  
Old 02-21-2006
Perhaps you misunderstood me, the single line was not fully correct -- it can not tell the difference between a NULL and a zero-length string. The larger chunk of code with the Differ boolean is fully correct.
# 5  
Old 02-21-2006
Quote:
Originally Posted by hegemaro
Perhaps you misunderstood me, the single line was not fully correct -- it can not tell the difference between a NULL and a zero-length string. The larger chunk of code with the Differ boolean is fully correct.
right, what I meant was I was hoping I could use something like strncmp() instead that would know how to treat NULL...etc..

thanks again though, I think your latter suggestion is the way I'll have to go.
# 6  
Old 02-22-2006
Barfing on a NULL pointer is actually the desired behavior.

Merely having a zero length string won't be a problem for strcmp. The mian cause for this is that you are missing the "trailing" nul character in a string.

If the strcmp occurs in the function where a and b are declared otherwise pass in sizoef(a) and sizeof(b) as arguments:
Code:
#define BARADRG -256
/* strcmp -- strncmp can't return a minus 256 */
if(a!=NULL && b!=NULL)
{
      result=strncmp(a,b, ( sizeof(a)>sizeof(b) )? sizeof(a):sizeof(b));
}
else
      result=BADARG;
}

# 7  
Old 02-26-2006
My obsessive-compulsiveness is shining through. Combing both Jim's post and mine, we can get a "null strcmp" function that is comparable to the strcmp library function in performance.

It is identical to the Solaris strcmp function returning the ordinal difference between the first pair of non-matching bytes. (As I recall, the AIX implementation returns a 0 for a match and 1 for a non-match). This function returns -256 if S1 is NULL, 256 if S2 is NULL, and 0 if both S1 and S2 are NULL.


Code:
    int nstrcmp ( void * S1 , void * S2 )
    {
        if ( S1 == NULL )
        {
            S2 = ( S2 ? (void *) 256 : S2 );
        }
        else if ( S2 == NULL )
        {
            S1 = (void *) 256;
        }
        else
        {
            unsigned char * s1 = (unsigned char *) S1,
                          * s2 = (unsigned char *) S2;
   

            while ( *s1 )
            {
                if ( *s1 != *s2 )
                {
                    break;
                }
                s1++;
                s2++;
            }

            return ( (int) (*s1 - *s2) );
        }

        return ( (int) (S1 - S2) );
    }

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. HP-UX

Generate core dumps

kill -SEGV <pid> gives me the core file for that process but also terminates the process. Can I not get the core dump without terminating the process ? (2 Replies)
Discussion started by: mohtashims
2 Replies

2. Programming

Stack only core dumps

I'm working on a program in Linux with a group of people scattered around the country. When we have a crash, I like to send a core dump to the appropriate person so that they can understand the problem better. The problem is that our application uses several gigabytes worth of data and these... (4 Replies)
Discussion started by: bmsterner
4 Replies

3. Red Hat

generating core dumps

Hi I have a Fedora installed and I try to generate my application's core dump file. My system has no coredump limit: $ ulimit core file size (blocks, -c) unlimited But when my application crashes no core dumps generated. I can generate dump file using gcore but it is not appropraite... (1 Reply)
Discussion started by: xyzt
1 Replies

4. Ubuntu

enabling core dumps for daemon processes

Hello, I am debugging a program which works as daemon. It sigfaults, unfortunately I'm unable to generate core dump file. Here is what I am doing: tsurko@bastila:~$ ulimit -c unlimited tsurko@bastila:~$ ulimit -c unlimited tsurko@bastila:~$ cat /etc/sysctl.conf | grep 'core_pattern'... (1 Reply)
Discussion started by: tsurko
1 Replies

5. Solaris

core dumps

i had a situation where a process was defunct. preap would not reap the process and gcore would not work properly (not sure why). therefore, the suggestion was to force a panic and collect the core dump. obviously you could do a savecore -L and capture the dump without bringing down the system.... (3 Replies)
Discussion started by: pupp
3 Replies

6. Programming

AIX core dumps

My program is not dumping core when hitting a segmentation violation inside a thread. However, it dumps core when the segv occurs within main. Any ideas on how to diagnose this? AIX 5.3 (4 Replies)
Discussion started by: bean66
4 Replies

7. UNIX for Advanced & Expert Users

Using GDB to analyse different CORE dumps

Hi, Can we modify the GDB source code so as to analyze core dumps from different targets? From my analysis, I think we need to build our section table statically for each target. i.e., including the various address boundaries in build_section_table() function. If this is the case, then the GDB... (2 Replies)
Discussion started by: nsdeeps
2 Replies

8. SCO

SCO 5.07 Panic / Core Dumps

Anyone know how you go about interrogating a panic / core dump with crash for SCO Unix (5 Replies)
Discussion started by: ccarcher
5 Replies

9. Solaris

Generating core dumps

I have the following set up on a Sun server running solaris 5.8 for core dump generation coreadm global core file pattern: /var/core init core file pattern: /var/core global core dumps: enabled per-process core dumps: enabled global setid core dumps:... (4 Replies)
Discussion started by: handak9
4 Replies

10. UNIX for Advanced & Expert Users

reading core dumps

Does anyone know how to read core dumps. Is gdb the only tool for it ? The OS is Solaris. Thanks (2 Replies)
Discussion started by: suntan
2 Replies
Login or Register to Ask a Question