Browse Source

SSH config: fix whitespace handling

Use Character.isWhitespace() instead of Character.isSpaceChar() to
treat TABs as whitespace, too.

Change-Id: Iffc59c13357d981ede6a1e0feb6ea6ff03fb3064
Signed-off-by: Thomas Wolf <thomas.wolf@paranor.ch>
tags/v5.12.0.202105261145-m3
Thomas Wolf 3 years ago
parent
commit
c718e6059c

+ 17
- 0
org.eclipse.jgit.ssh.jsch.test/tst/org/eclipse/jgit/transport/OpenSshConfigTest.java View File

assertArrayEquals(new Object[] { "/foo", "/bar", "/baz" }, assertArrayEquals(new Object[] { "/foo", "/bar", "/baz" },
h.getConfig().getValues("IdentityFile")); h.getConfig().getValues("IdentityFile"));
} }

@Test
public void testWhitespace() throws Exception {
config("Host foo \tbar baz\nPort 29418\n");
Host h = osc.lookup("foo");
assertNotNull(h);
assertEquals(29418, h.getPort());
h = osc.lookup("bar");
assertNotNull(h);
assertEquals(29418, h.getPort());
h = osc.lookup("baz");
assertNotNull(h);
assertEquals(29418, h.getPort());
h = osc.lookup("\tbar");
assertNotNull(h);
assertEquals(22, h.getPort());
}
} }

+ 8
- 5
org.eclipse.jgit/src/org/eclipse/jgit/internal/transport/ssh/OpenSshConfigFile.java View File

int length = argument.length(); int length = argument.length();
while (start < length) { while (start < length) {
// Skip whitespace // Skip whitespace
if (Character.isSpaceChar(argument.charAt(start))) {
if (Character.isWhitespace(argument.charAt(start))) {
start++; start++;
continue; continue;
} }
} else { } else {
int stop = start + 1; int stop = start + 1;
while (stop < length while (stop < length
&& !Character.isSpaceChar(argument.charAt(stop))) {
&& !Character.isWhitespace(argument.charAt(stop))) {
stop++; stop++;
} }
result.add(argument.substring(start, stop)); result.add(argument.substring(start, stop));


private static String stripWhitespace(String value) { private static String stripWhitespace(String value) {
final StringBuilder b = new StringBuilder(); final StringBuilder b = new StringBuilder();
for (int i = 0; i < value.length(); i++) {
if (!Character.isSpaceChar(value.charAt(i)))
b.append(value.charAt(i));
int length = value.length();
for (int i = 0; i < length; i++) {
char ch = value.charAt(i);
if (!Character.isWhitespace(ch)) {
b.append(ch);
}
} }
return b.toString(); return b.toString();
} }

Loading…
Cancel
Save