MCPcopy Create free account
hub / github.com/ElementsProject/lightning / xml_nesting_ok

Function xml_nesting_ok

plugins/rest-plugin/src/handlers.rs:48–146  ·  view source on GitHub ↗

Iteratively bound the element nesting depth of `s` without building a DOM or recursing, so it cannot itself overflow. Runs *before* roxmltree's recursive parse; only needs to be exact on well-formed XML (malformed input is rejected downstream) and a miscount can only over-reject, never under-protect.

(s: &str)

Source from the content-addressed store, hash-verified

46/// is rejected downstream) and a miscount can only over-reject, never
47/// under-protect.
48fn xml_nesting_ok(s: &str) -> bool {
49 let b = s.as_bytes();
50 let mut depth = 0usize;
51 let mut i = 0usize;
52 while i < b.len() {
53 if b[i] != b'<' {
54 i += 1;
55 continue;
56 }
57 // Comments: <!-- ... -->.
58 if b[i..].starts_with(b"<!--") {
59 match find_bytes(&b[i + 4..], b"-->") {
60 Some(off) => i += 4 + off + 3,
61 None => return true, // malformed; parse rejects it
62 }
63 continue;
64 }
65 // CDATA: <![CDATA[ ... ]]>.
66 if b[i..].starts_with(b"<![CDATA[") {
67 match find_bytes(&b[i + 9..], b"]]>") {
68 Some(off) => i += 9 + off + 3,
69 None => return true,
70 }
71 continue;
72 }
73 // Processing instructions and declarations: <? ... ?>.
74 if b[i..].starts_with(b"<?") {
75 match find_bytes(&b[i + 2..], b"?>") {
76 Some(off) => i += 2 + off + 2,
77 None => return true,
78 }
79 continue;
80 }
81 // DOCTYPE: <!DOCTYPE ...> (may contain a bracketed internal subset).
82 if b[i..].starts_with(b"<!") {
83 let mut j = i + 2;
84 let mut subset = 0usize;
85 while j < b.len() {
86 match b[j] {
87 b'[' => subset += 1,
88 b']' if subset > 0 => subset -= 1,
89 b'>' if subset == 0 => {
90 i = j + 1;
91 break;
92 }
93 _ => (),
94 }
95 j += 1;
96 }
97 if j >= b.len() {
98 return true;
99 }
100 continue;
101 }
102 // Closing tag: </name>.
103 if b[i..].starts_with(b"</") {
104 match find_bytes(&b[i + 2..], b">") {
105 Some(off) => {

Callers 1

Calls 1

find_bytesFunction · 0.85

Tested by

no test coverage detected