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>
This commit is contained in:
Jeff King
2020-04-10 15:44:28 -04:00
committed by Junio C Hamano
parent 021ba32a7b
commit f5914f4b6b
12 changed files with 16 additions and 15 deletions

View File

@ -222,7 +222,7 @@ static struct userdiff_driver driver_false = {
{ NULL, 0 }
};
static struct userdiff_driver *userdiff_find_by_namelen(const char *k, int len)
static struct userdiff_driver *userdiff_find_by_namelen(const char *k, size_t len)
{
int i;
for (i = 0; i < ndrivers; i++) {
@ -266,7 +266,7 @@ int userdiff_config(const char *k, const char *v)
{
struct userdiff_driver *drv;
const char *name, *type;
int namelen;
size_t namelen;
if (parse_config_key(k, "diff", &name, &namelen, &type) || !name)
return 0;