Skip to contentSkip to conversation

qemu-img: Check getchar() return value in read_password() for WIN32

Qemu-devel Applied Chen Gang 2 participants 3 messages Jul 6, 2014 – Aug 3, 2014

lore
Share & tools
lore

Chen Gang

author
#0 lore raw

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.

---
 qemu-img.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
qemu-img.c +8 -2
index c98896b..8423e8e 100644
@@ -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)) {
             buf[i++] = c;
+        }
     }
     buf[i] = '\0';
     return 0;
Signature · 1 line
1.7.11.7
#1 lore raw
Quoted 2 lines · Chen Gang

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:

Quoted 11 lines
qemu-img.c +1 -0
@@ -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(;;) {

qemu-img.c static int GCC_FMT_ATTR(2, 3) qprintf(bool quiet, const char *fmt, ...) · line 200

Quoted 10 lines
qemu-img.c +6 -2
         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.

qemu-img.c static int GCC_FMT_ATTR(2, 3) qprintf(bool quiet, const char *fmt, ...) · line 205

Quoted 5 lines
qemu-img.c +1 -0
             buf[i++] = c;
+        }
     }
     buf[i] = '\0';
     return 0;

Thanks,

/mjt

author
#2 lore raw
Quoted 6 lines · Michael Tokarev
Quoted 2 lines · Chen Gang

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:

Thanks, and I shall continue to make another patches for qemu within this month.

Quoted 16 lines
Quoted 16 lines
qemu-img.c +4 -1
@@ -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;
Quoted 9 lines

qemu-img.c static int GCC_FMT_ATTR(2, 3) qprintf(bool quiet, const char *fmt, ...) · line 200

Quoted 5 lines
qemu-img.c +3 -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.

OK, thanks.

Quoted 10 lines

qemu-img.c static int GCC_FMT_ATTR(2, 3) qprintf(bool quiet, const char *fmt, ...) · line 205

Quoted 5 lines
qemu-img.c +1 -0
             buf[i++] = c;
+        }
     }
     buf[i] = '\0';
     return 0;

Thanks,

/mjt

Thanks.

Signature · 3 lines
Chen Gang

Open, share, and attitude like air, water, and life which God blessed