Tweaked getpass() function gives an untraceable bug


 
Thread Tools Search this Thread
Top Forums Programming Tweaked getpass() function gives an untraceable bug
# 1  
Old 06-11-2013
Tweaked getpass() function gives an untraceable bug

I have customized the getpass() as follows:

Code:
char* my_getpass(const char* str) {
    struct termios oflags, nflags;
    static char passwd[64];

    /* disabling echo */
    tcgetattr(fileno(stdin), &oflags);
    nflags = oflags;
    nflags.c_lflag &= ~ECHO;
    nflags.c_lflag |= ECHONL;
    tcsetattr(fileno(stdin), TCSANOW, &nflags);
    printf(str);
    fgets(passwd, sizeof(passwd), stdin);
    passwd[strlen(passwd) - 1] = '\0';

    /* restore terminal */
    tcsetattr(fileno(stdin), TCSANOW, &oflags);
    return passwd;
}

int main() {
    char name[8], *pwd;
    printf("Login as: ");
    fgets(name, sizeof(name), stdin);
    pwd = my_getpass("Password: ");
    if (strcmp(pwd, "george"))
        fprintf(stderr, "\nPassword incorrect\n");
    else {
        puts("\nCorrect Password");
        FILE* fp = fopen("./passwd.txt", "w");
        fprintf(fp, "user: (%s), password: (%s) \n", name, pwd);
        fclose(fp);
    }
}

but now the output file "password.txt" contains a carriage return in the result as
Code:
$ more passwd.txt 
user: (dude
), password: (george)

which should have been as,
Code:
user: (dude), password: (george)

could anyone suggest me how to correct this bug? Smilie
# 2  
Old 06-11-2013
Code:
    fgets(passwd, sizeof(passwd), stdin);
    passwd[strlen(passwd) - 1] = '\0';

This looks to me like you're trying to add a null-terminator to the string.

strlen() only works when the string already has a null terminator.

fgets() gives it a null terminator anyway.

It's not a bug, it's doing exactly what you tell it to... fgets() adds the return key to the string. So change it to this:

Code:
    fgets(passwd, sizeof(passwd), stdin);
    passwd[strlen(passwd) - 2] = '\0';

...to wipe out the very last character in the string, the newline.

Also: Make your buffer 4096, not 8, or someone is almost guaranteed to overflow and crash it.

Last edited by Corona688; 06-11-2013 at 12:02 PM..
This User Gave Thanks to Corona688 For This Post:
# 3  
Old 06-12-2013
Quote:
Originally Posted by Corona688
Code:
    fgets(passwd, sizeof(passwd), stdin);
    passwd[strlen(passwd) - 1] = '\0';

This looks to me like you're trying to add a null-terminator to the string.

strlen() only works when the string already has a null terminator.

fgets() gives it a null terminator anyway.

It's not a bug, it's doing exactly what you tell it to... fgets() adds the return key to the string. So change it to this:

Code:
    fgets(passwd, sizeof(passwd), stdin);
    passwd[strlen(passwd) - 2] = '\0';

...to wipe out the very last character in the string, the newline.

Also: Make your buffer 4096, not 8, or someone is almost guaranteed to overflow and crash it.
Thanks a lot for the impeccable and intuitive hint!! hats off to you "The Geek" Smilie

But a small correction is, I have to do it for the name variable inside main() instead of passwd variable in the my_getpass() function like
Code:
    printf("Login as: ");
    fgets(name, sizeof(name), stdin);
    name[strlen(name) - 1] = '\0'; // newly added line for the bug

it then works fine and gives me the expected output.
# 4  
Old 06-12-2013
I suspect you'll want to do both, actually, since there will be a \n on the end in both places.

This is a common enough need I usually make it a function:

Code:
void chomp(char *str)
{
  int len=strlen(str);

  if(len>0)
  while((len>0) && isspace(str[len-1]))
    str[--len]='\0';
}

then you just
Code:
fgets(buf, size, stdin);
chomp(buf);

This User Gave Thanks to Corona688 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Function - Make your function return an exit status

Hi All, Good Day, seeking for your assistance on how to not perform my 2nd, 3rd,4th etc.. function if my 1st function is in else condition. #Body function1() { if then echo "exist" else echo "not exist" } #if not exist in function1 my all other function will not proceed.... (4 Replies)
Discussion started by: meister29
4 Replies

2. What is on Your Mind?

Small bug in the Quick Editor function in postbit

Hey, There was a small bug in the Quick Editor function in postbit, but I fixed it (basically a double quote was missing from an element id): <div id="post_message_$post" class="neo-message-area">$post</div> Was <div id="post_message_$post class="neo-message-area">$post</div> Should... (1 Reply)
Discussion started by: Neo
1 Replies

3. Shell Programming and Scripting

Will files, creaetd in one function of the same script will be recognized in another function?

Dear All. I have a script, which process files one by one. In the script I have two functions. one sftp files to different server the other from existing file create file with different name. My question is: Will sftp function recognize files names , which are created in another... (1 Reply)
Discussion started by: digioleg54
1 Replies

4. Shell Programming and Scripting

Sort function UNIX bug ???

Hello there i have a funny behiavor of the sort fonction, i try it out on different Solaris machine and i have the same issue. So i would like to see if there is a rationel explanation here is some data in a file:test.txt ,Test,RSD,RSD_Asset ,Test,RSD,RSD_Credit ,Test,RSD,RSD_Liab... (3 Replies)
Discussion started by: kykyboss
3 Replies

5. Shell Programming and Scripting

Bug in Function Call

Can anybody tell me where is the bug in this below mentioned function call. #The String Search File myString="${LOCATION}/config/stringFile.txt" # Functional Usage function usage() { if ; then echo "************************************************************" ... (5 Replies)
Discussion started by: baraghun
5 Replies

6. Shell Programming and Scripting

Return a value from called function to the calling function

I have two scripts. script1.sh looks -------------------------------- #!/bin/bash display() { echo "Welcome to Unix" } display ----------------------------- Script2.sh #!/bin/bash sh script1.sh //simply calling script1.sh ------------------------------ (1 Reply)
Discussion started by: mvictorvijayan
1 Replies

7. Shell Programming and Scripting

bash-function with array acting bizarre, bug?

Hello, basically what this script is supposed to do is showing a list of hosts that is given a number, that you will be able to choose from a list. A check is made to verify that the chosen number is within the array and this is where things go bad and I don't know why, bizarre. I've spent... (5 Replies)
Discussion started by: gand
5 Replies

8. Shell Programming and Scripting

Passing global variable to a function which is called by another function

Hi , I have three funcions f1, f2 and f3 . f1 calls f2 and f2 calls f3 . I have a global variable "period" which i want to pass to f3 . Can i pass the variable directly in the definition of f3 ? Pls help . sars (4 Replies)
Discussion started by: sars
4 Replies

9. Shell Programming and Scripting

Function Bug in script - need help

My script is erroring with: testtapemgr.sh: FTP_RETURNS: not found I cannot see what I am doing wrong..when it calls that function from the Volume returns function and says taht FTP_RETURNS is not found and exits out of the script. What am I not seeing here? #### Return Volume Function ... (4 Replies)
Discussion started by: gzs553
4 Replies
Login or Register to Ask a Question