Skip to content

Commit ccc7a1f

Browse files
committed
Merge branch 'fixes'
2 parents 3e9d3de + 62f586c commit ccc7a1f

File tree

2 files changed

+52
-14
lines changed

2 files changed

+52
-14
lines changed

gix-validate/src/tag.rs

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use bstr::{BStr, BString};
1+
use bstr::{BStr, BString, ByteSlice};
22

33
///
44
#[allow(clippy::empty_docs)]
@@ -70,7 +70,10 @@ pub(crate) fn name_inner(input: &BStr, mode: Mode) -> Result<Option<BString>, na
7070
}
7171

7272
let mut previous = 0;
73-
for byte in input.iter() {
73+
let mut component_start;
74+
let mut component_end = 0;
75+
let last = input.len() - 1;
76+
for (byte_pos, byte) in input.iter().enumerate() {
7477
match byte {
7578
b'\\' | b'^' | b':' | b'[' | b'?' | b' ' | b'~' | b'\0'..=b'\x1F' | b'\x7F' => {
7679
if let Some(out) = out.as_mut() {
@@ -121,9 +124,36 @@ pub(crate) fn name_inner(input: &BStr, mode: Mode) -> Result<Option<BString>, na
121124
}
122125
}
123126
c => {
127+
if *c == b'/' {
128+
component_start = component_end;
129+
component_end = byte_pos;
130+
131+
if input[component_start..component_end].ends_with_str(".lock") {
132+
if let Some(out) = out.as_mut() {
133+
while out.ends_with(b".lock") {
134+
let len_without_suffix = out.len() - b".lock".len();
135+
out.truncate(len_without_suffix);
136+
}
137+
} else {
138+
return Err(name::Error::LockFileSuffix);
139+
}
140+
}
141+
}
142+
124143
if let Some(out) = out.as_mut() {
125144
out.push(*c)
126145
}
146+
147+
if byte_pos == last && input[component_end + 1..].ends_with_str(".lock") {
148+
if let Some(out) = out.as_mut() {
149+
while out.ends_with(b".lock") {
150+
let len_without_suffix = out.len() - b".lock".len();
151+
out.truncate(len_without_suffix);
152+
}
153+
} else {
154+
return Err(name::Error::LockFileSuffix);
155+
}
156+
}
127157
}
128158
}
129159
previous = *byte;
@@ -137,30 +167,21 @@ pub(crate) fn name_inner(input: &BStr, mode: Mode) -> Result<Option<BString>, na
137167
out.remove(0);
138168
}
139169
}
140-
if input[0] == b'.' {
170+
if out.as_ref().map_or(input, |b| b.as_bstr())[0] == b'.' {
141171
if let Some(out) = out.as_mut() {
142172
out[0] = b'-';
143173
} else {
144174
return Err(name::Error::StartsWithDot);
145175
}
146176
}
147-
if input[input.len() - 1] == b'.' {
177+
let last = out.as_ref().map_or(input, |b| b.as_bstr()).len() - 1;
178+
if out.as_ref().map_or(input, |b| b.as_bstr())[last] == b'.' {
148179
if let Some(out) = out.as_mut() {
149180
let last = out.len() - 1;
150181
out[last] = b'-';
151182
} else {
152183
return Err(name::Error::EndsWithDot);
153184
}
154185
}
155-
if input.ends_with(b".lock") {
156-
if let Some(out) = out.as_mut() {
157-
while out.ends_with(b".lock") {
158-
let len_without_suffix = out.len() - b".lock".len();
159-
out.truncate(len_without_suffix);
160-
}
161-
} else {
162-
return Err(name::Error::LockFileSuffix);
163-
}
164-
}
165186
Ok(out)
166187
}

gix-validate/tests/tag/mod.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,23 @@ mod name {
8888
mktests!(empty_component_san, b"prefix//suffix", "prefix/suffix");
8989
mktests!(ends_with_slash_san, b"prefix/", "prefix");
9090
mktest!(is_dot_lock, b".lock", StartsWithDot);
91+
mktest!(dot_lock_in_component, b"foo.lock/baz.lock/bar", LockFileSuffix);
92+
mktests!(dot_lock_in_component_san, b"foo.lock/baz.lock/bar", "foo/baz/bar");
93+
mktests!(
94+
dot_lock_in_each_component_san,
95+
b"foo.lock/baz.lock/bar.lock",
96+
"foo/baz/bar"
97+
);
98+
mktests!(
99+
multiple_dot_lock_in_each_component_san,
100+
b"foo.lock.lock/baz.lock.lock/bar.lock.lock",
101+
"foo/baz/bar"
102+
);
103+
mktests!(
104+
dot_lock_in_each_component_special_san,
105+
b"...lock/..lock//lock",
106+
"-lock/lock"
107+
);
91108
mktests!(is_dot_lock_san, b".lock", "-lock");
92109
mktest!(contains_double_dot, b"with..double-dot", RepeatedDot);
93110
mktests!(contains_double_dot_san, b"with..double-dot", "with.double-dot");

0 commit comments

Comments
 (0)