LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-23-2012, 05:09 AM   #1
batman4
Member
 
Registered: Jul 2012
Posts: 47

Rep: Reputation: Disabled
confusion with concepts of strcmp in c language


strcmp compares two strings and gives the output ..
but if we fill one string with all 0s then the output is allways 0.ie both the strings are same.
why is his happening

Code:
#include <stdio.h>
#include <string.h>

int main (int argc,char *argv[])
{
        char szInput[3];
        int flag=0;
        memset(szInput,0,sizeof(szInput));
        strcpy(szInput, argv[1]);
        if(   strcmp(szInput, "a")== 0){
        flag =1;
        }
        if (flag){
            printf("correct");
        }
        else
            printf("incorrect ");
        return 0;
    }
if the output is ./a.out 0000
it gives correct
 
Old 08-23-2012, 05:25 AM   #2
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
You declared a buffer of size 3; then stuffed input of "0000\0" into that buffer. strcpy() will copy up to the terminating null-character. Thus szInput will have "0000" -- note that there is not a terminating null in that buffer because you have exhausted all the space, and in fact have overrun the buffer space, perhaps even overwriting the value of 'flag' which sits on the stack right after szInput.

Anyhow, I'm not sure how you got your result; perhaps you should fix your code or adjust how much input (say at most two character) you provide from the command line.


P.S. '0' and \0 are not the same. The former is the character zero, whose ASCII value is 48. The latter is a NUL character, whose ASCII value is 0.

P.S. #2 Never use strcpy(); use strncpy() instead. Similarly, don't use gets(), sprintf(), or any other library function where you cannot specify the length of the destination buffer.

Last edited by dwhitney67; 08-23-2012 at 05:29 AM.
 
1 members found this post helpful.
Old 08-23-2012, 05:30 AM   #3
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,880
Blog Entries: 1

Rep: Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871
You merrily overwrite your stack with the strcpy, so the results are unpredictable. Fix:

Code:
#include <stdio.h>
#include <string.h>

int main (int argc,char *argv[])
{
    const char *szInput;

    if (argc<2) {
	printf ("give param\n");
	return 0;
    }
    szInput= argv[1];

    if (strcmp (szInput, "a")== 0) {
	printf ("\"%s\" equal to \"a\"\n", szInput);
    } else {
	printf ("\"%s\" not equal to \"a\"\n", szInput);
    }
    return 0;
}
 
Old 09-27-2012, 07:33 PM   #4
KernelJay
LQ Newbie
 
Registered: Aug 2012
Posts: 15

Rep: Reputation: Disabled
For some additional insight into why strcpy is so dangerous, check out my latest blog post on the subject:
VERT Vuln School: Stack Buffer Overflows 101

Part 1: Introducing the Bug
Part 2: Explaining the Stack
Part 3: Exploiting the Bug

Thanks,
Craig

Last edited by KernelJay; 09-29-2012 at 08:36 AM. Reason: Added link to part 3
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] strcmp returns non-zero even if strings are the same kvm1983 Programming 2 08-04-2009 10:07 PM
strcmp() in kernel module simon_qwl Programming 3 07-02-2007 12:47 AM
source code of strcmp fssengg Linux - Newbie 3 06-22-2006 08:01 AM
[C]How to compare two strings w/o using strcmp() kponenation Programming 22 11-23-2005 08:29 AM
c++ strings-strcmp function...... sachitha Programming 4 09-12-2004 07:28 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:29 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration