From f0ca00e013885479228c4f076989566a2b77221b Mon Sep 17 00:00:00 2001 From: Patrick Steinhardt Date: Wed, 3 May 2017 12:25:48 +0200 Subject: [PATCH] examples: network: refactor credentials callback The credentials callback reads the username and password via scanf into fixed-length arrays. While these are simply examples and as such not as interesting, the unchecked return value of scanf causes GCC to emit warnings. So while we're busy to shut up GCC, we also fix the possible overflow of scanf by using getline instead. --- examples/network/common.c | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/examples/network/common.c b/examples/network/common.c index d123eedbd..1a81a10f8 100644 --- a/examples/network/common.c +++ b/examples/network/common.c @@ -1,5 +1,7 @@ #include "common.h" #include +#include +#include /* Shamelessly borrowed from http://stackoverflow.com/questions/3417837/ * with permission of the original author, Martin Pool. @@ -20,15 +22,27 @@ int cred_acquire_cb(git_cred **out, unsigned int UNUSED(allowed_types), void * UNUSED(payload)) { - char username[128] = {0}; - char password[128] = {0}; + char *username = NULL, *password = NULL; + int error; printf("Username: "); - scanf("%s", username); + if (getline(&username, NULL, stdin) < 0) { + fprintf(stderr, "Unable to read username: %s", strerror(errno)); + return -1; + } /* Yup. Right there on your terminal. Careful where you copy/paste output. */ printf("Password: "); - scanf("%s", password); + if (getline(&password, NULL, stdin) < 0) { + fprintf(stderr, "Unable to read password: %s", strerror(errno)); + free(username); + return -1; + } - return git_cred_userpass_plaintext_new(out, username, password); + error = git_cred_userpass_plaintext_new(out, username, password); + + free(username); + free(password); + + return error; }