terminal: seek when switching between reading and writing
authorJeff King <peff@peff.net>
Tue, 7 Aug 2012 04:10:26 +0000 (00:10 -0400)
committerJunio C Hamano <gitster@pobox.com>
Tue, 7 Aug 2012 05:11:47 +0000 (22:11 -0700)
When a stdio stream is opened in update mode (e.g., "w+"),
the C standard forbids switching between reading or writing
without an intervening positioning function. Many
implementations are lenient about this, but Solaris libc
will flush the recently-read contents to the output buffer.
In this instance, that meant writing the non-echoed password
that the user just typed to the terminal.

Fix it by inserting a no-op fseek between the read and
write.

The opposite direction (writing followed by reading) is also
disallowed, but our intervening fflush is an acceptable
positioning function for that alternative.

Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
compat/terminal.c
index 6d16c8fba0b305a2e1020d9e1c7f1d1ad64d302f..bbb038dd0103034b85b0e9d407dfa91e607c1eeb 100644 (file)
@@ -59,6 +59,7 @@ char *git_terminal_prompt(const char *prompt, int echo)
 
        r = strbuf_getline(&buf, fh, '\n');
        if (!echo) {
+               fseek(fh, SEEK_CUR, 0);
                putc('\n', fh);
                fflush(fh);
        }