Skip to content

Commit de05640

Browse files
Petr Baudisspearce
authored andcommitted
config.c: Tolerate UTF8 BOM at the beginning of config file
Unfortunately, the abomination of Windows Notepad likes to scatted non-sensical UTF8 BOM marks across text files it edits. This is especially troublesome when editing the Git configuration file, and it does not appear to be particularly harmful to teach Git to deal with this poo in the configfile. Signed-off-by: Petr Baudis <petr.baudis@novartis.com> Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
1 parent 25dfd17 commit de05640

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

config.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,8 +205,27 @@ static int git_parse_file(config_fn_t fn, void *data)
205205
int baselen = 0;
206206
static char var[MAXNAME];
207207

208+
/* U+FEFF Byte Order Mark in UTF8 */
209+
static const unsigned char *utf8_bom = (unsigned char *) "\xef\xbb\xbf";
210+
const unsigned char *bomptr = utf8_bom;
211+
208212
for (;;) {
209213
int c = get_next_char();
214+
if (bomptr && *bomptr) {
215+
/* We are at the file beginning; skip UTF8-encoded BOM
216+
* if present. Sane editors won't put this in on their
217+
* own, but e.g. Windows Notepad will do it happily. */
218+
if ((unsigned char) c == *bomptr) {
219+
bomptr++;
220+
continue;
221+
} else {
222+
/* Do not tolerate partial BOM. */
223+
if (bomptr != utf8_bom)
224+
break;
225+
/* No BOM at file beginning. Cool. */
226+
bomptr = NULL;
227+
}
228+
}
210229
if (c == '\n') {
211230
if (config_file_eof)
212231
return 0;

0 commit comments

Comments
 (0)