Skip to content

Commit f0ca00e

Browse files
committed
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.
1 parent 7776db5 commit f0ca00e

File tree

1 file changed

+19
-5
lines changed

1 file changed

+19
-5
lines changed

examples/network/common.c

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#include "common.h"
22
#include <stdio.h>
3+
#include <string.h>
4+
#include <errno.h>
35

46
/* Shamelessly borrowed from http://stackoverflow.com/questions/3417837/
57
* with permission of the original author, Martin Pool.
@@ -20,15 +22,27 @@ int cred_acquire_cb(git_cred **out,
2022
unsigned int UNUSED(allowed_types),
2123
void * UNUSED(payload))
2224
{
23-
char username[128] = {0};
24-
char password[128] = {0};
25+
char *username = NULL, *password = NULL;
26+
int error;
2527

2628
printf("Username: ");
27-
scanf("%s", username);
29+
if (getline(&username, NULL, stdin) < 0) {
30+
fprintf(stderr, "Unable to read username: %s", strerror(errno));
31+
return -1;
32+
}
2833

2934
/* Yup. Right there on your terminal. Careful where you copy/paste output. */
3035
printf("Password: ");
31-
scanf("%s", password);
36+
if (getline(&password, NULL, stdin) < 0) {
37+
fprintf(stderr, "Unable to read password: %s", strerror(errno));
38+
free(username);
39+
return -1;
40+
}
3241

33-
return git_cred_userpass_plaintext_new(out, username, password);
42+
error = git_cred_userpass_plaintext_new(out, username, password);
43+
44+
free(username);
45+
free(password);
46+
47+
return error;
3448
}

0 commit comments

Comments
 (0)