Skip to content

Commit

Permalink
lib/agetpass.*: Move allocation to helper macro
Browse files Browse the repository at this point in the history
This is in preparation for the following commit.

Signed-off-by: Alejandro Colomar <[email protected]>
  • Loading branch information
alejandro-colomar committed Jan 19, 2025
1 parent 5a7b086 commit f7f02f7
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
3 changes: 2 additions & 1 deletion lib/agetpass.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@


extern inline void erase_pass(char *pass);
extern inline char *agetpass_internal(const char *prompt, int flags);
extern inline char *getpass_(char pass[PASS_MAX + 2], const char *prompt,
int flags);
20 changes: 11 additions & 9 deletions lib/agetpass.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@

#include <config.h>

#include <errno.h>
#include <limits.h>
#include <readpassphrase.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>

Expand All @@ -21,21 +23,25 @@


// Similar to getpass(3), but free of its problems.
#define agetpass(prompt) agetpass_internal(prompt, RPP_REQUIRE_TTY)
#define agetpass_stdin() agetpass_internal(NULL, RPP_STDIN)
#define agetpass(prompt) agetpass_(prompt, RPP_REQUIRE_TTY)
#define agetpass_stdin() agetpass_(NULL, RPP_STDIN)

#define agetpass_(...) getpass_(MALLOC(PASS_MAX + 2, char), __VA_ARGS__)


inline void erase_pass(char *pass);
ATTR_MALLOC(erase_pass)
inline char *agetpass_internal(const char *prompt, int flags);
inline char *getpass_(char pass[PASS_MAX + 2], const char *prompt, int flags);


inline char *
agetpass_internal(const char *prompt, int flags)
getpass_(char pass[PASS_MAX + 2], const char *prompt, int flags)
{
char *pass;
size_t len;

if (pass == NULL)
return NULL;

/*
* Since we want to support passwords upto PASS_MAX, we need
* PASS_MAX bytes for the password itself, and one more byte for
Expand All @@ -44,10 +50,6 @@ agetpass_internal(const char *prompt, int flags)
* Let's add one more byte, and if the password uses it, it
* means the introduced password was longer than PASS_MAX.
*/
pass = MALLOC(PASS_MAX + 2, char);
if (pass == NULL)
return NULL;

if (readpassphrase(prompt, pass, PASS_MAX + 2, flags) == NULL)
goto fail;

Expand Down

0 comments on commit f7f02f7

Please sign in to comment.