summaryrefslogtreecommitdiff
path: root/src/ident.c
diff options
context:
space:
mode:
authorRussell Belfer <rb@github.com>2013-09-16 12:54:40 -0700
committerRussell Belfer <rb@github.com>2013-09-17 09:31:46 -0700
commiteefc32d54944ead5a5e3041c1b1f6c8c946cc014 (patch)
treee6d22bdf1655a37cbde72d192ca9c0d0bae6fa12 /src/ident.c
parenteab3746b3026950ed62842c1e5641556d7131a5b (diff)
downloadlibgit2-eefc32d54944ead5a5e3041c1b1f6c8c946cc014.tar.gz
Bug fixes and cleanups
This contains a few bug fixes and some header and API cleanups. The main API change is that filters should now use GIT_PASSTHROUGH to indicate that they wish to skip processing a file instead of GIT_ENOTFOUND. The bug fixes include a possible out-of-range buffer access in the ident filter, a filter ordering problem I introduced into the custom filter tests on Windows, and a filter buf NUL termination issue that was coming up on Linux.
Diffstat (limited to 'src/ident.c')
-rw-r--r--src/ident.c26
1 files changed, 14 insertions, 12 deletions
diff --git a/src/ident.c b/src/ident.c
index 23c407f16..51630879d 100644
--- a/src/ident.c
+++ b/src/ident.c
@@ -13,23 +13,25 @@
static int ident_find_id(
const char **id_start, const char **id_end, const char *start, size_t len)
{
- const char *found;
+ const char *end = start + len, *found = NULL;
- while (len > 0 && (found = memchr(start, '$', len)) != NULL) {
- size_t remaining = len - (size_t)(found - start);
+ while (len > 3 && (found = memchr(start, '$', len)) != NULL) {
+ size_t remaining = (size_t)(end - found) - 1;
if (remaining < 3)
return GIT_ENOTFOUND;
- if (found[1] == 'I' && found[2] == 'd')
- break;
+
start = found + 1;
- len = remaining - 1;
+ len = remaining;
+
+ if (start[0] == 'I' && start[1] == 'd')
+ break;
}
- if (!found || len < 3)
+ if (len < 3 || !found)
return GIT_ENOTFOUND;
*id_start = found;
- if ((found = memchr(found + 3, '$', len - 3)) == NULL)
+ if ((found = memchr(start + 2, '$', len - 2)) == NULL)
return GIT_ENOTFOUND;
*id_end = found + 1;
@@ -46,12 +48,12 @@ static int ident_insert_id(
/* replace $Id$ with blob id */
if (!git_filter_source_id(src))
- return GIT_ENOTFOUND;
+ return GIT_PASSTHROUGH;
git_oid_tostr(oid, sizeof(oid), git_filter_source_id(src));
if (ident_find_id(&id_start, &id_end, from->ptr, from->size) < 0)
- return GIT_ENOTFOUND;
+ return GIT_PASSTHROUGH;
need_size = (size_t)(id_start - from->ptr) +
5 /* "$Id: " */ + GIT_OID_HEXSZ + 1 /* "$" */ +
@@ -76,7 +78,7 @@ static int ident_remove_id(
size_t need_size;
if (ident_find_id(&id_start, &id_end, from->ptr, from->size) < 0)
- return GIT_ENOTFOUND;
+ return GIT_PASSTHROUGH;
need_size = (size_t)(id_start - from->ptr) +
4 /* "$Id$" */ + (size_t)(from_end - id_end);
@@ -102,7 +104,7 @@ static int ident_apply(
/* Don't filter binary files */
if (git_buf_text_is_binary(from))
- return GIT_ENOTFOUND;
+ return GIT_PASSTHROUGH;
if (git_filter_source_mode(src) == GIT_FILTER_SMUDGE)
return ident_insert_id(to, from, src);