mirror of
https://git.proxmox.com/git/libgit2
synced 2025-05-08 01:20:53 +00:00
Correctly quote config values while saving
If the value contains a command (; or #) char or starts or ends with space it needs to be quoted. Signed-off-by: Sven Strickroth <email@cs-ware.de>
This commit is contained in:
parent
b47949254e
commit
fde9325032
@ -1178,6 +1178,22 @@ static int write_section(git_filebuf *file, const char *key)
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int needsQuote(const char *value)
|
||||||
|
{
|
||||||
|
const char *ptr = value;
|
||||||
|
if (*value == ' ')
|
||||||
|
return 1;
|
||||||
|
while (*ptr) {
|
||||||
|
if (*ptr == ';' || *ptr == '#')
|
||||||
|
return 1;
|
||||||
|
++ptr;
|
||||||
|
}
|
||||||
|
if (ptr != value && *(--ptr) == ' ')
|
||||||
|
return 1;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* This is pretty much the parsing, except we write out anything we don't have
|
* This is pretty much the parsing, except we write out anything we don't have
|
||||||
*/
|
*/
|
||||||
@ -1299,7 +1315,10 @@ static int config_write(diskfile_backend *cfg, const char *key, const regex_t *p
|
|||||||
/* Then replace the variable. If the value is NULL, it
|
/* Then replace the variable. If the value is NULL, it
|
||||||
* means we want to delete it, so don't write anything. */
|
* means we want to delete it, so don't write anything. */
|
||||||
if (value != NULL) {
|
if (value != NULL) {
|
||||||
git_filebuf_printf(&file, "\t%s = %s\n", name, value);
|
if (needsQuote(value))
|
||||||
|
git_filebuf_printf(&file, "\t%s = \"%s\"\n", name, value);
|
||||||
|
else
|
||||||
|
git_filebuf_printf(&file, "\t%s = %s\n", name, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -1359,7 +1378,10 @@ static int config_write(diskfile_backend *cfg, const char *key, const regex_t *p
|
|||||||
if (reader->buffer.size > 0 && *(reader->buffer.ptr + reader->buffer.size - 1) != '\n')
|
if (reader->buffer.size > 0 && *(reader->buffer.ptr + reader->buffer.size - 1) != '\n')
|
||||||
git_filebuf_write(&file, "\n", 1);
|
git_filebuf_write(&file, "\n", 1);
|
||||||
|
|
||||||
git_filebuf_printf(&file, "\t%s = %s\n", name, value);
|
if (needsQuote(value))
|
||||||
|
git_filebuf_printf(&file, "\t%s = \"%s\"\n", name, value);
|
||||||
|
else
|
||||||
|
git_filebuf_printf(&file, "\t%s = %s\n", name, value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user