Skip to content

Commit 333a821

Browse files
dmcppcheckclaude
andcommitted
Drop a dangling last byte in UTF-16 input, add tests from review
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 3530830 commit 333a821

2 files changed

Lines changed: 21 additions & 1 deletion

File tree

‎simplecpp.cpp‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -378,8 +378,11 @@ class simplecpp::TokenList::Stream {
378378
// For UTF-16 encoded files the BOM is 0xfeff/0xfffe. If the
379379
// character is non-ASCII character then replace it with 0xff
380380
const int ch2 = get();
381-
if (ch2 == EOF)
381+
if (ch2 == EOF) {
382+
// a dangling last byte is dropped
382383
inputEnd = true;
384+
return EOF;
385+
}
383386
const int ch16 = makeUtf16Char(static_cast<unsigned char>(ch), static_cast<unsigned char>(ch2));
384387
return (ch16 >= 0x80) ? 0xff : ch16;
385388
}

‎test.cpp‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3981,6 +3981,11 @@ static void readfile_char_error()
39813981

39823982
readfile("A = 's\n'", {}, &outputList);
39833983
ASSERT_EQUALS("file0,1,syntax_error,No pair for character (\'). Can't process file. File is either invalid or unicode, which is currently not supported.\n", toString(outputList));
3984+
outputList.clear();
3985+
3986+
// the ' at the end of the file is not dropped after the peek for a digit separator
3987+
readfile("A = 1'", {}, &outputList);
3988+
ASSERT_EQUALS("file0,1,syntax_error,No pair for character (\'). Can't process file. File is either invalid or unicode, which is currently not supported.\n", toString(outputList));
39843989
}
39853990

39863991
static void readfile_string()
@@ -4386,6 +4391,18 @@ static void unicode()
43864391
const char code[] = "\xff\xfe\x0d\x00\x0a\x00\x2f\x00\x2f\x00\x31\x00\x0d\x00\x0a\x00";
43874392
ASSERT_EQUALS("\n//1", readfile(code, sizeof(code)-1));
43884393
}
4394+
{
4395+
// CR newlines
4396+
const char code[] = "\xff\xfe\x78\x00\x0d\x00\x79\x00\x0d\x00";
4397+
ASSERT_EQUALS("x\ny", readfile(code, sizeof(code)-1));
4398+
}
4399+
{
4400+
// odd number of bytes, the dangling last byte is dropped
4401+
const char code[] = "\xff\xfe\x31\x00\x0a";
4402+
simplecpp::OutputList outputList;
4403+
ASSERT_EQUALS("1", readfile(code, sizeof(code)-1, {}, &outputList));
4404+
ASSERT_EQUALS("file0,1,portability_no_eof_newline,No newline at end of file is undefined behavior in C.\n", toString(outputList));
4405+
}
43894406
}
43904407

43914408
static void unicode_invalid()

0 commit comments

Comments
 (0)