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

parse_config_key(): return subsection len as size_t

We return the length to a subset of a string using an "int *" out-parameter. This is fine most of the time, as we'd expect config keys to be relatively short, but it could behave oddly if we had a gigantic config key. A more appropriate type is size_t. Let's switch over, which lets our callers use size_t as appropriate (they are bound by our type because they must pass the out-parameter as a pointer). This is mostly just a cleanup to make it clear this code handles long strings correctly. In practice, our config parser already chokes on long key names (because of a similar int/size_t mixup!). When doing an int/size_t conversion, we have to be careful that nobody was trying to assign a negative value to the variable. I manually confirmed that for each case here. They tend to just feed the result to xmemdupz() or similar; in a few cases I adjusted the parameter types for helper functions to make sure the size_t is preserved. 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:44 UTC f5914f4b6bcdb517733c761fe5ba9d94471eb01d
12 files changed +16 -15
archive-tar.c
+2 -2
@@ -364,7 +364,7 @@ static struct archiver **tar_filters;
364 static int nr_tar_filters;
365 static int alloc_tar_filters;
366
367 -static struct archiver *find_tar_filter(const char *name, int len)
367 +static struct archiver *find_tar_filter(const char *name, size_t len)
368 {
369 int i;
370 for (i = 0; i < nr_tar_filters; i++) {
@@ -380,7 +380,7 @@ static int tar_filter_config(const char *var, const char *value, void *data)
380 struct archiver *ar;
381 const char *name;
382 const char *type;
383 - int namelen;
383 + size_t namelen;
384
385 if (parse_config_key(var, "tar", &name, &namelen, &type) < 0 || !name)
386 return 0;
builtin/help.c
+1 -1
@@ -242,7 +242,7 @@ static int add_man_viewer_cmd(const char *name,
242 static int add_man_viewer_info(const char *var, const char *value)
243 {
244 const char *name, *subkey;
245 - int namelen;
245 + size_t namelen;
246
247 if (parse_config_key(var, "man", &name, &namelen, &subkey) < 0 || !name)
248 return 0;
builtin/reflog.c
+1 -1
@@ -459,7 +459,7 @@ static struct reflog_expire_cfg *find_cfg_ent(const char *pattern, size_t len)
459 static int reflog_expire_config(const char *var, const char *value, void *cb)
460 {
461 const char *pattern, *key;
462 - int pattern_len;
462 + size_t pattern_len;
463 timestamp_t expire;
464 int slot;
465 struct reflog_expire_cfg *ent;
config.c
+2 -2
@@ -309,7 +309,7 @@ int git_config_include(const char *var, const char *value, void *data)
309 {
310 struct config_include_data *inc = data;
311 const char *cond, *key;
312 - int cond_len;
312 + size_t cond_len;
313 int ret;
314
315 /*
@@ -3238,7 +3238,7 @@ int config_error_nonbool(const char *var)
3238
3239 int parse_config_key(const char *var,
3240 const char *section,
3241 - const char **subsection, int *subsection_len,
3241 + const char **subsection, size_t *subsection_len,
3242 const char **key)
3243 {
3244 const char *dot;
config.h
+1 -1
@@ -359,7 +359,7 @@ int git_config_include(const char *name, const char *value, void *data);
359 */
360 int parse_config_key(const char *var,
361 const char *section,
362 - const char **subsection, int *subsection_len,
362 + const char **subsection, size_t *subsection_len,
363 const char **key);
364
365 /**
convert.c
+1 -1
@@ -1018,7 +1018,7 @@ static int apply_filter(const char *path, const char *src, size_t len,
1018 static int read_convert_config(const char *var, const char *value, void *cb)
1019 {
1020 const char *key, *name;
1021 - int namelen;
1021 + size_t namelen;
1022 struct convert_driver *drv;
1023
1024 /*
fsck.c
+1 -1
@@ -920,7 +920,7 @@ static int fsck_gitmodules_fn(const char *var, const char *value, void *vdata)
920 {
921 struct fsck_gitmodules_data *data = vdata;
922 const char *subsection, *key;
923 - int subsection_len;
923 + size_t subsection_len;
924 char *name;
925
926 if (parse_config_key(var, "submodule", &subsection, &subsection_len, &key) < 0 ||
ll-merge.c
+1 -1
@@ -247,7 +247,7 @@ static int read_merge_config(const char *var, const char *value, void *cb)
247 {
248 struct ll_merge_driver *fn;
249 const char *key, *name;
250 - int namelen;
250 + size_t namelen;
251
252 if (!strcmp(var, "merge.default"))
253 return git_config_string(&default_ll_merge, var, value);
promisor-remote.c
+1 -1
@@ -101,7 +101,7 @@ static void promisor_remote_move_to_tail(struct promisor_remote *r,
101 static int promisor_remote_config(const char *var, const char *value, void *data)
102 {
103 const char *name;
104 - int namelen;
104 + size_t namelen;
105 const char *subkey;
106
107 if (!strcmp(var, "core.partialclonefilter"))
remote.c
+1 -1
@@ -305,7 +305,7 @@ static void read_branches_file(struct remote *remote)
305 static int handle_config(const char *key, const char *value, void *cb)
306 {
307 const char *name;
308 - int namelen;
308 + size_t namelen;
309 const char *subkey;
310 struct remote *remote;
311 struct branch *branch;
submodule-config.c
+2 -1
@@ -225,7 +225,8 @@ static int name_and_item_from_var(const char *var, struct strbuf *name,
225 struct strbuf *item)
226 {
227 const char *subsection, *key;
228 - int subsection_len, parse;
228 + size_t subsection_len;
229 + int parse;
230 parse = parse_config_key(var, "submodule", &subsection,
231 &subsection_len, &key);
232 if (parse < 0 || !subsection)
userdiff.c
+2 -2
@@ -222,7 +222,7 @@ static struct userdiff_driver driver_false = {
222 { NULL, 0 }
223 };
224
225 -static struct userdiff_driver *userdiff_find_by_namelen(const char *k, int len)
225 +static struct userdiff_driver *userdiff_find_by_namelen(const char *k, size_t len)
226 {
227 int i;
228 for (i = 0; i < ndrivers; i++) {
@@ -266,7 +266,7 @@ int userdiff_config(const char *k, const char *v)
266 {
267 struct userdiff_driver *drv;
268 const char *name, *type;
269 - int namelen;
269 + size_t namelen;
270
271 if (parse_config_key(k, "diff", &name, &namelen, &type) || !name)
272 return 0;