]> git.proxmox.com Git - libgit2.git/commitdiff
git_buf_quote/unquote: handle > \177
authorEdward Thomson <ethomson@microsoft.com>
Fri, 25 Sep 2015 14:48:19 +0000 (10:48 -0400)
committerEdward Thomson <ethomson@github.com>
Thu, 26 May 2016 18:01:07 +0000 (13:01 -0500)
Parse values up to and including `\377` (`0xff`) when unquoting.
Print octal values as an unsigned char when quoting, lest `printf`
think we're talking about negatives.

src/buffer.c
tests/buf/quote.c

index 31341c4b5d03fd7ce979642b1ff10ab5778b473c..d135ebe4a3b5c7e904a4b3c3a8eb6c59be65f9af 100644 (file)
@@ -901,7 +901,7 @@ int git_buf_quote(git_buf *buf)
                /* escape anything unprintable as octal */
                else if (buf->ptr[i] != ' ' &&
                                (buf->ptr[i] < '!' || buf->ptr[i] > '~')) {
-                       git_buf_printf(&quoted, "\\%03o", buf->ptr[i]);
+                       git_buf_printf(&quoted, "\\%03o", (unsigned char)buf->ptr[i]);
                }
 
                /* yay, printable! */
@@ -959,7 +959,7 @@ int git_buf_unquote(git_buf *buf)
                        case 'v': ch = '\v'; break;
 
                        /* \xyz digits convert to the char*/
-                       case '0': case '1': case '2':
+                       case '0': case '1': case '2': case '3':
                                if (j == buf->size-3) {
                                        giterr_set(GITERR_INVALID,
                                                "Truncated quoted character \\%c", ch);
index ed5021e25fe6cf9a87d7c9ce990b8bb7ebf9d4bf..6f77ab9c1c98832689bbd02cf2108e19d9f0d8a5 100644 (file)
@@ -25,6 +25,7 @@ void test_buf_quote__quote_succeeds(void)
        expect_quote_pass("\"foo\\r\\nbar\"", "foo\r\nbar");
        expect_quote_pass("\"foo\\177bar\"", "foo\177bar");
        expect_quote_pass("\"foo\\001bar\"", "foo\001bar");
+       expect_quote_pass("\"foo\\377bar\"", "foo\377bar");
 }
 
 static void expect_unquote_pass(const char *expected, const char *quoted)
@@ -64,6 +65,7 @@ void test_buf_quote__unquote_succeeds(void)
        expect_unquote_pass("foo\r\nbar", "\"foo\\015\\012bar\"");
        expect_unquote_pass("foo\r\nbar", "\"\\146\\157\\157\\015\\012\\142\\141\\162\"");
        expect_unquote_pass("newline: \n", "\"newline: \\012\"");
+       expect_unquote_pass("0xff: \377", "\"0xff: \\377\"");
 }
 
 void test_buf_quote__unquote_fails(void)
@@ -76,6 +78,9 @@ void test_buf_quote__unquote_fails(void)
        expect_unquote_fail("\"invalid escape char \\p\"");
        expect_unquote_fail("\"invalid \\1 escape char \"");
        expect_unquote_fail("\"invalid \\14 escape char \"");
+       expect_unquote_fail("\"invalid \\280 escape char\"");
+       expect_unquote_fail("\"invalid \\378 escape char\"");
+       expect_unquote_fail("\"invalid \\380 escape char\"");
        expect_unquote_fail("\"invalid \\411 escape char\"");
        expect_unquote_fail("\"truncated escape char \\\"");
        expect_unquote_fail("\"truncated escape char \\0\"");