Title: Infinite loop with unbounded memory on a 4-byte UTF-16 file that ends in an identifier character · Issue #693 · cppcheck-opensource/simplecpp · GitHub
Open Graph Title: Infinite loop with unbounded memory on a 4-byte UTF-16 file that ends in an identifier character · Issue #693 · cppcheck-opensource/simplecpp
X Title: Infinite loop with unbounded memory on a 4-byte UTF-16 file that ends in an identifier character · Issue #693 · cppcheck-opensource/simplecpp
Description: Summary A 4-byte file consisting of a UTF-16 BOM plus one identifier character, with no newline after it, makes simplecpp loop forever at ~100 % CPU while allocating without bound. $ printf '\xFF\xFE\x41\x00' > poc.c # UTF-16 LE BOM + 'A...
Open Graph Description: Summary A 4-byte file consisting of a UTF-16 BOM plus one identifier character, with no newline after it, makes simplecpp loop forever at ~100 % CPU while allocating without bound. $ printf '\xFF\x...
X Description: Summary A 4-byte file consisting of a UTF-16 BOM plus one identifier character, with no newline after it, makes simplecpp loop forever at ~100 % CPU while allocating without bound. $ printf '\x...
Opengraph URL: https://github.com/cppcheck-opensource/simplecpp/issues/693
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Infinite loop with unbounded memory on a 4-byte UTF-16 file that ends in an identifier character","articleBody":"\n\n## Summary\n\nA 4-byte file consisting of a UTF-16 BOM plus one identifier character, with no newline after\nit, makes `simplecpp` loop forever at ~100 % CPU while allocating without bound.\n\n```console\n$ printf '\\xFF\\xFE\\x41\\x00' > poc.c # UTF-16 LE BOM + 'A'\n$ ./simplecpp poc.c\n (never returns)\n```\n\nMeasured on the standalone `simplecpp` binary, sampling `/proc/\u003cpid>/status`:\n\n```\nt= 20s rss=0.31 GB cpu=100%\nt= 60s rss=0.78 GB cpu= 95%\nt=120s rss=1.63 GB cpu= 97%\nt=180s rss=2.39 GB cpu= 98% (still climbing linearly, ~13 MB/s)\n```\n\nThis is adjacent to #637 / #636 but is a different path: #637's exact bytes\n(`\\xFF\\xFE\\x00\\x3B`) are fixed and complete instantly on current master; the case above does not.\n\n## Version\n\nReproduced on `master` at `f420e61` (\"Fix #688: Support `#elifdef` and `#elifndef` (#691)\",\n2026-08-03), built exactly as the README describes, with no sanitizers and no fuzzing engine:\n\n```console\n$ g++ -std=c++11 -O2 -o simplecpp main.cpp simplecpp.cpp\n```\n\nHost: Linux x86-64, GCC.\n\n## Trigger matrix\n\n| bytes | meaning | result |\n| --- | --- | --- |\n| `FF FE 41 00` | UTF-16 LE BOM + `A` | **hang** |\n| `FE FF 00 41` | UTF-16 BE BOM + `A` | **hang** |\n| `FF FE 24 00` | UTF-16 LE BOM + `$` | **hang** |\n| `FE FF 00 3B` | UTF-16 BE BOM + `;` | ok |\n| `FF FE 41 00 00 00` | LE BOM + `A` + NUL | ok |\n| `FF FE 41 00 0A 00` | LE BOM + `A` + newline | ok |\n| `41` | plain UTF-8 `A`, no BOM | ok |\n| `FF FE 00 3B` | the bytes from #637 | ok (fixed) |\n| `FF FE` | BOM only | ok |\n\nSo it needs all three of: a UTF-16 BOM, a final character that continues an identifier, and\nnothing after it. A trailing newline avoids it, which is presumably why ordinary files do not\nhit this.\n\n## Cause\n\n`FileStream::unget_internal()` rewinds unconditionally in the UTF-16 branch:\n\n```cpp\nvoid unget_internal(int ch) {\n if (isUtf16) {\n // TODO: use ungetc() as well\n // UTF-16 has subsequent unget() calls\n fseek(file, -1, SEEK_CUR);\n } else {\n ungetc(ch, file);\n }\n}\n```\n\n`FileStream::peek()` calls `fgetc()` and then hands the result straight to `unget_internal()`:\n\n```cpp\nint peek() override {\n // keep lastCh intact\n const int ch = fgetc(file);\n unget_internal(ch);\n return ch;\n}\n```\n\nAt end of file that `fgetc()` consumes nothing and returns `EOF`, but the `fseek(-1)` still runs,\nso every `peek()` past the end moves the read position one byte *backwards*. The reader can then\nnever reach the end, and `readfile()` keeps appending tokens — hence both the spin and the memory\ngrowth. The `ungetc()` branch does not have the problem because `ungetc(EOF, …)` is a no-op.\n\n## Possible fix\n\nSkipping the rewind when nothing was consumed fixes every case in the table above:\n\n```diff\n--- a/simplecpp.cpp\n+++ b/simplecpp.cpp\n@@ -463,7 +463,8 @@\n if (isUtf16) {\n // TODO: use ungetc() as well\n // UTF-16 has subsequent unget() calls\n- fseek(file, -1, SEEK_CUR);\n+ if (ch != EOF)\n+ fseek(file, -1, SEEK_CUR);\n } else {\n ungetc(ch, file);\n }\n```\n\nWith that applied, `make testrunner && ./testrunner` still exits 0, and all nine inputs in the\ntable behave as \"ok\".\n\nI have not checked whether this is the fix you would prefer — you may want to handle it at the\ncall site instead. While reading that code I noticed something adjacent that looks unintended,\nin `Stream::peekChar()`:\n\n```cpp\nif (isUtf16) {\n (void)get();\n const auto ch2 = static_cast\u003cunsigned char>(peek()); // EOF (-1) becomes 0xFF here\n unget();\n```\n\nthe second `peek()` is cast to `unsigned char` without an `EOF` check, so a truncated final\nUTF-16 unit is silently read as `0xFF`. That is not what makes the loop above spin — the\n`unget_internal` change alone is enough — but it may be worth a look.\n\n## Also affects cppcheck\n\ncppcheck vendors this file in `externals/simplecpp`, and the same input reaches it through\n`CppCheck::check(FileWithDetails)` → `simplecpp::TokenList::TokenList(filename, …)`, which is the\npath `cppcheck \u003cfile>` uses. I observed the identical hang there (via the OSS-Fuzz `cppcheck`\ntarget, ASan build): 100 % CPU with RSS at 4.9 GB after 288 s on a 68-byte UTF-16 file that ends\nin `M`. The standalone reproducer above is self-contained, so nothing in this report depends on\nthat build.\n\n## How it was found\n\nAutomated fuzzing of cppcheck; the input was then minimised by hand to the 4 bytes above and\nre-verified against upstream `simplecpp` master, so the reproducer involves no fuzzing harness.\n","author":{"url":"https://github.com/CrJyyy","@type":"Person","name":"CrJyyy"},"datePublished":"2026-08-04T06:23:36Z","url":"https://github.com/cppcheck-opensource/simplecpp/issues/693"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:00e6c6d7-6daa-dad7-cb35-cc0f8104f2be |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | C38E:2DF45D:205AC1:2CC0BF:6AFC4ABA |
| html-safe-nonce | 373db572bc4da8970aa4fa0765d7675b47ab3401abebef4ac4196635fb426bab |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDMzhFOjJERjQ1RDoyMDVBQzE6MkNDMEJGOjZBRkM0QUJBIiwidmlzaXRvcl9pZCI6IjgyMzEwMjcxNzIxMjMxMDE4ODIiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | e41afc3eafd52b6822def201a920c30f00bbb179b1392d3e50438d291fb62029 |
| hovercard-subject-tag | issue:5057409772 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/cppcheck-opensource/simplecpp/693/issue_layout |
| twitter:image | https://opengraph.githubassets.com/6a0464acbda4de62ed5a59ce5ec51b1ca17f03bde2b8af9c4a0cca67a117f64e/cppcheck-opensource/simplecpp/issues/693 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/6a0464acbda4de62ed5a59ce5ec51b1ca17f03bde2b8af9c4a0cca67a117f64e/cppcheck-opensource/simplecpp/issues/693 |
| og:image:alt | Summary A 4-byte file consisting of a UTF-16 BOM plus one identifier character, with no newline after it, makes simplecpp loop forever at ~100 % CPU while allocating without bound. $ printf '\xFF\x... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | CrJyyy |
| hostname | github.com |
| expected-hostname | github.com |
| None | 20dfdada12bc00bbf8065dc10dfe3ff3711b85db5cc117f83296f8074e861960 |
| turbo-cache-control | no-preview |
| go-import | github.com/cppcheck-opensource/simplecpp git https://github.com/cppcheck-opensource/simplecpp.git |
| octolytics-dimension-user_id | 278097703 |
| octolytics-dimension-user_login | cppcheck-opensource |
| octolytics-dimension-repository_id | 33434464 |
| octolytics-dimension-repository_nwo | cppcheck-opensource/simplecpp |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 33434464 |
| octolytics-dimension-repository_network_root_nwo | cppcheck-opensource/simplecpp |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 041909865f220ab51714e7efdc276dd349a99ade |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width