qemu-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Qemu-devel] [Qemu-trivial] [PATCH-trivial] qemu-img: Check getchar(


From: Michael Tokarev
Subject: Re: [Qemu-devel] [Qemu-trivial] [PATCH-trivial] qemu-img: Check getchar() return value in read_password() for WIN32
Date: Sat, 02 Aug 2014 17:35:43 +0400
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20100101 Icedove/24.6.0

06.07.2014 12:43, Chen Gang wrote:
> 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.

Applied to -trivial queue, with a slight modification:

> --- a/qemu-img.c
> +++ b/qemu-img.c
> @@ -185,15 +185,21 @@ 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))
> +        }
> +        if (i < (buf_size - 1)) {

I've added an 'else' there and merged with the previous line,
to match with the above code which is being added.

>              buf[i++] = c;
> +        }
>      }
>      buf[i] = '\0';
>      return 0;

Thanks,

/mjt




reply via email to

[Prev in Thread] Current Thread [Next in Thread]