Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
View all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
full-stack-plugins
codeguard
Repository navigation
Code
Issues
Pull requests
1
(1)
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
main
Breadcrumbs
codeguard
/
crates
/
codeguard-runtime
/
src
/
wasm_root_child_scan.rs
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
49 lines (47 loc) · 1.91 KB
main
Breadcrumbs
codeguard
/
crates
/
codeguard-runtime
/
src
/
wasm_root_child_scan.rs
Copy path
Top
File metadata and controls
Code
Blame
49 lines (47 loc) · 1.91 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
use tree_sitter::Tree;
/// 根节点直接命名子节点的有界存在事实;不解释语言规则或赋予源码违规权威。
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct WasmRootChildScan {
/// 固定grammar返回的根节点类型;相同根类型不代表相同语言。
pub root_syntax_kind: String,
/// 调用方明确指定的子节点类型。
pub child_syntax_kind: String,
/// 已遍历的直接命名子节点中是否存在该类型。
pub present: bool,
/// 存在未遍历的直接命名子节点;为true时,present=false不能证明缺失。
pub truncated: bool,
}
/// 有界检查根节点的直接命名子节点,不搜索注释文本或嵌套节点。
/// 参数为固定grammar语法树、非空节点类型及1至200000个子节点预算;
/// 返回AST事实及截断状态,片段/完整文件和语言规则必须由上层单独核对。
pub fn scan_wasm_root_child(
tree: &Tree,
child_syntax_kind: &str,
max_children: usize,
) -> Result<WasmRootChildScan, String> {
if !(1..=200_000).contains(&max_children)
|| child_syntax_kind.is_empty()
|| child_syntax_kind.len() > 128
|| child_syntax_kind.chars().any(char::is_control)
{
return Err("根节点事实参数或预算无效".into());
}
let root = tree.root_node();
let mut cursor = root.walk();
let mut present = false;
let mut truncated = false;
// 游标保持线性遍历;不能反复按索引查找而造成大文件二次复杂度。
for (visited, child) in root.named_children(&mut cursor).enumerate() {
if visited == max_children {
truncated = true;
break;
}
present |= child.kind() == child_syntax_kind;
}
Ok(WasmRootChildScan {
root_syntax_kind: root.kind().to_owned(),
child_syntax_kind: child_syntax_kind.to_owned(),
present,
truncated,
})
}
You can’t perform that action at this time.