Skip to contentSkip to conversation

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

PATCH In review Carlos Rica 3 participants 6 messages Jun 15, 2016

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 builtin-tag.c: remove global variable to use the callback data of git-config.

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.

---

QUESTION: Is it safe to remove this limit?

 builtin-tag.c |   39 ++++++++++++++++-----------------------
 1 files changed, 16 insertions(+), 23 deletions(-)
builtin-tag.c +16 -23
index 01e7374..ed8b24f 100644
@@ -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)
Signature · 1 line
1.6.0.5
#1 lore raw

Hi,

Quoted 9 lines · Carlos Rica

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.

---

QUESTION: Is it safe to remove this limit?

I think so. GPG should return an error if it thinks it is too large.

builtin-tag.c static int do_sign(struct strbuf *buffer) · line 165

Quoted 10 lines
builtin-tag.c +1 -5
@@ -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

#2 lore raw
Quoted 6 lines · Johannes Schindelin

Hi,

Quoted 4 lines · Carlos Rica
builtin-tag.c +0 -0
@@ -164,11 +162,10 @@ static int do_sign(struct strbuf *buffer)
      int len;
      int i, j;
 
Quoted 9 lines

builtin-tag.c static int do_sign(struct strbuf *buffer) · line 165

Quoted 6 lines
builtin-tag.c +1 -5
-     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';
#3 lore raw
Quoted 18 lines · Carlos Rica
Quoted 6 lines · Johannes Schindelin

Hi,

Quoted 4 lines · Carlos Rica
builtin-tag.c +0 -0
@@ -164,11 +162,10 @@ static int do_sign(struct strbuf *buffer)
      int len;
      int i, j;
 
Quoted 9 lines

builtin-tag.c static int do_sign(struct strbuf *buffer) · line 165

Quoted 6 lines
builtin-tag.c +1 -5
-     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).

Quoted 7 lines

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?

#4 lore raw
In reply to Carlos Rica

Hi,

Quoted 18 lines · Carlos Rica
Quoted 6 lines · Johannes Schindelin

Hi,

Quoted 4 lines · Carlos Rica
builtin-tag.c +0 -0
@@ -164,11 +162,10 @@ static int do_sign(struct strbuf *buffer)
      int len;
      int i, j;
 
Quoted 9 lines

builtin-tag.c static int do_sign(struct strbuf *buffer) · line 165

Quoted 6 lines
builtin-tag.c +1 -5
-     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).

Quoted 7 lines

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

#5 lore raw
Quoted 30 lines · Johannes Schindelin

Hi,

Quoted 18 lines · Carlos Rica
Quoted 6 lines · Johannes Schindelin

Hi,

Quoted 4 lines · Carlos Rica
builtin-tag.c +0 -0
@@ -164,11 +162,10 @@ static int do_sign(struct strbuf *buffer)
      int len;
      int i, j;
 
Quoted 9 lines

builtin-tag.c static int do_sign(struct strbuf *buffer) · line 165

Quoted 6 lines
builtin-tag.c +1 -5
-     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).

Quoted 7 lines

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';
Quoted 6 lines

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.