ref-filter: drop sprintf and strcpy calls
[gitweb.git] / builtin / config.c
index 91aa56feedc87e52a63dd6662d4227bba6267ccf..adc772786a7ddf9952aaccb7578b57d10396da93 100644 (file)
@@ -111,41 +111,35 @@ static int format_config(struct strbuf *buf, const char *key_, const char *value
        if (show_keys)
                strbuf_addstr(buf, key_);
        if (!omit_values) {
-               int must_free_vptr = 0;
-               int must_add_delim = show_keys;
-               char value[256];
-               const char *vptr = value;
+               if (show_keys)
+                       strbuf_addch(buf, key_delim);
 
                if (types == TYPE_INT)
-                       sprintf(value, "%"PRId64,
-                               git_config_int64(key_, value_ ? value_ : ""));
+                       strbuf_addf(buf, "%"PRId64,
+                                   git_config_int64(key_, value_ ? value_ : ""));
                else if (types == TYPE_BOOL)
-                       vptr = git_config_bool(key_, value_) ? "true" : "false";
+                       strbuf_addstr(buf, git_config_bool(key_, value_) ?
+                                     "true" : "false");
                else if (types == TYPE_BOOL_OR_INT) {
                        int is_bool, v;
                        v = git_config_bool_or_int(key_, value_, &is_bool);
                        if (is_bool)
-                               vptr = v ? "true" : "false";
+                               strbuf_addstr(buf, v ? "true" : "false");
                        else
-                               sprintf(value, "%d", v);
+                               strbuf_addf(buf, "%d", v);
                } else if (types == TYPE_PATH) {
-                       if (git_config_pathname(&vptr, key_, value_) < 0)
+                       const char *v;
+                       if (git_config_pathname(&v, key_, value_) < 0)
                                return -1;
-                       must_free_vptr = 1;
+                       strbuf_addstr(buf, v);
+                       free((char *)v);
                } else if (value_) {
-                       vptr = value_;
+                       strbuf_addstr(buf, value_);
                } else {
-                       /* Just show the key name */
-                       vptr = "";
-                       must_add_delim = 0;
+                       /* Just show the key name; back out delimiter */
+                       if (show_keys)
+                               strbuf_setlen(buf, buf->len - 1);
                }
-
-               if (must_add_delim)
-                       strbuf_addch(buf, key_delim);
-               strbuf_addstr(buf, vptr);
-
-               if (must_free_vptr)
-                       free((char *)vptr);
        }
        strbuf_addch(buf, term);
        return 0;
@@ -252,8 +246,6 @@ static int get_value(const char *key_, const char *regex_)
 
 static char *normalize_value(const char *key, const char *value)
 {
-       char *normalized;
-
        if (!value)
                return NULL;
 
@@ -264,27 +256,21 @@ static char *normalize_value(const char *key, const char *value)
                 * "~/foobar/" in the config file, and to expand the ~
                 * when retrieving the value.
                 */
-               normalized = xstrdup(value);
-       else {
-               normalized = xmalloc(64);
-               if (types == TYPE_INT) {
-                       int64_t v = git_config_int64(key, value);
-                       sprintf(normalized, "%"PRId64, v);
-               }
-               else if (types == TYPE_BOOL)
-                       sprintf(normalized, "%s",
-                               git_config_bool(key, value) ? "true" : "false");
-               else if (types == TYPE_BOOL_OR_INT) {
-                       int is_bool, v;
-                       v = git_config_bool_or_int(key, value, &is_bool);
-                       if (!is_bool)
-                               sprintf(normalized, "%d", v);
-                       else
-                               sprintf(normalized, "%s", v ? "true" : "false");
-               }
+               return xstrdup(value);
+       if (types == TYPE_INT)
+               return xstrfmt("%"PRId64, git_config_int64(key, value));
+       if (types == TYPE_BOOL)
+               return xstrdup(git_config_bool(key, value) ?  "true" : "false");
+       if (types == TYPE_BOOL_OR_INT) {
+               int is_bool, v;
+               v = git_config_bool_or_int(key, value, &is_bool);
+               if (!is_bool)
+                       return xstrfmt("%d", v);
+               else
+                       return xstrdup(v ? "true" : "false");
        }
 
-       return normalized;
+       die("BUG: cannot normalize type %d", types);
 }
 
 static int get_color_found;
@@ -431,14 +417,11 @@ static int get_urlmatch(const char *var, const char *url)
 
        for_each_string_list_item(item, &values) {
                struct urlmatch_current_candidate_value *matched = item->util;
-               struct strbuf key = STRBUF_INIT;
                struct strbuf buf = STRBUF_INIT;
 
-               strbuf_addstr(&key, item->string);
-               format_config(&buf, key.buf,
+               format_config(&buf, item->string,
                              matched->value_is_null ? NULL : matched->value.buf);
                fwrite(buf.buf, 1, buf.len, stdout);
-               strbuf_release(&key);
                strbuf_release(&buf);
 
                strbuf_release(&matched->value);