Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix a crash with std::regex_match during code analysis #12285

Closed
Lablace opened this issue May 6, 2024 · 7 comments
Closed

Fix a crash with std::regex_match during code analysis #12285

Lablace opened this issue May 6, 2024 · 7 comments
Assignees
Labels
bug Feature: Code Analysis Related to integration with clang-tidy, cppcheck, cl.exe /analyze, etc. fixed Check the Milestone for the release in which the fix is or will be available. reliability verified Bug has been reproduced
Milestone

Comments

@Lablace
Copy link

Lablace commented May 6, 2024

Environment

  • OS and Version: macOS Sonoma 14.4.1
  • VS Code Version: 1.89.0
  • C/C++ Extension Version: v1.19.9
  • If using SSH remote, specify OS of remote machine: Ubuntu 22.04.1 (Kernel: 5.15.0)

Bug Summary and Steps to Reproduce

Bug Summary:

When clang-tidy is enabled without tweaking check list, when diagnostic message from it is too long (in below case about 8 wrapped lines in debug output), the extension crashes.

Steps to Reproduce:

  • Use the Python code below (at the end section) to generate a main.cpp.
  • Open the main.cpp
  • Extension crashes.

Expected behavior:

I don't know if vscode have length limitation for diagnostic messages, if not, the extension should work well and display the message intactly, if that's the case, the extension should (at least I think it should) truncate the message and indicate user that the original message is too long to display. Anyway, I think the extension should not crash.

Configuration and Logs

TEMPDIR/cpp-clang-analyzer-optin-padding-mre/main.cpp:1:7: warning: Excessive padding in 'class Sample' (48 padding bytes, where 0 is optimal). Optimal fields order: dvar_000, dvar_001, dvar_002, dvar_003, dvar_004, dvar_005, dvar_006, dvar_007, dvar_008, dvar_009, dvar_00a, dvar_00b, dvar_00c, dvar_00d, dvar_00e, dvar_00f, dvar_010, dvar_011, dvar_012, dvar_013, dvar_014, dvar_015, dvar_016, dvar_017, dvar_018, dvar_019, dvar_01a, dvar_01b, dvar_01c, dvar_01d, dvar_01e, dvar_01f, dvar_020, dvar_021, dvar_022, dvar_023, dvar_024, dvar_025, dvar_026, dvar_027, dvar_028, dvar_029, dvar_02a, dvar_02b, dvar_02c, dvar_02d, dvar_02e, dvar_02f, dvar_030, dvar_031, dvar_032, dvar_033, dvar_034, dvar_035, dvar_036, dvar_037, dvar_038, dvar_039, dvar_03a, dvar_03b, dvar_03c, dvar_03d, dvar_03e, dvar_03f, dvar_040, dvar_041, dvar_042, dvar_043, dvar_044, dvar_045, dvar_046, dvar_047, dvar_048, dvar_049, dvar_04a, dvar_04b, dvar_04c, dvar_04d, dvar_04e, dvar_04f, dvar_050, dvar_051, 
dvar_052, dvar_053, dvar_054, dvar_055, dvar_056, dvar_057, dvar_058, dvar_059, dvar_05a, dvar_05b, dvar_05c, dvar_05d, dvar_05e, dvar_05f, dvar_060, dvar_061, dvar_062, dvar_063, dvar_064, dvar_065, dvar_066, dvar_067, dvar_068, dvar_069, dvar_06a, dvar_06b, dvar_06c, dvar_06d, dvar_06e, dvar_06f, dvar_070, dvar_071, dvar_072, dvar_073, dvar_074, dvar_075, dvar_076, dvar_077, dvar_078, dvar_079, dvar_07a, dvar_07b, dvar_07c, dvar_07d, dvar_07e, dvar_07f, dvar_080, dvar_081, dvar_082, dvar_083, dvar_084, dvar_085, dvar_086, dvar_087, dvar_088, dvar_089, dvar_08a, dvar_08b, dvar_08c, dvar_08d, dvar_08e, dvar_08f, dvar_090, dvar_091, dvar_092, dvar_093, dvar_094, dvar_095, dvar_096, dvar_097, dvar_098, dvar_099, dvar_09a, dvar_09b, dvar_09c, dvar_09d, dvar_09e, dvar_09f, dvar_0a0, dvar_0a1, dvar_0a2, dvar_0a3, dvar_0a4, dvar_0a5, dvar_0a6, dvar_0a7, dvar_0a8, dvar_0a9, dvar_0aa, dvar_0ab, dvar_0ac, dvar_0ad, dvar_0ae, dvar_0af, dvar_0b0, dvar_0b1, dvar_0b2, dvar_0b3, dvar_0b4, dvar_0b5, 
dvar_0b6, dvar_0b7, dvar_0b8, dvar_0b9, dvar_0ba, dvar_0bb, dvar_0bc, dvar_0bd, dvar_0be, dvar_0bf, dvar_0c0, dvar_0c1, dvar_0c2, dvar_0c3, dvar_0c4, dvar_0c5, dvar_0c6, dvar_0c7, dvar_0c8, dvar_0c9, dvar_0ca, dvar_0cb, dvar_0cc, dvar_0cd, dvar_0ce, dvar_0cf, dvar_0d0, dvar_0d1, dvar_0d2, dvar_0d3, dvar_0d4, dvar_0d5, dvar_0d6, dvar_0d7, dvar_0d8, dvar_0d9, dvar_0da, dvar_0db, dvar_0dc, dvar_0dd, dvar_0de, dvar_0df, dvar_0e0, dvar_0e1, dvar_0e2, dvar_0e3, dvar_0e4, dvar_0e5, dvar_0e6, dvar_0e7, dvar_0e8, dvar_0e9, dvar_0ea, dvar_0eb, dvar_0ec, dvar_0ed, dvar_0ee, dvar_0ef, dvar_0f0, dvar_0f1, dvar_0f2, dvar_0f3, dvar_0f4, dvar_0f5, dvar_0f6, dvar_0f7, dvar_0f8, dvar_0f9, dvar_0fa, dvar_0fb, dvar_0fc, dvar_0fd, dvar_0fe, dvar_0ff, dvar_100, dvar_101, dvar_102, dvar_103, dvar_104, dvar_105, dvar_106, dvar_107, dvar_108, dvar_109, dvar_10a, dvar_10b, dvar_10c, dvar_10d, dvar_10e, dvar_10f, dvar_110, dvar_111, dvar_112, dvar_113, dvar_114, dvar_115, dvar_116, dvar_117, dvar_118, dvar_119, 
dvar_11a, dvar_11b, dvar_11c, dvar_11d, dvar_11e, dvar_11f, dvar_120, dvar_121, dvar_122, dvar_123, dvar_124, dvar_125, dvar_126, dvar_127, dvar_128, dvar_129, dvar_12a, dvar_12b, dvar_12c, dvar_12d, dvar_12e, dvar_12f, dvar_130, dvar_131, dvar_132, dvar_133, dvar_134, dvar_135, dvar_136, dvar_137, dvar_138, dvar_139, dvar_13a, dvar_13b, dvar_13c, dvar_13d, dvar_13e, dvar_13f, dvar_140, dvar_141, dvar_142, dvar_143, dvar_144, dvar_145, dvar_146, dvar_147, dvar_148, dvar_149, dvar_14a, dvar_14b, dvar_14c, dvar_14d, dvar_14e, dvar_14f, dvar_150, dvar_151, dvar_152, dvar_153, dvar_154, dvar_155, dvar_156, dvar_157, dvar_158, dvar_159, dvar_15a, dvar_15b, dvar_15c, dvar_15d, dvar_15e, dvar_15f, dvar_160, dvar_161, dvar_162, dvar_163, dvar_164, dvar_165, dvar_166, dvar_167, dvar_168, dvar_169, dvar_16a, dvar_16b, dvar_16c, dvar_16d, dvar_16e, dvar_16f, dvar_170, dvar_171, dvar_172, dvar_173, dvar_174, dvar_175, dvar_176, dvar_177, dvar_178, dvar_179, dvar_17a, dvar_17b, dvar_17c, dvar_17d, 
dvar_17e, dvar_17f, ivar_001_000, ivar_002_000, ivar_002_001, ivar_003_000, ivar_003_001, ivar_003_002, ivar_004_000, ivar_004_001, ivar_004_002, ivar_004_003, ivar_005_000, ivar_005_001, ivar_005_002, ivar_005_003, ivar_005_004, ivar_006_000, ivar_006_001, ivar_006_002, ivar_006_003, ivar_006_004, ivar_006_005, ivar_007_000, ivar_007_001, ivar_007_002, ivar_007_003, ivar_007_004, ivar_007_005, ivar_007_006, ivar_008_000, ivar_008_001, ivar_008_002, ivar_008_003, ivar_008_004, ivar_008_005, ivar_008_006, ivar_008_007, ivar_009_000, ivar_009_001, ivar_009_002, ivar_009_003, ivar_009_004, ivar_009_005, ivar_009_006, ivar_009_007, ivar_009_008, ivar_00a_000, ivar_00a_001, ivar_00a_002, ivar_00a_003, ivar_00a_004, ivar_00a_005, ivar_00a_006, ivar_00a_007, ivar_00a_008, ivar_00a_009, ivar_00b_000, ivar_00b_001, ivar_00b_002, ivar_00b_003, ivar_00b_004, ivar_00b_005, ivar_00b_006, ivar_00b_007, ivar_00b_008, ivar_00b_009, ivar_00b_00a, ivar_00c_000, ivar_00c_001, ivar_00c_002, ivar_00c_003, 
ivar_00c_004, ivar_00c_005, ivar_00c_006, ivar_00c_007, ivar_00c_008, ivar_00c_009, ivar_00c_00a, ivar_00c_00b, ivar_00d_000, ivar_00d_001, ivar_00d_002, ivar_00d_003, ivar_00d_004, ivar_00d_005, ivar_00d_006, ivar_00d_007, ivar_00d_008, ivar_00d_009, ivar_00d_00a, ivar_00d_00b, ivar_00d_00c, ivar_00e_000, ivar_00e_001, ivar_00e_002, ivar_00e_003, ivar_00e_004, ivar_00e_005, ivar_00e_006, ivar_00e_007, ivar_00e_008, ivar_00e_009, ivar_00e_00a, ivar_00e_00b, ivar_00e_00c, ivar_00e_00d, ivar_00f_000, ivar_00f_001, ivar_00f_002, ivar_00f_003, ivar_00f_004, ivar_00f_005, ivar_00f_006, ivar_00f_007, ivar_00f_008, ivar_00f_009, ivar_00f_00a, ivar_00f_00b, ivar_00f_00c, ivar_00f_00d, ivar_00f_00e, ivar_010_000, ivar_010_001, ivar_010_002, ivar_010_003, ivar_010_004, ivar_010_005, ivar_010_006, ivar_010_007, ivar_010_008, ivar_010_009, ivar_010_00a, ivar_010_00b, ivar_010_00c, ivar_010_00d, ivar_010_00e, ivar_010_00f, ivar_011_000, ivar_011_001, ivar_011_002, ivar_011_003, ivar_011_004, 
ivar_011_005, ivar_011_006, ivar_011_007, ivar_011_008, ivar_011_009, ivar_011_00a, ivar_011_00b, ivar_011_00c, ivar_011_00d, ivar_011_00e, ivar_011_00f, ivar_011_010, ivar_012_000, ivar_012_001, ivar_012_002, ivar_012_003, ivar_012_004, ivar_012_005, ivar_012_006, ivar_012_007, ivar_012_008, ivar_012_009, ivar_012_00a, ivar_012_00b, ivar_012_00c, ivar_012_00d, ivar_012_00e, ivar_012_00f, ivar_012_010, ivar_012_011, ivar_013_000, ivar_013_001, ivar_013_002, ivar_013_003, ivar_013_004, ivar_013_005, ivar_013_006, ivar_013_007, ivar_013_008, ivar_013_009, ivar_013_00a, ivar_013_00b, ivar_013_00c, ivar_013_00d, ivar_013_00e, ivar_013_00f, ivar_013_010, ivar_013_011, ivar_013_012, ivar_014_000, ivar_014_001, ivar_014_002, ivar_014_003, ivar_014_004, ivar_014_005, ivar_014_006, ivar_014_007, ivar_014_008, ivar_014_009, ivar_014_00a, ivar_014_00b, ivar_014_00c, ivar_014_00d, ivar_014_00e, ivar_014_00f, ivar_014_010, ivar_014_011, ivar_014_012, ivar_014_013, ivar_015_000, ivar_015_001, 
ivar_015_002, ivar_015_003, ivar_015_004, ivar_015_005, ivar_015_006, ivar_015_007, ivar_015_008, ivar_015_009, ivar_015_00a, ivar_015_00b, ivar_015_00c, ivar_015_00d, ivar_015_00e, ivar_015_00f, ivar_015_010, ivar_015_011, ivar_015_012, ivar_015_013, ivar_015_014, ivar_016_000, ivar_016_001, ivar_016_002, ivar_016_003, ivar_016_004, ivar_016_005, ivar_016_006, ivar_016_007, ivar_016_008, ivar_016_009, ivar_016_00a, ivar_016_00b, ivar_016_00c, ivar_016_00d, ivar_016_00e, ivar_016_00f, ivar_016_010, ivar_016_011, ivar_016_012, ivar_016_013, ivar_016_014, ivar_016_015, ivar_017_000, ivar_017_001, ivar_017_002, ivar_017_003, ivar_017_004, ivar_017_005, ivar_017_006, ivar_017_007, ivar_017_008, ivar_017_009, ivar_017_00a, ivar_017_00b, ivar_017_00c, ivar_017_00d, ivar_017_00e, ivar_017_00f, ivar_017_010, ivar_017_011, ivar_017_012, ivar_017_013, ivar_017_014, ivar_017_015, ivar_017_016, ivar_018_000, ivar_018_001, ivar_018_002, ivar_018_003, ivar_018_004, ivar_018_005, ivar_018_006, 
ivar_018_007, ivar_018_008, ivar_018_009, ivar_018_00a, ivar_018_00b, ivar_018_00c, ivar_018_00d, ivar_018_00e, ivar_018_00f, ivar_018_010, ivar_018_011, ivar_018_012, ivar_018_013, ivar_018_014, ivar_018_015, ivar_018_016, ivar_018_017, consider reordering the fields or adding explicit padding members [clang-analyzer-optin.performance.Padding]

Other Extensions

No response

Additional context

I use this simple Python code to generate a main.cpp, with which clang-tidy applies clang-analyzer-optin.performance.Padding and emits warning: Excessive padding in 'class Sample', along with suggested order (which is too long).

with open("main.cpp", mode="w", encoding="utf8") as f:
    f.write(
        r"class Sample {" + "\n",
    )

    j = 0
    for i in range(384):
        f.write(f"    double dvar_{i:>03x};\n")
        if i % 16 == 0:
            j += 1
            for k in range(j):
                f.write(f"    int ivar_{j:>03x}_{k:>03x} = 0;\n")

    f.write(r"};")

I tried replacing 384 with 256 and this time the extension displayed the diagnostics normally.

I also tried manually disabling the clang-analyzer-optin.performance.Padding check in workspace setting with "C_Cpp.codeAnalysis.clangTidy.checks.disabled": ["clang-analyzer-optin.performance.Padding"], and the extension worked normally too.

@sean-mcmanus sean-mcmanus self-assigned this May 6, 2024
@sean-mcmanus sean-mcmanus added bug reliability verified Bug has been reproduced Feature: Code Analysis Related to integration with clang-tidy, cppcheck, cl.exe /analyze, etc. labels May 6, 2024
@sean-mcmanus
Copy link
Contributor

@Lablace Thanks for reporting this. It repros on Linux but not Windows.

@sean-mcmanus
Copy link
Contributor

sean-mcmanus commented May 6, 2024

@Lablace It's crashing due to a call to std::regex_match with the gcc 11.4 implementation, which seems to rely on some 256 bitset internally:
image

It doesn't repro with clang/msvc's implementation of std::regex_match so it looks like a gcc bug. In fact, it's fixed if we use gcc 12 instead, but due to our usage of the Linux musl implementation I'm not sure if we can upgrade or not yet.

@sean-mcmanus sean-mcmanus assigned Colengms and unassigned sean-mcmanus May 6, 2024
@sean-mcmanus sean-mcmanus changed the title Extension crashes when diagnostic message is long Upgrade to gcc 12 libs to fix a crash with std::regex_match May 6, 2024
@sean-mcmanus sean-mcmanus added this to the 1.21 milestone May 6, 2024
@sean-mcmanus sean-mcmanus changed the title Upgrade to gcc 12 libs to fix a crash with std::regex_match Upgrade to gcc 12+ libs to fix a crash with std::regex_match May 6, 2024
@Lablace
Copy link
Author

Lablace commented May 7, 2024

Thanks for your detailed clarification, I had never thought this would have something to do with GCC. Let me know if I could offer more help, and feel free to close this if you like or need.

@sean-mcmanus sean-mcmanus changed the title Upgrade to gcc 12+ libs to fix a crash with std::regex_match Increase thread stack size to fix a crash with std::regex_match May 7, 2024
@sean-mcmanus
Copy link
Contributor

@Lablace It's actually a known long-standing issue with the gcc regex implementation that uses the stack too much: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=61601 . It's fixed after we increase the thread stack size.

@sean-mcmanus sean-mcmanus assigned sean-mcmanus and unassigned Colengms May 7, 2024
@sean-mcmanus sean-mcmanus changed the title Increase thread stack size to fix a crash with std::regex_match Increase the Linux thread stack size to fix a crash with std::regex_match May 7, 2024
@sean-mcmanus sean-mcmanus modified the milestones: 1.21, 1.21.0 May 7, 2024
@Lablace
Copy link
Author

Lablace commented May 8, 2024

It just hit my mind that increasing the stack size would solve THIS KIND of problem (like cases when I increase the range to 512 or 1024 in the Python code, though one perhaps should not use a class to store such things) or just the specific case (range is 384)?

@sean-mcmanus
Copy link
Contributor

@Lablace Yeah, you're right. Increasing the stack size isn't really a good fix since it'll crash again with a bigger string. I'll see if I can modify the regex or do some other string manipulation to not make it crash (i.e. limit the size of the matching it's trying to do).

@sean-mcmanus sean-mcmanus changed the title Increase the Linux thread stack size to fix a crash with std::regex_match Fix a crash with std::regex_match during code analysis May 9, 2024
@sean-mcmanus sean-mcmanus added the fixed Check the Milestone for the release in which the fix is or will be available. label Jun 13, 2024
@bobbrow bobbrow added this to cpptools Jun 17, 2024
@bobbrow bobbrow moved this to Done in cpptools Jun 17, 2024
@bobbrow bobbrow modified the milestones: 1.21.0, 1.21 Jun 17, 2024
@sean-mcmanus
Copy link
Contributor

@Lablace Fixed with https://github.com/microsoft/vscode-cpptools/releases/tag/v1.21.0 . We changed the regex used so that it wouldn't stack overflow.

@github-actions github-actions bot locked and limited conversation to collaborators Sep 6, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Feature: Code Analysis Related to integration with clang-tidy, cppcheck, cl.exe /analyze, etc. fixed Check the Milestone for the release in which the fix is or will be available. reliability verified Bug has been reproduced
Projects
Status: Done
Development

No branches or pull requests

4 participants