{"thread":{"id":"37847","subject":"[PATCH] config: add show_err flag to git_config_parse_key()","startedAt":"2014-10-30T17:48:05Z","lastAt":"2014-10-30T18:25:42Z","messageCount":3,"participants":["Tanay Abhra","Junio C Hamano"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"251224","messageId":"CAEc54XBOvqT234s0UX-osYbOJfyuPquWLtmzDNDhAQoJ8b9eXg@mail.gmail.com","threadId":"37847","inReplyTo":null,"subject":"[PATCH] config: add show_err flag to git_config_parse_key()","fromName":"Tanay Abhra","fromEmail":"tanayabh@gmail.com","sentAt":"2014-10-30T17:48:05Z","receivedAt":"2014-10-30T17:48:05Z","isPatch":true,"sender":{"key":"tanayabh@gmail.com","avatar":"https://avatars.githubusercontent.com/u/2097229?v=4"},"body":"`git_config_parse_key()` is used to sanitize the input key.\nSome callers of the function like `git_config_set_multivar_in_file()`\nget the pre-sanitized key directly from the user so it becomes\nnecessary to raise an error specifying what went wrong when the entered\nkey is defective.\n\nOther callers like `configset_find_element()` get their keys from\nthe git itself so a return value signifying error would be enough.\nThe error output shown to the user is useless and confusing in this\ncase so add a show_err flag to suppress errors in such cases.\n\nSigned-off-by: Tanay Abhra <tanayabh@gmail.com>\n---\n\nHi,\n\nYou were right, one of the functions was calling git_config_parse_key()\nwhich was leaking errors to the console. git_config_parse_key() was\nmeant for sanitizing user provided keys only but it was being used\ninternally in a place where only a return value would be enough.\n\nThanks for bringing this to our attention.\n\nCheers,\nTanay Abhra.\n\n builtin/config.c |  2 +-\n cache.h          |  2 +-\n config.c         | 19 ++++++++++++-------\n 3 files changed, 14 insertions(+), 9 deletions(-)\n\ndiff --git a/builtin/config.c b/builtin/config.c\nindex 8cc2604..51635dc 100644\n--- a/builtin/config.c\n+++ b/builtin/config.c\n@@ -200,7 +200,7 @@ static int get_value(const char *key_, const char *regex_)\n             goto free_strings;\n         }\n     } else {\n-        if (git_config_parse_key(key_, &key, NULL)) {\n+        if (git_config_parse_key(key_, &key, NULL, 1)) {\n             ret = CONFIG_INVALID_KEY;\n             goto free_strings;\n         }\ndiff --git a/cache.h b/cache.h\nindex 99ed096..8129590 100644\n--- a/cache.h\n+++ b/cache.h\n@@ -1362,7 +1362,7 @@ extern int git_config_string(const char **,\nconst char *, const char *);\n extern int git_config_pathname(const char **, const char *, const char *);\n extern int git_config_set_in_file(const char *, const char *, const char *);\n extern int git_config_set(const char *, const char *);\n-extern int git_config_parse_key(const char *, char **, int *);\n+extern int git_config_parse_key(const char *, char **, int *, int);\n extern int git_config_set_multivar(const char *, const char *, const\nchar *, int);\n extern int git_config_set_multivar_in_file(const char *, const char\n*, const char *, const char *, int);\n extern int git_config_rename_section(const char *, const char *);\ndiff --git a/config.c b/config.c\nindex 15a2983..eb9058c 100644\n--- a/config.c\n+++ b/config.c\n@@ -1299,7 +1299,7 @@ static struct config_set_element\n*configset_find_element(struct config_set *cs,\n      * `key` may come from the user, so normalize it before using it\n      * for querying entries from the hashmap.\n      */\n-    ret = git_config_parse_key(key, &normalized_key, NULL);\n+    ret = git_config_parse_key(key, &normalized_key, NULL, 0);\n\n     if (ret)\n         return NULL;\n@@ -1832,8 +1832,9 @@ int git_config_set(const char *key, const char *value)\n  *             lowercase section and variable name\n  * baselen - pointer to int which will hold the length of the\n  *           section + subsection part, can be NULL\n+ * show_err - toggle whether the function raises an error on a defective key\n  */\n-int git_config_parse_key(const char *key, char **store_key, int *baselen_)\n+int git_config_parse_key(const char *key, char **store_key, int\n*baselen_, int show_err)\n {\n     int i, dot, baselen;\n     const char *last_dot = strrchr(key, '.');\n@@ -1844,12 +1845,14 @@ int git_config_parse_key(const char *key, char\n**store_key, int *baselen_)\n      */\n\n     if (last_dot == NULL || last_dot == key) {\n-        error(\"key does not contain a section: %s\", key);\n+        if (show_err)\n+            error(\"key does not contain a section: %s\", key);\n         return -CONFIG_NO_SECTION_OR_NAME;\n     }\n\n     if (!last_dot[1]) {\n-        error(\"key does not contain variable name: %s\", key);\n+        if (show_err)\n+            error(\"key does not contain variable name: %s\", key);\n         return -CONFIG_NO_SECTION_OR_NAME;\n     }\n\n@@ -1871,12 +1874,14 @@ int git_config_parse_key(const char *key, char\n**store_key, int *baselen_)\n         if (!dot || i > baselen) {\n             if (!iskeychar(c) ||\n                 (i == baselen + 1 && !isalpha(c))) {\n-                error(\"invalid key: %s\", key);\n+                if (show_err)\n+                    error(\"invalid key: %s\", key);\n                 goto out_free_ret_1;\n             }\n             c = tolower(c);\n         } else if (c == '\\n') {\n-            error(\"invalid key (newline): %s\", key);\n+            if (show_err)\n+                error(\"invalid key (newline): %s\", key);\n             goto out_free_ret_1;\n         }\n         (*store_key)[i] = c;\n@@ -1926,7 +1931,7 @@ int git_config_set_multivar_in_file(const char\n*config_filename,\n     char *filename_buf = NULL;\n\n     /* parse-key returns negative; flip the sign to feed exit(3) */\n-    ret = 0 - git_config_parse_key(key, &store.key, &store.baselen);\n+    ret = 0 - git_config_parse_key(key, &store.key, &store.baselen, 1);\n     if (ret)\n         goto out_free;\n\n-- \n1.9.0.GIT\n"},{"id":"251229","messageId":"xmqqd299wh9m.fsf@gitster.dls.corp.google.com","threadId":"37847","inReplyTo":"CAEc54XBOvqT234s0UX-osYbOJfyuPquWLtmzDNDhAQoJ8b9eXg@mail.gmail.com","subject":"Re: [PATCH] config: add show_err flag to git_config_parse_key()","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2014-10-30T18:21:09Z","receivedAt":"2014-10-30T18:21:09Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Tanay Abhra <tanayabh@gmail.com> writes:\n\n> `git_config_parse_key()` is used to sanitize the input key.\n> Some callers of the function like `git_config_set_multivar_in_file()`\n> get the pre-sanitized key directly from the user so it becomes\n> necessary to raise an error specifying what went wrong when the entered\n> key is defective.\n>\n> Other callers like `configset_find_element()` get their keys from\n> the git itself so a return value signifying error would be enough.\n> The error output shown to the user is useless and confusing in this\n> case so add a show_err flag to suppress errors in such cases.\n>\n> Signed-off-by: Tanay Abhra <tanayabh@gmail.com>\n> ---\n>\n> Hi,\n>\n> You were right, one of the functions was calling git_config_parse_key()\n> which was leaking errors to the console. git_config_parse_key() was\n> meant for sanitizing user provided keys only but it was being used\n> internally in a place where only a return value would be enough.\n>\n> Thanks for bringing this to our attention.\n>\n> Cheers,\n> Tanay Abhra.\n\nWho are *you* in the above, and what was the bug report about (if it\nwas a bug report)?  Perhaps summarize it in a form of a few new tests\nin t/t13XX series is in order?\n\nThanks.\n\n>\n>  builtin/config.c |  2 +-\n>  cache.h          |  2 +-\n>  config.c         | 19 ++++++++++++-------\n>  3 files changed, 14 insertions(+), 9 deletions(-)\n>\n> diff --git a/builtin/config.c b/builtin/config.c\n> index 8cc2604..51635dc 100644\n> --- a/builtin/config.c\n> +++ b/builtin/config.c\n> @@ -200,7 +200,7 @@ static int get_value(const char *key_, const char *regex_)\n>              goto free_strings;\n>          }\n>      } else {\n> -        if (git_config_parse_key(key_, &key, NULL)) {\n> +        if (git_config_parse_key(key_, &key, NULL, 1)) {\n>              ret = CONFIG_INVALID_KEY;\n>              goto free_strings;\n>          }\n> diff --git a/cache.h b/cache.h\n> index 99ed096..8129590 100644\n> --- a/cache.h\n> +++ b/cache.h\n> @@ -1362,7 +1362,7 @@ extern int git_config_string(const char **,\n> const char *, const char *);\n>  extern int git_config_pathname(const char **, const char *, const char *);\n>  extern int git_config_set_in_file(const char *, const char *, const char *);\n>  extern int git_config_set(const char *, const char *);\n> -extern int git_config_parse_key(const char *, char **, int *);\n> +extern int git_config_parse_key(const char *, char **, int *, int);\n>  extern int git_config_set_multivar(const char *, const char *, const\n> char *, int);\n>  extern int git_config_set_multivar_in_file(const char *, const char\n> *, const char *, const char *, int);\n>  extern int git_config_rename_section(const char *, const char *);\n> diff --git a/config.c b/config.c\n> index 15a2983..eb9058c 100644\n> --- a/config.c\n> +++ b/config.c\n> @@ -1299,7 +1299,7 @@ static struct config_set_element\n> *configset_find_element(struct config_set *cs,\n>       * `key` may come from the user, so normalize it before using it\n>       * for querying entries from the hashmap.\n>       */\n> -    ret = git_config_parse_key(key, &normalized_key, NULL);\n> +    ret = git_config_parse_key(key, &normalized_key, NULL, 0);\n>\n>      if (ret)\n>          return NULL;\n> @@ -1832,8 +1832,9 @@ int git_config_set(const char *key, const char *value)\n>   *             lowercase section and variable name\n>   * baselen - pointer to int which will hold the length of the\n>   *           section + subsection part, can be NULL\n> + * show_err - toggle whether the function raises an error on a defective key\n>   */\n> -int git_config_parse_key(const char *key, char **store_key, int *baselen_)\n> +int git_config_parse_key(const char *key, char **store_key, int\n> *baselen_, int show_err)\n>  {\n>      int i, dot, baselen;\n>      const char *last_dot = strrchr(key, '.');\n> @@ -1844,12 +1845,14 @@ int git_config_parse_key(const char *key, char\n> **store_key, int *baselen_)\n>       */\n>\n>      if (last_dot == NULL || last_dot == key) {\n> -        error(\"key does not contain a section: %s\", key);\n> +        if (show_err)\n> +            error(\"key does not contain a section: %s\", key);\n>          return -CONFIG_NO_SECTION_OR_NAME;\n>      }\n>\n>      if (!last_dot[1]) {\n> -        error(\"key does not contain variable name: %s\", key);\n> +        if (show_err)\n> +            error(\"key does not contain variable name: %s\", key);\n>          return -CONFIG_NO_SECTION_OR_NAME;\n>      }\n>\n> @@ -1871,12 +1874,14 @@ int git_config_parse_key(const char *key, char\n> **store_key, int *baselen_)\n>          if (!dot || i > baselen) {\n>              if (!iskeychar(c) ||\n>                  (i == baselen + 1 && !isalpha(c))) {\n> -                error(\"invalid key: %s\", key);\n> +                if (show_err)\n> +                    error(\"invalid key: %s\", key);\n>                  goto out_free_ret_1;\n>              }\n>              c = tolower(c);\n>          } else if (c == '\\n') {\n> -            error(\"invalid key (newline): %s\", key);\n> +            if (show_err)\n> +                error(\"invalid key (newline): %s\", key);\n>              goto out_free_ret_1;\n>          }\n>          (*store_key)[i] = c;\n> @@ -1926,7 +1931,7 @@ int git_config_set_multivar_in_file(const char\n> *config_filename,\n>      char *filename_buf = NULL;\n>\n>      /* parse-key returns negative; flip the sign to feed exit(3) */\n> -    ret = 0 - git_config_parse_key(key, &store.key, &store.baselen);\n> +    ret = 0 - git_config_parse_key(key, &store.key, &store.baselen, 1);\n>      if (ret)\n>          goto out_free;\n"},{"id":"251230","messageId":"CAEc54XBWbBNzkh6OqBoMpMkO07qyDEOJ8vd8AN-tRWa9wKDJZg@mail.gmail.com","threadId":"37847","inReplyTo":"xmqqd299wh9m.fsf@gitster.dls.corp.google.com","subject":"Re: [PATCH] config: add show_err flag to git_config_parse_key()","fromName":"Tanay Abhra","fromEmail":"tanayabh@gmail.com","sentAt":"2014-10-30T18:25:42Z","receivedAt":"2014-10-30T18:25:42Z","isPatch":true,"sender":{"key":"tanayabh@gmail.com","avatar":"https://avatars.githubusercontent.com/u/2097229?v=4"},"body":">> ---\n>>\n>> Hi,\n>>\n>> You were right, one of the functions was calling git_config_parse_key()\n>> which was leaking errors to the console. git_config_parse_key() was\n>> meant for sanitizing user provided keys only but it was being used\n>> internally in a place where only a return value would be enough.\n>>\n>> Thanks for bringing this to our attention.\n>>\n>> Cheers,\n>> Tanay Abhra.\n>\n> Who are *you* in the above, and what was the bug report about (if it\n> was a bug report)?  Perhaps summarize it in a form of a few new tests\n> in t/t13XX series is in order?\n>\n> Thanks.\n>\n\nSorry about that, I am behind a firewall and had to use the gmail web interface.\nThe patches are butchered, I will send new ones with a proper connection\ntomorrow.\n\nThe original bug report is at [1].\n\nhttp://thread.gmane.org/gmane.comp.version-control.git/258886\n"}]}