add_ref(): verify that the refname is formatted correctly
In add_ref(), verify that the refname is formatted correctly before adding it to the ref_list. Here we have to allow refname components that start with ".", since (for example) the remote protocol uses synthetic reference name ".have". So add a new REFNAME_DOT_COMPONENT flag that can be passed to check_refname_format() to allow leading dots. Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu> Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
parent
7cb368421f
commit
dce4bab656
18
refs.c
18
refs.c
@ -56,6 +56,8 @@ static struct ref_list *add_ref(const char *name, const unsigned char *sha1,
|
|||||||
entry = xmalloc(sizeof(struct ref_list) + len);
|
entry = xmalloc(sizeof(struct ref_list) + len);
|
||||||
hashcpy(entry->sha1, sha1);
|
hashcpy(entry->sha1, sha1);
|
||||||
hashclr(entry->peeled);
|
hashclr(entry->peeled);
|
||||||
|
if (check_refname_format(name, REFNAME_ALLOW_ONELEVEL|REFNAME_DOT_COMPONENT))
|
||||||
|
die("Reference has invalid format: '%s'", name);
|
||||||
memcpy(entry->name, name, len);
|
memcpy(entry->name, name, len);
|
||||||
entry->flag = flag;
|
entry->flag = flag;
|
||||||
entry->next = list;
|
entry->next = list;
|
||||||
@ -900,7 +902,7 @@ static inline int bad_ref_char(int ch)
|
|||||||
* the length of the component found, or -1 if the component is not
|
* the length of the component found, or -1 if the component is not
|
||||||
* legal.
|
* legal.
|
||||||
*/
|
*/
|
||||||
static int check_refname_component(const char *ref)
|
static int check_refname_component(const char *ref, int flags)
|
||||||
{
|
{
|
||||||
const char *cp;
|
const char *cp;
|
||||||
char last = '\0';
|
char last = '\0';
|
||||||
@ -919,8 +921,16 @@ static int check_refname_component(const char *ref)
|
|||||||
}
|
}
|
||||||
if (cp == ref)
|
if (cp == ref)
|
||||||
return -1; /* Component has zero length. */
|
return -1; /* Component has zero length. */
|
||||||
if (ref[0] == '.')
|
if (ref[0] == '.') {
|
||||||
return -1; /* Component starts with '.'. */
|
if (!(flags & REFNAME_DOT_COMPONENT))
|
||||||
|
return -1; /* Component starts with '.'. */
|
||||||
|
/*
|
||||||
|
* Even if leading dots are allowed, don't allow "."
|
||||||
|
* as a component (".." is prevented by a rule above).
|
||||||
|
*/
|
||||||
|
if (ref[1] == '\0')
|
||||||
|
return -1; /* Component equals ".". */
|
||||||
|
}
|
||||||
if (cp - ref >= 5 && !memcmp(cp - 5, ".lock", 5))
|
if (cp - ref >= 5 && !memcmp(cp - 5, ".lock", 5))
|
||||||
return -1; /* Refname ends with ".lock". */
|
return -1; /* Refname ends with ".lock". */
|
||||||
return cp - ref;
|
return cp - ref;
|
||||||
@ -932,7 +942,7 @@ int check_refname_format(const char *ref, int flags)
|
|||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
/* We are at the start of a path component. */
|
/* We are at the start of a path component. */
|
||||||
component_len = check_refname_component(ref);
|
component_len = check_refname_component(ref, flags);
|
||||||
if (component_len < 0) {
|
if (component_len < 0) {
|
||||||
if ((flags & REFNAME_REFSPEC_PATTERN) &&
|
if ((flags & REFNAME_REFSPEC_PATTERN) &&
|
||||||
ref[0] == '*' &&
|
ref[0] == '*' &&
|
||||||
|
6
refs.h
6
refs.h
@ -99,6 +99,7 @@ extern int for_each_reflog(each_ref_fn, void *);
|
|||||||
|
|
||||||
#define REFNAME_ALLOW_ONELEVEL 1
|
#define REFNAME_ALLOW_ONELEVEL 1
|
||||||
#define REFNAME_REFSPEC_PATTERN 2
|
#define REFNAME_REFSPEC_PATTERN 2
|
||||||
|
#define REFNAME_DOT_COMPONENT 4
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Return 0 iff ref has the correct format for a refname according to
|
* Return 0 iff ref has the correct format for a refname according to
|
||||||
@ -106,7 +107,10 @@ extern int for_each_reflog(each_ref_fn, void *);
|
|||||||
* REFNAME_ALLOW_ONELEVEL is set in flags, then accept one-level
|
* REFNAME_ALLOW_ONELEVEL is set in flags, then accept one-level
|
||||||
* reference names. If REFNAME_REFSPEC_PATTERN is set in flags, then
|
* reference names. If REFNAME_REFSPEC_PATTERN is set in flags, then
|
||||||
* allow a "*" wildcard character in place of one of the name
|
* allow a "*" wildcard character in place of one of the name
|
||||||
* components. No leading or repeated slashes are accepted.
|
* components. No leading or repeated slashes are accepted. If
|
||||||
|
* REFNAME_DOT_COMPONENT is set in flags, then allow refname
|
||||||
|
* components to start with "." (but not a whole component equal to
|
||||||
|
* "." or "..").
|
||||||
*/
|
*/
|
||||||
extern int check_refname_format(const char *ref, int flags);
|
extern int check_refname_format(const char *ref, int flags);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user