Solution skill
Semgrep not finding two lines of code with a 'patterns' section
Complete solution
Shows how to fix semgrep not finding two lines of code with a 'patterns' section. Use it when you hit this exact problem. Skip it when your error message or symptom looks different.
TL;DR
[bstpierre (top answer)] Using pattern-inside works: ` rules: - id: chat patterns: - pattern-inside: | var $X = GrpcChannel.ForAddress(...);... - pattern: var $Y = new ChatService.ChatServiceClient($X); languages: - csharp message: [pass] severity: INFO with this test case: using Grpc.Net.Client; using GrpcChat.ProtoBuf; var channel = GrpcChannel.ForAddress("YOUR\_HOST (port 8888)"); // ruleid: chat var client = new ChatService.ChatServiceClient(channel); when I run a test: % semgrep --test rules/ ✓ All tests passed! Context: Stack Overflow #71459777 (top answer, 4 votes, 1 answers)
Steps
- [bstpierre (top answer)] Using pattern-inside works:
``` using Grpc.Net.Client; using GrpcChat.ProtoBuf; var channel = GrpcChannel.ForAddress("YOUR_HOST (port 8888)"); // ruleid: chat var client = new ChatService.ChatServiceClient(channel); ```
``` % semgrep --test rules/ ✓ All tests passed! ```
``` rules: - id: create-chat-client patterns: - pattern: var $X = GrpcChannel.ForAddress(...); - pattern: var $Y = new ChatService.ChatServiceClient($X); languages: - csharp message: [pass] severity: INFO ```
2. And I am trying to match this code:
``` using Grpc.Net.Client; using GrpcChat.ProtoBuf; var channel = GrpcChannel.ForAddress("YOUR_HOST (port 8888)"); var client = new ChatService.ChatServiceClient(channel); ```
3. These match separately, but the 'patterns' should be a "AND" match and it fails. I must be missing something o
## When to use
You are seeing this: I must be missing something o Use this skill when you run into "Semgrep not finding two lines of code with a 'patterns' section".
## When not to use
If your error message or symptom does not match what is described above, this is probably not your fix. Search for your exact error text instead of forcing this one to fit.
## Versions
No specific versions are mentioned in the source material, so treat the fix as generally applicable and check the examples against whatever you have installed.
## Why this happens
The original report does not dig into a root cause. It documents the symptom and the fix that resolved it.
Evidence
Source: Stack Overflow #71459777 (top answer, 4 votes, 1 answers): I have a Semgrep rule: `` rules: - id: create-chat-client patterns: - pattern: var $X = GrpcChannel.ForAddress(...); - pattern: var $Y = new ChatService.ChatServiceClient($X); languages: - csharp message: [pass] severity: INFO And I am trying to match this code: using Grpc.Net.Client; using GrpcChat.ProtoBuf; var channel = GrpcChannel.ForAddress("YOUR_HOST (port 8888)"); var client = new ChatService.ChatServiceClient(channel); `` These match separately, but the 'patterns' should be a "AND" match and it fails. I must be missing something o
Verified resolution: [bstpierre (top answer)] Using pattern-inside works: `` rules: - id: chat patterns: - pattern-inside: | var $X = GrpcChannel.ForAddress(...); ... - pattern: var $Y = new ChatService.ChatServiceClient($X); languages: - csharp message: [pass] severity: INFO with this test case: using Grpc.Net.Client; using GrpcChat.ProtoBuf; var channel = GrpcChannel.ForAddress("YOUR_HOST (port 8888)"); // ruleid: chat var client = new ChatService.ChatServiceClient(channel); when I run a test: % semgrep --test rules/ ✓ All tests passed!
Source URL: https://stackoverflow.com/questions/71459777
By @issueatlas · Version 3 · Exact version
Embed this skill
Add this card to a website or documentation page. It links to the current skill on Vectle.
<a href="https://vectle.com/skills/skl_sQJxZvhhQT8O3_7zeQq_Sw" style="display:inline-block;padding:12px 16px;border:1px solid #d8ddd3;border-radius:8px;color:#194d35;text-decoration:none;font:14px/1.5 system-ui,sans-serif">Semgrep not finding two lines of code with a 'patterns' section <span>— Vectle</span></a>