diff --git a/src/oid.c b/src/oid.c index 182859f51..0bc152bdf 100644 --- a/src/oid.c +++ b/src/oid.c @@ -106,7 +106,9 @@ char *git_oid_to_string(char *out, size_t n, const git_oid *oid) if (n > 0) { git_oid_fmt(str, oid); - memcpy(out, str, n > GIT_OID_HEXSZ ? GIT_OID_HEXSZ : n); + if (n > GIT_OID_HEXSZ) + n = GIT_OID_HEXSZ; + memcpy(out, str, n); } out[n] = '\0'; diff --git a/tests/t0101-oid.c b/tests/t0101-oid.c index c867e4ff4..3f61a7570 100644 --- a/tests/t0101-oid.c +++ b/tests/t0101-oid.c @@ -251,3 +251,28 @@ BEGIN_TEST(oid_to_string) must_pass(strcmp(exp, out)); END_TEST +BEGIN_TEST(oid_to_string_big) + const char *exp = "16a0123456789abcdef4b775213c23a8bd74f5e0"; + git_oid in; + char big[GIT_OID_HEXSZ + 1 + 3]; /* note + 4 => big buffer */ + char *str; + + must_pass(git_oid_mkstr(&in, exp)); + + /* place some tail material */ + big[GIT_OID_HEXSZ+0] = 'W'; /* should be '\0' afterwards */ + big[GIT_OID_HEXSZ+1] = 'X'; /* should remain untouched */ + big[GIT_OID_HEXSZ+2] = 'Y'; /* ditto */ + big[GIT_OID_HEXSZ+3] = 'Z'; /* ditto */ + + /* returns big as hex formatted c-string */ + str = git_oid_to_string(big, sizeof(big), &in); + must_be_true(str && str == big && *(str+GIT_OID_HEXSZ) == '\0'); + must_pass(strcmp(exp, big)); + + /* check tail material is untouched */ + must_be_true(str && str == big && *(str+GIT_OID_HEXSZ+1) == 'X'); + must_be_true(str && str == big && *(str+GIT_OID_HEXSZ+2) == 'Y'); + must_be_true(str && str == big && *(str+GIT_OID_HEXSZ+3) == 'Z'); +END_TEST +