Skip to content

Commit 213c6e4

Browse files
lib/agetpass.*: Move allocation to helper macro
This is in preparation for the following commit. Signed-off-by: Alejandro Colomar <[email protected]>
1 parent d9a9ed1 commit 213c6e4

File tree

2 files changed

+13
-10
lines changed

2 files changed

+13
-10
lines changed

lib/agetpass.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@
88

99

1010
extern inline void erase_pass(char *pass);
11-
extern inline char *agetpass_internal(const char *prompt, int flags);
11+
extern inline char *getpass_(char pass[PASS_MAX + 2], const char *prompt,
12+
int flags);

lib/agetpass.h

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@
88

99
#include <config.h>
1010

11+
#include <errno.h>
1112
#include <limits.h>
1213
#include <readpassphrase.h>
14+
#include <stddef.h>
1315
#include <stdlib.h>
1416
#include <string.h>
1517

@@ -22,21 +24,25 @@
2224

2325

2426
// Similar to getpass(3), but free of its problems.
25-
#define agetpass(prompt) agetpass_internal(prompt, RPP_REQUIRE_TTY)
26-
#define agetpass_stdin() agetpass_internal(NULL, RPP_STDIN)
27+
#define agetpass(prompt) agetpass_(prompt, RPP_REQUIRE_TTY)
28+
#define agetpass_stdin() agetpass_(NULL, RPP_STDIN)
29+
30+
#define agetpass_(...) getpass_(MALLOC(PASS_MAX + 2, char), __VA_ARGS__)
2731

2832

2933
inline void erase_pass(char *pass);
3034
ATTR_MALLOC(erase_pass)
31-
inline char *agetpass_internal(const char *prompt, int flags);
35+
inline char *getpass_(char pass[PASS_MAX + 2], const char *prompt, int flags);
3236

3337

3438
inline char *
35-
agetpass_internal(const char *prompt, int flags)
39+
getpass_(char pass[PASS_MAX + 2], const char *prompt, int flags)
3640
{
37-
char *pass;
3841
size_t len;
3942

43+
if (pass == NULL)
44+
return NULL;
45+
4046
/*
4147
* Since we want to support passwords upto PASS_MAX, we need
4248
* PASS_MAX bytes for the password itself, and one more byte for
@@ -45,10 +51,6 @@ agetpass_internal(const char *prompt, int flags)
4551
* Let's add one more byte, and if the password uses it, it
4652
* means the introduced password was longer than PASS_MAX.
4753
*/
48-
pass = MALLOC(PASS_MAX + 2, char);
49-
if (pass == NULL)
50-
return NULL;
51-
5254
if (readpassphrase(prompt, pass, PASS_MAX + 2, flags) == NULL)
5355
goto fail;
5456

0 commit comments

Comments
 (0)