From b07db609089749ed49a7f0b1fb3841a8f74110c2 Mon Sep 17 00:00:00 2001 From: Thomas Wolf Date: Mon, 14 Aug 2017 08:04:56 +0200 Subject: Fix off-by-one error in Strings.count() Change-Id: I0667b1624827d1cf0cc1b81f86c7bb44eafd68a7 Signed-off-by: Thomas Wolf --- .../src/org/eclipse/jgit/ignore/internal/Strings.java | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) (limited to 'org.eclipse.jgit/src') diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/ignore/internal/Strings.java b/org.eclipse.jgit/src/org/eclipse/jgit/ignore/internal/Strings.java index da482fa50a..79df1511d1 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/ignore/internal/Strings.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/ignore/internal/Strings.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2014, Andrey Loskutov + * Copyright (C) 2014, 2017 Andrey Loskutov * and other copyright owners as documented in the project's IP log. * * This program and the accompanying materials are made available @@ -123,12 +123,15 @@ public class Strings { static int count(String s, char c, boolean ignoreFirstLast) { int start = 0; int count = 0; - while (true) { + int length = s.length(); + while (start < length) { start = s.indexOf(c, start); - if (start == -1) + if (start == -1) { break; - if (!ignoreFirstLast || (start != 0 && start != s.length())) + } + if (!ignoreFirstLast || (start != 0 && start != length - 1)) { count++; + } start++; } return count; -- cgit v1.2.3