All of lore.kernel.org
 help / color / mirror / Atom feed
From: Erik Faye-Lund <kusmabite@gmail.com>
To: Junio C Hamano <gitster@pobox.com>
Cc: Rafael Gieschke <rafael@gieschke.de>,
	Git Mailing List <git@vger.kernel.org>,
	Johannes Schindelin <Johannes.Schindelin@gmx.de>
Subject: Re: [PATCH] compat: add a getpass() compatibility function
Date: Thu, 19 May 2011 20:07:44 +0200	[thread overview]
Message-ID: <BANLkTimDW8W13Wm8i+n0ww9jCeHsXc__iA@mail.gmail.com> (raw)
In-Reply-To: <7v62p68ut0.fsf@alter.siamese.dyndns.org>

On Thu, May 19, 2011 at 7:27 PM, Junio C Hamano <gitster@pobox.com> wrote:
> Rafael Gieschke <rafael@gieschke.de> writes:
>
>>> Windows doesn't have /dev/tty, but the logic in this version handles
>>> that by using stdin/stderr instead. The signal-stuff has a comment
>>> that indicates it might not even be correct. tcgetattr/tcsetattr isn't
>>> supported on Windows, but it's not needed if we use getch (as the
>>> version in compat/mingw.c does). POSIX/curses getch respects the
>>> echo-setting, while Windows getch never echo.
>
> Probably a properly abstracted common version would look like a function
> that calls four platform-dependent helper funcions in this order:
>
>        0. prompt
>        1. start "noecho" mode
>        2. get whole line
>        3. exit "noecho" mode
>
> where Windows may use stderr for 0, have noop() implementation for 1 and
> 3, use _getch() that does not echo for 2, while POSIX may write to
> /dev/tty for 0, use tc[gs]etattr() with perhaps some signal settings
> sprinkled in for 1 and 3.
>
> So I don't see a need for Windows to emulate tc[g]setattr nor curses in
> order to get a generic getpass() abstraction between two platforms.
>

When I think about it a bit more, it feels a bit pointless:
0. is identical (fputs)
1. is different (tc[gs]etattr vs nop)
2. is different (getc vs _getch)
3. is different (tcsetattr vs nop)

So there's probably not much code to share here. There's a bit of
logic, but I'm not entirely sure this should be the same either,
because on Windows we have to take care of '\r' (since we open stdin
in binary mode at start-up).

But looking at the implementations, there's one thing that strike me:
both the netbsd and uclibc implementations fill a static buffer, while
our windows-version returns an allocated buffer. Reading POSIX, it's
not entirely clear if the returned pointer should be free'd or not:
http://pubs.opengroup.org/onlinepubs/007908799/xsh/getpass.html

But there is a hint: the limit of PASS_MAX bytes indicate that the
buffer is expected to be statically allocated. So perhaps we should do
this to prevent a leak?

diff --git a/compat/mingw.c b/compat/mingw.c
index 878b1de..dba3e64 100644
--- a/compat/mingw.c
+++ b/compat/mingw.c
@@ -1668,17 +1668,19 @@ int link(const char *oldpath, const char *newpath)

 char *getpass(const char *prompt)
 {
-	struct strbuf buf = STRBUF_INIT;
+	static char buf[PASS_MAX];
+	int i;

 	fputs(prompt, stderr);
-	for (;;) {
+	for (i = 0; i < PASS_MAX - 1; ++i) {
 		char c = _getch();
 		if (c == '\r' || c == '\n')
 			break;
-		strbuf_addch(&buf, c);
+		buf[i] = c;
 	}
+	buf[i] = '\0';
 	fputs("\n", stderr);
-	return strbuf_detach(&buf, NULL);
+	return buf;
 }

 pid_t waitpid(pid_t pid, int *status, unsigned options)
diff --git a/compat/mingw.h b/compat/mingw.h
index 62eccd3..e37d557 100644
--- a/compat/mingw.h
+++ b/compat/mingw.h
@@ -48,6 +48,8 @@ typedef int socklen_t;
 #define EAFNOSUPPORT WSAEAFNOSUPPORT
 #define ECONNABORTED WSAECONNABORTED

+#define PASS_MAX 512
+
 struct passwd {
 	char *pw_name;
 	char *pw_gecos;

  reply	other threads:[~2011-05-19 18:16 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-05-19 11:37 [PATCH] compat: add a getpass() compatibility function Rafael Gieschke
2011-05-19 12:17 ` Erik Faye-Lund
2011-05-19 16:30   ` Junio C Hamano
2011-05-19 17:01   ` Rafael Gieschke
2011-05-19 17:27     ` Junio C Hamano
2011-05-19 18:07       ` Erik Faye-Lund [this message]
2011-05-19 19:16         ` Rafael Gieschke
2011-05-19 19:19           ` Erik Faye-Lund
2011-05-19 19:42             ` Rafael Gieschke
2011-05-19 20:12               ` Erik Faye-Lund
2011-05-19 21:16                 ` Erik Faye-Lund
2011-05-20 10:06                   ` Rafael Gieschke
2011-05-20 10:48                     ` Erik Faye-Lund
2011-05-20 17:18                       ` Junio C Hamano
2011-05-20 17:26                         ` Erik Faye-Lund
2011-05-19 12:21 ` Erik Faye-Lund
2011-05-19 15:14   ` Jonathan Nieder
2011-05-19 15:29     ` Erik Faye-Lund
2011-05-19 17:17     ` Junio C Hamano

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=BANLkTimDW8W13Wm8i+n0ww9jCeHsXc__iA@mail.gmail.com \
    --to=kusmabite@gmail.com \
    --cc=Johannes.Schindelin@gmx.de \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=rafael@gieschke.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.