attr.c: avoid inappropriate access to strbuf "buf" member

This code sequence performs a strcpy into the buf member of a strbuf
struct.  The strcpy may move the position of the terminating nul of the
string and effectively change the length of string so that it does not
match the len member of the strbuf struct.

Currently, this sequence works since the strbuf was given a hint when it
was initialized to allocate enough space to accomodate the string that will
be strcpy'ed, but this is an implementation detail of strbufs, not a
guarantee.

So, lets rework this sequence so that the strbuf is only manipulated by
strbuf functions, and direct modification of its "buf" member is not
necessary.

Signed-off-by: Brandon Casey <drafnel@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Brandon Casey 2011-10-06 13:22:21 -05:00 committed by Junio C Hamano
parent 5738c9c21e
commit 97410b27e9

24
attr.c
View File

@ -552,7 +552,6 @@ static void prepare_attr_stack(const char *path)
{ {
struct attr_stack *elem, *info; struct attr_stack *elem, *info;
int dirlen, len; int dirlen, len;
struct strbuf pathbuf;
const char *cp; const char *cp;
cp = strrchr(path, '/'); cp = strrchr(path, '/');
@ -561,8 +560,6 @@ static void prepare_attr_stack(const char *path)
else else
dirlen = cp - path; dirlen = cp - path;
strbuf_init(&pathbuf, dirlen+2+strlen(GITATTRIBUTES_FILE));
/* /*
* At the bottom of the attribute stack is the built-in * At the bottom of the attribute stack is the built-in
* set of attribute definitions, followed by the contents * set of attribute definitions, followed by the contents
@ -607,27 +604,28 @@ static void prepare_attr_stack(const char *path)
* Read from parent directories and push them down * Read from parent directories and push them down
*/ */
if (!is_bare_repository() || direction == GIT_ATTR_INDEX) { if (!is_bare_repository() || direction == GIT_ATTR_INDEX) {
while (1) { struct strbuf pathbuf = STRBUF_INIT;
char *cp;
while (1) {
len = strlen(attr_stack->origin); len = strlen(attr_stack->origin);
if (dirlen <= len) if (dirlen <= len)
break; break;
strbuf_reset(&pathbuf); cp = memchr(path + len + 1, '/', dirlen - len - 1);
strbuf_add(&pathbuf, path, dirlen); if (!cp)
cp = path + dirlen;
strbuf_add(&pathbuf, path, cp - path);
strbuf_addch(&pathbuf, '/'); strbuf_addch(&pathbuf, '/');
cp = strchr(pathbuf.buf + len + 1, '/'); strbuf_addstr(&pathbuf, GITATTRIBUTES_FILE);
strcpy(cp + 1, GITATTRIBUTES_FILE);
elem = read_attr(pathbuf.buf, 0); elem = read_attr(pathbuf.buf, 0);
*cp = '\0'; strbuf_setlen(&pathbuf, cp - path);
elem->origin = strdup(pathbuf.buf); elem->origin = strbuf_detach(&pathbuf, NULL);
elem->prev = attr_stack; elem->prev = attr_stack;
attr_stack = elem; attr_stack = elem;
debug_push(elem); debug_push(elem);
} }
}
strbuf_release(&pathbuf); strbuf_release(&pathbuf);
}
/* /*
* Finally push the "info" one at the top of the stack. * Finally push the "info" one at the top of the stack.