This is an archive of the discontinued LLVM Phabricator instance.

[clang-format] Don't crash on malformed preprocessor conditions
ClosedPublic

Authored by sstwcw on Oct 14 2022, 10:22 AM.

Details

Summary

Previously the program would crash on this input:

#else
#if X

The problem was that in parsePPElse, PPBranchLevel would be
incremented when the first #else gets parsed, but PPLevelBranchCount
and PPLevelBranchIndex would not be updated together.

I found the problem when working on D135740.

Diff Detail

Event Timeline

sstwcw created this revision.Oct 14 2022, 10:22 AM
Herald added a project: Restricted Project. · View Herald TranscriptOct 14 2022, 10:22 AM
sstwcw requested review of this revision.Oct 14 2022, 10:22 AM
Herald added a project: Restricted Project. · View Herald TranscriptOct 14 2022, 10:22 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript
sstwcw updated this revision to Diff 467832.Oct 14 2022, 10:32 AM
owenpan added inline comments.Oct 21 2022, 12:16 AM
clang/lib/Format/UnwrappedLineParser.cpp
1219

Nit.

clang/unittests/Format/FormatTest.cpp
5196–5206

Can we have individual verifyFormat or verifyNoCrashtests for easy reading and to avoid the overhead? Some examples:

#else
a;
#if X
b;
#endif
#endif

#elif X
a;
#endif
#ifdef X
b;
#endif
sstwcw updated this revision to Diff 470015.Oct 23 2022, 2:52 PM
sstwcw marked 2 inline comments as done.Oct 23 2022, 2:58 PM
sstwcw added inline comments.
clang/unittests/Format/FormatTest.cpp
5196–5206

I added some tests like the examples. But I kept the generated cases. I feel more secure that way. At first I only found the bug because of the generated cases. As for overhead, the debug build of this test took 1.6s on my laptop.

sstwcw marked an inline comment as done.Oct 23 2022, 3:01 PM
clang/lib/Format/UnwrappedLineParser.cpp
1220

You assert >= -1, that means this has to be == -1.

clang/unittests/Format/FormatTest.cpp
5196–5206

I added some tests like the examples. But I kept the generated cases. I feel more secure that way. At first I only found the bug because of the generated cases. As for overhead, the debug build of this test took 1.6s on my laptop.

Couldn't you just take the previously crashing ones, you find important, as test cases?
The overhead is not only compile and runtime, it's also while trying to understand what's happening here.

owenpan added inline comments.Oct 24 2022, 9:10 PM
clang/lib/Format/UnwrappedLineParser.cpp
1220

Yep, as I suggested above.

sstwcw updated this revision to Diff 470623.Oct 25 2022, 3:18 PM
sstwcw marked 3 inline comments as done.
sstwcw added inline comments.
clang/unittests/Format/FormatTest.cpp
5196–5206

The overhead is not only compile and runtime, it's also while trying to understand what's happening here.

I see.

sstwcw marked an inline comment as done.Oct 25 2022, 3:19 PM
owenpan accepted this revision.Oct 25 2022, 11:49 PM
owenpan added inline comments.
clang/unittests/Format/FormatTest.cpp
5209

#elif Y instead of #else?

This revision is now accepted and ready to land.Oct 25 2022, 11:49 PM
This revision was landed with ongoing or failed builds.Oct 29 2022, 7:22 PM
This revision was automatically updated to reflect the committed changes.