Skip to content

Commit

Permalink
qemu-img: Check getchar() return value in read_password() for WIN32
Browse files Browse the repository at this point in the history
getchar() is a standard c library function which may return with failure
(e.g. -1), so like another platforms, also need check it under WIN32.

And make the related code match current qemu code styles, too.

Signed-off-by: Chen Gang <[email protected]>
Signed-off-by: Michael Tokarev <[email protected]>
  • Loading branch information
Chen-Gang authored and Michael Tokarev committed Aug 8, 2014
1 parent f13bef9 commit fdcf6e6
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions qemu-img.c
Original file line number Diff line number Diff line change
Expand Up @@ -185,15 +185,20 @@ static int GCC_FMT_ATTR(2, 3) qprintf(bool quiet, const char *fmt, ...)
static int read_password(char *buf, int buf_size)
{
int c, i;

printf("Password: ");
fflush(stdout);
i = 0;
for(;;) {
c = getchar();
if (c == '\n')
if (c < 0) {
buf[i] = '\0';
return -1;
} else if (c == '\n') {
break;
if (i < (buf_size - 1))
} else if (i < (buf_size - 1)) {
buf[i++] = c;
}
}
buf[i] = '\0';
return 0;
Expand Down

0 comments on commit fdcf6e6

Please sign in to comment.