{"thread":{"id":"66576","subject":"[PATCH] config: read the bytes of a blob as unsigned characters","startedAt":"2026-10-18T15:24:26Z","lastAt":"2026-10-19T20:55:35Z","messageCount":2,"participants":["Olof Fairbrook","Junio C Hamano"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"555353","messageId":"20261018152426.2716899-1-olof.fairbrook@gmail.com","threadId":"66576","inReplyTo":null,"subject":"[PATCH] config: read the bytes of a blob as unsigned characters","fromName":"Olof Fairbrook","fromEmail":"olof.fairbrook@gmail.com","sentAt":"2026-10-18T15:24:26Z","receivedAt":"2026-10-18T15:24:26Z","isPatch":true,"sender":{"key":"olof.fairbrook@gmail.com","avatar":null},"body":"\"git config --blob\" and the other readers of config from memory get\neach byte from config_buf_fgetc(), which returns it as a plain \"char\".\nWhere \"char\" is signed, as on x86, a byte 0xff comes back as -1, which\nis EOF, and any byte from 0x80 up as a negative number.  So the parser\nstops at a byte 0xff and silently ignores the rest of the blob:\n\n  $ printf '# caf\\377\\n[some]\\n\\tkey = value\\n' >config\n  $ git config --blob=$(git hash-object -w config) --list\n  $\n\nand a blob that starts with a UTF-8 byte order mark is a \"bad config\nline 1\", because the code that skips the mark compares with unsigned\nbytes.  A config file has neither problem, as getc() returns unsigned\nbytes.  \".gitmodules\" read from a commit goes through the same reader.\n\nThis has been so since config_buf_fgetc() was added in 1bc888193e\n(teach config --blob option to parse config from database,\n2013-07-12).\n\nReturn the byte as an unsigned char, as getc() does.\nconfig_buf_ungetc() checks that it is given back the byte it read; it\nnow compares with the byte as an unsigned char too, or a CR followed\nby such a byte would hit its BUG().\n\nSigned-off-by: Olof Fairbrook <olof.fairbrook@gmail.com>\n---\n config.c               |  4 ++--\n t/t1307-config-blob.sh | 21 +++++++++++++++++++++\n 2 files changed, 23 insertions(+), 2 deletions(-)\n\ndiff --git a/config.c b/config.c\nindex d9019e7e6c..fbe873274e 100644\n--- a/config.c\n+++ b/config.c\n@@ -92,7 +92,7 @@ static long config_file_ftell(struct config_source *conf)\n static int config_buf_fgetc(struct config_source *conf)\n {\n \tif (conf->u.buf.pos < conf->u.buf.len)\n-\t\treturn conf->u.buf.buf[conf->u.buf.pos++];\n+\t\treturn (unsigned char)conf->u.buf.buf[conf->u.buf.pos++];\n \n \treturn EOF;\n }\n@@ -101,7 +101,7 @@ static int config_buf_ungetc(int c, struct config_source *conf)\n {\n \tif (conf->u.buf.pos > 0) {\n \t\tconf->u.buf.pos--;\n-\t\tif (conf->u.buf.buf[conf->u.buf.pos] != c)\n+\t\tif ((unsigned char)conf->u.buf.buf[conf->u.buf.pos] != c)\n \t\t\tBUG(\"config_buf can only ungetc the same character\");\n \t\treturn c;\n \t}\ndiff --git a/t/t1307-config-blob.sh b/t/t1307-config-blob.sh\nindex 5cb546dd00..e2c0883350 100755\n--- a/t/t1307-config-blob.sh\n+++ b/t/t1307-config-blob.sh\n@@ -72,6 +72,27 @@ test_expect_success 'can parse blob ending with CR' '\n \ttest_cmp expect actual\n '\n \n+test_expect_success 'a byte 0xff does not end a blob' '\n+\ttest_commit --printf FF config \"# caf\\\\377\\\\n[some]\\\\n\\\\tkey = value\\\\n\" &&\n+\techo value >expect &&\n+\tgit config --blob=HEAD:config some.key >actual &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success 'a byte 0xff after a CR in a blob' '\n+\ttest_commit --printf CRFF config \"# a\\\\r\\\\377\\\\n[some]\\\\n\\\\tkey = value\\\\n\" &&\n+\techo value >expect &&\n+\tgit config --blob=HEAD:config some.key >actual &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success 'can parse blob starting with a byte order mark' '\n+\ttest_commit --printf BOM config \"\\\\357\\\\273\\\\277[some]\\\\n\\\\tkey = value\\\\n\" &&\n+\techo value >expect &&\n+\tgit config --blob=HEAD:config some.key >actual &&\n+\ttest_cmp expect actual\n+'\n+\n test_expect_success 'config --blob outside of a repository is an error' '\n \tnongit test_must_fail git config --blob=foo --list\n '\n\nbase-commit: a018953688f1b10bddf91bff8747068f5f4746a4\n-- \n2.43.0\n\n\n"},{"id":"555464","messageId":"xmqq1p9l4dh4.fsf@gitster.g","threadId":"66576","inReplyTo":"20261018152426.2716899-1-olof.fairbrook@gmail.com","subject":"Re: [PATCH] config: read the bytes of a blob as unsigned characters","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-10-19T20:55:35Z","receivedAt":"2026-10-19T20:55:35Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Olof Fairbrook <olof.fairbrook@gmail.com> writes:\n\n> \"git config --blob\" and the other readers of config from memory get\n> each byte from config_buf_fgetc(), which returns it as a plain \"char\".\n> Where \"char\" is signed, as on x86, a byte 0xff comes back as -1, which\n> is EOF, and any byte from 0x80 up as a negative number.  So the parser\n> stops at a byte 0xff and silently ignores the rest of the blob:\n>\n>   $ printf '# caf\\377\\n[some]\\n\\tkey = value\\n' >config\n>   $ git config --blob=$(git hash-object -w config) --list\n>   $\n>\n> and a blob that starts with a UTF-8 byte order mark is a \"bad config\n> line 1\", because the code that skips the mark compares with unsigned\n> bytes.  A config file has neither problem, as getc() returns unsigned\n> bytes.  \".gitmodules\" read from a commit goes through the same reader.\n\nNicely explained, even though it is a tad overly verbose side\n(especially given that the example code is something we can see in\nthe added test easily).\n\n> This has been so since config_buf_fgetc() was added in 1bc888193e\n> (teach config --blob option to parse config from database,\n> 2013-07-12).\n>\n> Return the byte as an unsigned char, as getc() does.\n> config_buf_ungetc() checks that it is given back the byte it read; it\n> now compares with the byte as an unsigned char too, or a CR followed\n> by such a byte would hit its BUG().\n\nVery nice.  Will queue as-is.\n\n> diff --git a/t/t1307-config-blob.sh b/t/t1307-config-blob.sh\n> index 5cb546dd00..e2c0883350 100755\n> --- a/t/t1307-config-blob.sh\n> +++ b/t/t1307-config-blob.sh\n> @@ -72,6 +72,27 @@ test_expect_success 'can parse blob ending with CR' '\n>  \ttest_cmp expect actual\n>  '\n>  \n> +test_expect_success 'a byte 0xff does not end a blob' '\n> +\ttest_commit --printf FF config \"# caf\\\\377\\\\n[some]\\\\n\\\\tkey = value\\\\n\" &&\n> +\techo value >expect &&\n> +\tgit config --blob=HEAD:config some.key >actual &&\n> +\ttest_cmp expect actual\n> +'\n> +\n> +test_expect_success 'a byte 0xff after a CR in a blob' '\n> +\ttest_commit --printf CRFF config \"# a\\\\r\\\\377\\\\n[some]\\\\n\\\\tkey = value\\\\n\" &&\n> +\techo value >expect &&\n> +\tgit config --blob=HEAD:config some.key >actual &&\n> +\ttest_cmp expect actual\n> +'\n> +\n> +test_expect_success 'can parse blob starting with a byte order mark' '\n> +\ttest_commit --printf BOM config \"\\\\357\\\\273\\\\277[some]\\\\n\\\\tkey = value\\\\n\" &&\n> +\techo value >expect &&\n> +\tgit config --blob=HEAD:config some.key >actual &&\n> +\ttest_cmp expect actual\n> +'\n> +\n>  test_expect_success 'config --blob outside of a repository is an error' '\n>  \tnongit test_must_fail git config --blob=foo --list\n>  '\n>\n> base-commit: a018953688f1b10bddf91bff8747068f5f4746a4\n\n"}]}