Commit f0ca00e0 by Patrick Steinhardt

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.
parent 7776db51
#include "common.h" #include "common.h"
#include <stdio.h> #include <stdio.h>
#include <string.h>
#include <errno.h>
/* Shamelessly borrowed from http://stackoverflow.com/questions/3417837/ /* Shamelessly borrowed from http://stackoverflow.com/questions/3417837/
* with permission of the original author, Martin Pool. * with permission of the original author, Martin Pool.
...@@ -20,15 +22,27 @@ int cred_acquire_cb(git_cred **out, ...@@ -20,15 +22,27 @@ int cred_acquire_cb(git_cred **out,
unsigned int UNUSED(allowed_types), unsigned int UNUSED(allowed_types),
void * UNUSED(payload)) void * UNUSED(payload))
{ {
char username[128] = {0}; char *username = NULL, *password = NULL;
char password[128] = {0}; int error;
printf("Username: "); 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. */ /* Yup. Right there on your terminal. Careful where you copy/paste output. */
printf("Password: "); 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;
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment