Source-Changes-HG archive

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

[src/trunk]: src/lib/libc/gen - don't update the length of the buffer until t...



details:   https://anonhg.NetBSD.org/src/rev/c7393360fd7b
branches:  trunk
changeset: 778782:c7393360fd7b
user:      christos <christos%NetBSD.org@localhost>
date:      Fri Apr 13 14:16:27 2012 +0000

description:
- don't update the length of the buffer until the allocation succeeds
- print only printable characters otherwise print '?'

diffstat:

 lib/libc/gen/getpass.c |  20 +++++++++++---------
 1 files changed, 11 insertions(+), 9 deletions(-)

diffs (63 lines):

diff -r 4f7bda15bcfe -r c7393360fd7b lib/libc/gen/getpass.c
--- a/lib/libc/gen/getpass.c    Fri Apr 13 13:36:57 2012 +0000
+++ b/lib/libc/gen/getpass.c    Fri Apr 13 14:16:27 2012 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: getpass.c,v 1.21 2012/04/13 02:20:50 christos Exp $    */
+/*     $NetBSD: getpass.c,v 1.22 2012/04/13 14:16:27 christos Exp $    */
 
 /*-
  * Copyright (c) 2012 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 #include <sys/cdefs.h>
 #if defined(LIBC_SCCS) && !defined(lint)
-__RCSID("$NetBSD: getpass.c,v 1.21 2012/04/13 02:20:50 christos Exp $");
+__RCSID("$NetBSD: getpass.c,v 1.22 2012/04/13 14:16:27 christos Exp $");
 #endif /* LIBC_SCCS and not lint */
 
 #include "namespace.h"
@@ -40,6 +40,7 @@
 #include <stdio.h>
 #endif
 #include <errno.h>
+#include <ctype.h>
 #include <signal.h>
 #include <string.h>
 #include <paths.h>
@@ -139,7 +140,7 @@
                }
 
                /* Ignored */
-               if (c == C(VREPRINT, CTRL('r')) || c == C(VSTART, CTRL('q')) || 
+               if (c == C(VREPRINT, CTRL('r')) || c == C(VSTART, CTRL('q')) ||
                    c == C(VSTOP, CTRL('s')) || c == C(VSTATUS, CTRL('t')) || 
                    c == C(VDISCARD, CTRL('o')))
                        continue;
@@ -203,12 +204,12 @@
 add:
                if (l >= len) {
                        if (allocated) {
-                               char *b;
-                               len += 1024;
-                               b = realloc(buf, len);
-                               if (b == NULL)
+                               size_t nlen = len + 1024;
+                               char *nbuf = realloc(buf, nlen);
+                               if (nbuf == NULL)
                                        goto restore;
-                               buf = b;
+                               buf = nbuf;
+                               len = nlen;
                        } else {
                                if (flags & GETPASS_BUF_LIMIT) {
                                        beep();
@@ -225,7 +226,8 @@
                        if (flags & GETPASS_ECHO_STAR)
                                (void)write(fd[1], "*", 1);
                        else if (flags & GETPASS_ECHO)
-                               (void)write(fd[1], &c, 1);
+                               (void)write(fd[1], isprint((unsigned char)c) ?
+                                   &c : "?", 1);
                }
        }
 



Home | Main Index | Thread Index | Old Index