]> git.proxmox.com Git - libgit2.git/commitdiff
Fix a bug in the git_oid_to_string() function
authorRamsay Jones <ramsay@ramsay1.demon.co.uk>
Wed, 2 Jun 2010 18:16:28 +0000 (19:16 +0100)
committerRamsay Jones <ramsay@ramsay1.demon.co.uk>
Mon, 7 Jun 2010 18:44:04 +0000 (19:44 +0100)
When git_oid_to_string() was passed a buffer size larger than
GIT_OID_HEXSZ+1, the function placed the c-string NUL char at
the wrong position. Fix the code to place the NUL at the end
of the (possibly truncated) oid string.

Signed-off-by: Ramsay Jones <ramsay@ramsay1.demon.co.uk>
src/oid.c
tests/t0101-oid.c

index 182859f51a5cdd6ae9a1f75e21eb8d3f95d0a833..0bc152bdf2707576e6ef9331189607c89f9ef8a1 100644 (file)
--- 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';
index c867e4ff48cb00173ab50011a7cd593ab9d3905e..3f61a75703b0430062d1908fc0e3e682cb8b95e7 100644 (file)
@@ -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
+