threads / patch / 18347

patchbuiltin-tag.c: remove global variable to use the callback data of git-config.

Subject: [PATCH] builtin-tag.c: remove global variable to use the callback data of git-config.

## tl;dr

6 messages between Mar 17, 2009 and Mar 18, 2009. Diffs are folded; open one to read it.

replies: 5people: 3as markdown or json

Carlos Rica· Mar 17, 2009, 14:43 UTC · lore

By using strbuf to save the signing-key id, it also imposes no limit to the length of the string obtained from the config or command-line. This string is then passed to gpg to sign the tag, when appropriate.

Signed-off-by: Carlos Rica <jasampler@gmail.com>
---
QUESTION: Is it safe to remove this limit?
 builtin-tag.c |   39 ++++++++++++++++-----------------------
 1 files changed, 16 insertions(+), 23 deletions(-)
Show changes to builtin-tag.c +16 −23
diff --git a/builtin-tag.c b/builtin-tag.c
index 01e7374..ed8b24f 100644
--- a/builtin-tag.c
+++ b/builtin-tag.c
@@ -21,8 +21,6 @@ static const char * const git_tag_usage[] = {
 	NULL
 };
 
-static char signingkey[1000];
-
 struct tag_filter {
 	const char *pattern;
 	int lines;
@@ -156,7 +154,7 @@ static int verify_tag(const char *name, const char *ref,
 	return 0;
 }
 
-static int do_sign(struct strbuf *buffer)
+static int do_sign(struct strbuf *signingkey, struct strbuf *buffer)
 {
 	struct child_process gpg;
 	const char *args[4];
@@ -164,11 +162,10 @@ static int do_sign(struct strbuf *buffer)
 	int len;
 	int i, j;
 
-	if (!*signingkey) {
-		if (strlcpy(signingkey, git_committer_info(IDENT_ERROR_ON_NO_NAME),
-				sizeof(signingkey)) > sizeof(signingkey) - 1)
-			return error("committer info too long.");
-		bracket = strchr(signingkey, '>');
+	if (!signingkey->buf[0]) {
+		strbuf_addstr(signingkey,
+				git_committer_info(IDENT_ERROR_ON_NO_NAME));
+		bracket = strchr(signingkey->buf, '>');
 		if (bracket)
 			bracket[1] = '\0';
 	}
@@ -183,7 +180,7 @@ static int do_sign(struct strbuf *buffer)
 	gpg.out = -1;
 	args[0] = "gpg";
 	args[1] = "-bsau";
-	args[2] = signingkey;
+	args[2] = signingkey->buf;
 	args[3] = NULL;
 
 	if (start_command(&gpg))
@@ -220,18 +217,12 @@ static const char tag_template[] =
 	"# Write a tag message\n"
 	"#\n";
 
-static void set_signingkey(const char *value)
-{
-	if (strlcpy(signingkey, value, sizeof(signingkey)) >= sizeof(signingkey))
-		die("signing key value too long (%.10s...)", value);
-}
-
 static int git_tag_config(const char *var, const char *value, void *cb)
 {
 	if (!strcmp(var, "user.signingkey")) {
 		if (!value)
 			return config_error_nonbool(var);
-		set_signingkey(value);
+		strbuf_addstr((struct strbuf *) cb, value);
 		return 0;
 	}
 
@@ -266,9 +257,10 @@ static void write_tag_body(int fd, const unsigned char *sha1)
 	free(buf);
 }
 
-static int build_tag_object(struct strbuf *buf, int sign, unsigned char *result)
+static int build_tag_object(struct strbuf *buf, int sign,
+			struct strbuf *signingkey, unsigned char *result)
 {
-	if (sign && do_sign(buf) < 0)
+	if (sign && do_sign(signingkey, buf) < 0)
 		return error("unable to sign the tag");
 	if (write_sha1_file(buf->buf, buf->len, tag_type, result) < 0)
 		return error("unable to write tag file");
@@ -277,6 +269,7 @@ static int build_tag_object(struct strbuf *buf, int sign, unsigned char *result)
 
 static void create_tag(const unsigned char *object, const char *tag,
 		       struct strbuf *buf, int message, int sign,
+		       struct strbuf *signingkey,
 		       unsigned char *prev, unsigned char *result)
 {
 	enum object_type type;
@@ -331,7 +324,7 @@ static void create_tag(const unsigned char *object, const char *tag,
 
 	strbuf_insert(buf, 0, header_buf, header_len);
 
-	if (build_tag_object(buf, sign, result) < 0) {
+	if (build_tag_object(buf, sign, signingkey, result) < 0) {
 		if (path)
 			fprintf(stderr, "The tag message has been left in %s\n",
 				path);
@@ -363,7 +356,7 @@ static int parse_msg_arg(const struct option *opt, const char *arg, int unset)
 
 int cmd_tag(int argc, const char **argv, const char *prefix)
 {
-	struct strbuf buf = STRBUF_INIT;
+	struct strbuf buf = STRBUF_INIT, signingkey = STRBUF_INIT;
 	unsigned char object[20], prev[20];
 	char ref[PATH_MAX];
 	const char *object_ref, *tag;
@@ -403,14 +396,14 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
 		OPT_END()
 	};
 
-	git_config(git_tag_config, NULL);
+	git_config(git_tag_config, &signingkey);
 
 	argc = parse_options(argc, argv, options, git_tag_usage, 0);
 	msgfile = parse_options_fix_filename(prefix, msgfile);
 
 	if (keyid) {
 		sign = 1;
-		set_signingkey(keyid);
+		strbuf_addstr(&signingkey, keyid);
 	}
 	if (sign)
 		annotate = 1;
@@ -474,7 +467,7 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
 
 	if (annotate)
 		create_tag(object, tag, &buf, msg.given || msgfile,
-			   sign, prev, object);
+			   sign, &signingkey, prev, object);
 
 	lock = lock_any_ref_for_update(ref, prev, 0);
 	if (!lock)
-- 
1.6.0.5
Johannes Schindelin· Mar 17, 2009, 15:47 UTC · re: Carlos Rica · lore

Re: [PATCH] builtin-tag.c: remove global variable to use the callback data of git-config.

Hi,
On Tue, 17 Mar 2009, Carlos Rica wrote:
Show 9 quoted lines
> By using strbuf to save the signing-key id, it also imposes no limit
> to the length of the string obtained from the config or command-line.
> This string is then passed to gpg to sign the tag, when appropriate.
> 
> Signed-off-by: Carlos Rica <jasampler@gmail.com>
> ---
> 
> 
> QUESTION: Is it safe to remove this limit?
I think so.  GPG should return an error if it thinks it is too large.
Show 10 quoted lines
> @@ -164,11 +162,10 @@ static int do_sign(struct strbuf *buffer)
>  	int len;
>  	int i, j;
>  
> -	if (!*signingkey) {
> -		if (strlcpy(signingkey, git_committer_info(IDENT_ERROR_ON_NO_NAME),
> -				sizeof(signingkey)) > sizeof(signingkey) - 1)
> -			return error("committer info too long.");
> -		bracket = strchr(signingkey, '>');
> +	if (!signingkey->buf[0]) {

It is probably better to ask for !signingkey->len (think of trying to understand the code in 6 months from now).

Other than that, very nice!

Ciao, Dscho

Carlos Rica· Mar 17, 2009, 17:57 UTC · re: Johannes Schindelin · lore

Re: [PATCH] builtin-tag.c: remove global variable to use the callback data of git-config.

On Tue, Mar 17, 2009 at 4:47 PM, Johannes Schindelin <Johannes.Schindelin@gmx.de> wrote:

Show 15 quoted lines
> Hi,
> On Tue, 17 Mar 2009, Carlos Rica wrote:
>> @@ -164,11 +162,10 @@ static int do_sign(struct strbuf *buffer)
>>       int len;
>>       int i, j;
>>
>> -     if (!*signingkey) {
>> -             if (strlcpy(signingkey, git_committer_info(IDENT_ERROR_ON_NO_NAME),
>> -                             sizeof(signingkey)) > sizeof(signingkey) - 1)
>> -                     return error("committer info too long.");
>> -             bracket = strchr(signingkey, '>');
>> +     if (!signingkey->buf[0]) {
>
> It is probably better to ask for !signingkey->len (think of trying to
> understand the code in 6 months from now).

I was in doubt here. By avoiding the use of signingkey->len I was trying to say that you cannot rely in such field if we touch the buffer directly, as it happens below:

   bracket = strchr(signingkey->buf, '>');
   if (bracket)
      bracket[1] = '\0';
Junio C Hamano· Mar 17, 2009, 18:45 UTC · re: Carlos Rica · lore

Re: [PATCH] builtin-tag.c: remove global variable to use the callback data of git-config.

Carlos Rica <jasampler@gmail.com> writes:
Show 25 quoted lines
> On Tue, Mar 17, 2009 at 4:47 PM, Johannes Schindelin
> <Johannes.Schindelin@gmx.de> wrote:
>> Hi,
>> On Tue, 17 Mar 2009, Carlos Rica wrote:
>>> @@ -164,11 +162,10 @@ static int do_sign(struct strbuf *buffer)
>>>       int len;
>>>       int i, j;
>>>
>>> -     if (!*signingkey) {
>>> -             if (strlcpy(signingkey, git_committer_info(IDENT_ERROR_ON_NO_NAME),
>>> -                             sizeof(signingkey)) > sizeof(signingkey) - 1)
>>> -                     return error("committer info too long.");
>>> -             bracket = strchr(signingkey, '>');
>>> +     if (!signingkey->buf[0]) {
>>
>> It is probably better to ask for !signingkey->len (think of trying to
>> understand the code in 6 months from now).
>
> I was in doubt here. By avoiding the use of signingkey->len  I was
> trying to say that you cannot rely in such field if we touch the
> buffer directly, as it happens below:
>
>    bracket = strchr(signingkey->buf, '>');
>    if (bracket)
>       bracket[1] = '\0';
That's a wrong use of strbuf, isn't it?
Johannes Schindelin· Mar 17, 2009, 22:27 UTC · re: Carlos Rica · lore

Re: [PATCH] builtin-tag.c: remove global variable to use the callback data of git-config.

Hi,
On Tue, 17 Mar 2009, Carlos Rica wrote:
Show 25 quoted lines
> On Tue, Mar 17, 2009 at 4:47 PM, Johannes Schindelin
> <Johannes.Schindelin@gmx.de> wrote:
> > Hi,
> > On Tue, 17 Mar 2009, Carlos Rica wrote:
> >> @@ -164,11 +162,10 @@ static int do_sign(struct strbuf *buffer)
> >>       int len;
> >>       int i, j;
> >>
> >> -     if (!*signingkey) {
> >> -             if (strlcpy(signingkey, git_committer_info(IDENT_ERROR_ON_NO_NAME),
> >> -                             sizeof(signingkey)) > sizeof(signingkey) - 1)
> >> -                     return error("committer info too long.");
> >> -             bracket = strchr(signingkey, '>');
> >> +     if (!signingkey->buf[0]) {
> >
> > It is probably better to ask for !signingkey->len (think of trying to
> > understand the code in 6 months from now).
> 
> I was in doubt here. By avoiding the use of signingkey->len  I was
> trying to say that you cannot rely in such field if we touch the
> buffer directly, as it happens below:
> 
>    bracket = strchr(signingkey->buf, '>');
>    if (bracket)
>       bracket[1] = '\0';
Oh, I missed that.  It should read
	if (bracket)
		strbuf_setlen(signingkey, bracket + 1 - signingkey->buf);
instead.

Ciao, Dscho

Carlos Rica· Mar 18, 2009, 00:50 UTC · re: Johannes Schindelin · lore

Re: [PATCH] builtin-tag.c: remove global variable to use the callback data of git-config.

On Tue, Mar 17, 2009 at 11:27 PM, Johannes Schindelin <Johannes.Schindelin@gmx.de> wrote:

Show 36 quoted lines
> Hi,
>
> On Tue, 17 Mar 2009, Carlos Rica wrote:
>
>> On Tue, Mar 17, 2009 at 4:47 PM, Johannes Schindelin
>> <Johannes.Schindelin@gmx.de> wrote:
>> > Hi,
>> > On Tue, 17 Mar 2009, Carlos Rica wrote:
>> >> @@ -164,11 +162,10 @@ static int do_sign(struct strbuf *buffer)
>> >>       int len;
>> >>       int i, j;
>> >>
>> >> -     if (!*signingkey) {
>> >> -             if (strlcpy(signingkey, git_committer_info(IDENT_ERROR_ON_NO_NAME),
>> >> -                             sizeof(signingkey)) > sizeof(signingkey) - 1)
>> >> -                     return error("committer info too long.");
>> >> -             bracket = strchr(signingkey, '>');
>> >> +     if (!signingkey->buf[0]) {
>> >
>> > It is probably better to ask for !signingkey->len (think of trying to
>> > understand the code in 6 months from now).
>>
>> I was in doubt here. By avoiding the use of signingkey->len  I was
>> trying to say that you cannot rely in such field if we touch the
>> buffer directly, as it happens below:
>>
>>    bracket = strchr(signingkey->buf, '>');
>>    if (bracket)
>>       bracket[1] = '\0';
>
> Oh, I missed that.  It should read
>
>        if (bracket)
>                strbuf_setlen(signingkey, bracket + 1 - signingkey->buf);
>
> instead.

I agree! That's much better. Thanks Dscho and Junio. Tell me if you need me to resend the patch with both changes.

← back to recent threads