Sponsored Content
Top Forums Programming Tweaked getpass() function gives an untraceable bug Post 302820053 by royalibrahim on Wednesday 12th of June 2013 02:45:50 AM
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.
 

9 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

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

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

7. 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

8. 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

9. 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
getpass(3C)                                                Standard C Library Functions                                                getpass(3C)

NAME
getpass, getpassphrase - read a string of characters without echo SYNOPSIS
#include <stdlib.h> char *getpass(const char *prompt); char *getpassphrase(const char *prompt); XPG4, SUS, SUSv2 #include <unistd.h> char *getpass(const char *prompt); DESCRIPTION
The getpass() function opens the process's controlling terminal, writes to that device the null-terminated string prompt, disables echoing, reads a string of characters up to the next newline character or EOF, restores the terminal state and closes the terminal. The getpassphrase() function is identical to getpass(), except that it reads and returns a string of up to 257 characters in length. RETURN VALUES
Upon successful completion, getpass() returns a pointer to a null-terminated string of at most 9 bytes that were read from the terminal device. If an error is encountered, the terminal state is restored and a null pointer is returned. ERRORS
The getpass() and getpassphrase() functions may fail if: EINTR The function was interrupted by a signal. EIO The process is a member of a background process attempting to read from its controlling terminal, the process is ignoring or blocking the SIGTTIN signal or the process group is orphaned. EMFILE OPEN_MAX file descriptors are currently open in the calling process. ENFILE The maximum allowable number of files is currently open in the system. ENXIO The process does not have a controlling terminal. USAGE
The return value points to static data whose content may be overwritten by each call. ATTRIBUTES
See attributes(5) for descriptions of the following attributes: +-----------------------------+-----------------------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | +-----------------------------+-----------------------------+ |Interface Stability |getpass() is Standard. | +-----------------------------+-----------------------------+ |MT-Level |Unsafe | +-----------------------------+-----------------------------+ SEE ALSO
attributes(5), standards(5) SunOS 5.10 2 Sep 2003 getpass(3C)
All times are GMT -4. The time now is 08:30 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy