@matheusbsbs / matheus-bagliano / commits / f011a9654d
Fork 0

git_config_parse_key(): return baselen as size_t

As with the recent change to parse_config_key(), the best type to return a string length is a size_t, as it won't cause integer truncation for a gigantic key. And as with that change, this is mostly a clarity / hygiene issue for now, as our config parser would choke on such a large key anyway. There are a few ripple effects within the config code, as callers switch to using size_t. I also adjusted a few related variables that iterate over strings. The most unexpected change is that a call to strbuf_addf() had to switch to strbuf_add(). We can't use a size_t with "%.*s", because printf precisions must have type "int" (we could cast, of course, but that would miss the point of using size_t in the first place). Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Apr 10, 2020 at 15:46 UTC f011a9654dc2a2e3238985ba7767c1058e7cb3c2
2 files changed +11 -8
config.c
+10 -7
@@ -358,12 +358,13 @@ static inline int iskeychar(int c)
358 *
359 * store_key - pointer to char* which will hold a copy of the key with
360 * lowercase section and variable name
361 - * baselen - pointer to int which will hold the length of the
361 + * baselen - pointer to size_t which will hold the length of the
362 * section + subsection part, can be NULL
363 */
364 -static int git_config_parse_key_1(const char *key, char **store_key, int *baselen_, int quiet)
364 +static int git_config_parse_key_1(const char *key, char **store_key, size_t *baselen_, int quiet)
365 {
366 - int i, dot, baselen;
366 + size_t i, baselen;
367 + int dot;
368 const char *last_dot = strrchr(key, '.');
369
370 /*
@@ -425,7 +426,7 @@ out_free_ret_1:
426 return -CONFIG_INVALID_KEY;
427 }
428
428 -int git_config_parse_key(const char *key, char **store_key, int *baselen)
429 +int git_config_parse_key(const char *key, char **store_key, size_t *baselen)
430 {
431 return git_config_parse_key_1(key, store_key, baselen, 0);
432 }
@@ -2383,7 +2384,7 @@ void git_die_config(const char *key, const char *err, ...)
2384 */
2385
2386 struct config_store_data {
2386 - int baselen;
2387 + size_t baselen;
2388 char *key;
2389 int do_not_match;
2390 regex_t *value_regex;
@@ -2509,7 +2510,7 @@ static struct strbuf store_create_section(const char *key,
2510 const struct config_store_data *store)
2511 {
2512 const char *dot;
2512 - int i;
2513 + size_t i;
2514 struct strbuf sb = STRBUF_INIT;
2515
2516 dot = memchr(key, '.', store->baselen);
@@ -2522,7 +2523,9 @@ static struct strbuf store_create_section(const char *key,
2523 }
2524 strbuf_addstr(&sb, "\"]\n");
2525 } else {
2525 - strbuf_addf(&sb, "[%.*s]\n", store->baselen, key);
2526 + strbuf_addch(&sb, '[');
2527 + strbuf_add(&sb, key, store->baselen);
2528 + strbuf_addstr(&sb, "]\n");
2529 }
2530
2531 return sb;
config.h
+1 -1
@@ -254,7 +254,7 @@ int git_config_set_gently(const char *, const char *);
254 */
255 void git_config_set(const char *, const char *);
256
257 -int git_config_parse_key(const char *, char **, int *);
257 +int git_config_parse_key(const char *, char **, size_t *);
258 int git_config_key_is_valid(const char *key);
259 int git_config_set_multivar_gently(const char *, const char *, const char *, int);
260 void git_config_set_multivar(const char *, const char *, const char *, int);