Skip to content

Commit b6d4e99

Browse files
committed
Merge branch 'gix-config-fix'
2 parents 8ee1194 + 75f48d6 commit b6d4e99

File tree

3 files changed

+43
-10
lines changed

3 files changed

+43
-10
lines changed

gix-config/src/value/normalize.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -54,27 +54,27 @@ use bstr::{BStr, BString, ByteSlice};
5454
/// assert_eq!(normalize_bstr(r#"hello "world\"""#), Cow::<BStr>::Owned(BString::from(r#"hello world""#)));
5555
/// ```
5656
#[must_use]
57-
pub fn normalize(input: Cow<'_, BStr>) -> Cow<'_, BStr> {
57+
pub fn normalize(mut input: Cow<'_, BStr>) -> Cow<'_, BStr> {
5858
if input.as_ref() == "\"\"" {
5959
return Cow::Borrowed("".into());
6060
}
61-
62-
let size = input.len();
63-
if size >= 3 && input[0] == b'"' && input[size - 1] == b'"' && input[size - 2] != b'\\' {
64-
match input {
65-
Cow::Borrowed(input) => return normalize_bstr(&input[1..size - 1]),
66-
Cow::Owned(mut input) => {
61+
// An optimization to strip enclosing quotes without producing a new value/copy it.
62+
while input.len() >= 3 && input[0] == b'"' && input[input.len() - 1] == b'"' && input[input.len() - 2] != b'\\' {
63+
match &mut input {
64+
Cow::Borrowed(input) => *input = &input[1..input.len() - 1],
65+
Cow::Owned(input) => {
6766
input.pop();
6867
input.remove(0);
69-
return normalize_bstring(input);
7068
}
7169
}
70+
if input.as_ref() == "\"\"" {
71+
return Cow::Borrowed("".into());
72+
}
7273
}
7374

74-
if input.find_byteset(b"\\\"").is_none() {
75+
if input.find_byteset(br#"\""#).is_none() {
7576
return input;
7677
}
77-
7878
let mut out: BString = Vec::with_capacity(input.len()).into();
7979
let mut bytes = input.iter().copied();
8080
while let Some(c) = bytes.next() {

gix-config/tests/file/mod.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use std::borrow::Cow;
22

33
use bstr::{BStr, ByteSlice};
4+
use gix_config::File;
45

56
pub fn cow_str(s: &str) -> Cow<'_, BStr> {
67
Cow::Borrowed(s.as_bytes().as_bstr())
@@ -25,6 +26,23 @@ mod open {
2526
}
2627
}
2728

29+
#[test]
30+
fn fuzzed() {
31+
let file = File::from_bytes_no_includes(
32+
include_bytes!("../fixtures/fuzzed/stackoverflow-01.config"),
33+
gix_config::file::Metadata::default(),
34+
Default::default(),
35+
)
36+
.unwrap();
37+
for section in file.sections() {
38+
for key in section.keys() {
39+
section
40+
.value_implicit(key.as_ref())
41+
.expect("The key exists, so should the value.");
42+
}
43+
}
44+
}
45+
2846
mod access;
2947
mod impls;
3048
mod init;

gix-config/tests/fixtures/fuzzed/stackoverflow-01.config

Lines changed: 15 additions & 0 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)