diff options
Diffstat (limited to 'org.eclipse.jgit/src/org/eclipse/jgit/util')
5 files changed, 31 insertions, 33 deletions
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/util/RawParseUtils.java b/org.eclipse.jgit/src/org/eclipse/jgit/util/RawParseUtils.java index 93bf847196..0e8e9b3d84 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/util/RawParseUtils.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/util/RawParseUtils.java @@ -30,6 +30,7 @@ import java.util.HashMap; import java.util.Map; import org.eclipse.jgit.annotations.Nullable; +import org.eclipse.jgit.diff.RawText; import org.eclipse.jgit.errors.BinaryBlobException; import org.eclipse.jgit.lib.Constants; import org.eclipse.jgit.lib.PersonIdent; @@ -679,36 +680,30 @@ public final class RawParseUtils { * 1 past the end of the content within <code>buf</code>. * @return a line map indicating the starting position of each line. * @throws BinaryBlobException - * if a NUL byte is found. + * if a NUL byte or a lone CR is found. * @since 5.0 */ public static final IntList lineMapOrBinary(byte[] buf, int ptr, int end) throws BinaryBlobException { - IntList map = lineMapOrNull(buf, ptr, end); - if (map == null) { - throw new BinaryBlobException(); - } - return map; - } - - @Nullable - private static IntList lineMapOrNull(byte[] buf, int ptr, int end) { // Experimentally derived from multiple source repositories // the average number of bytes/line is 36. Its a rough guess // to initially size our map close to the target. IntList map = new IntList((end - ptr) / 36); map.add(Integer.MIN_VALUE); - boolean foundLF = true; + byte last = '\n'; // Must be \n to add the initial ptr for (; ptr < end; ptr++) { - if (foundLF) { + if (last == '\n') { map.add(ptr); } - - if (buf[ptr] == '\0') { - return null; + byte curr = buf[ptr]; + if (RawText.isBinary(curr, last)) { + throw new BinaryBlobException(); } - - foundLF = (buf[ptr] == '\n'); + last = curr; + } + if (last == '\r') { + // Counts as binary + throw new BinaryBlobException(); } map.add(end); return map; diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/util/io/AutoCRLFInputStream.java b/org.eclipse.jgit/src/org/eclipse/jgit/util/io/AutoCRLFInputStream.java index 1b03d097b6..cedb159827 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/util/io/AutoCRLFInputStream.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/util/io/AutoCRLFInputStream.java @@ -123,7 +123,7 @@ public class AutoCRLFInputStream extends InputStream { return false; } if (detectBinary) { - isBinary = RawText.isBinary(buf, cnt); + isBinary = RawText.isBinary(buf, cnt, cnt < buf.length); detectBinary = false; } ptr = 0; diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/util/io/AutoCRLFOutputStream.java b/org.eclipse.jgit/src/org/eclipse/jgit/util/io/AutoCRLFOutputStream.java index 05e271febd..e638b2de3a 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/util/io/AutoCRLFOutputStream.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/util/io/AutoCRLFOutputStream.java @@ -122,22 +122,24 @@ public class AutoCRLFOutputStream extends OutputStream { } private int buffer(byte[] b, int off, int len) throws IOException { - if (binbufcnt > binbuf.length) + if (binbufcnt > binbuf.length) { return len; + } int copy = Math.min(binbuf.length - binbufcnt, len); System.arraycopy(b, off, binbuf, binbufcnt, copy); binbufcnt += copy; int remaining = len - copy; - if (remaining > 0) - decideMode(); + if (remaining > 0) { + decideMode(false); + } return remaining; } - private void decideMode() throws IOException { + private void decideMode(boolean complete) throws IOException { if (detectBinary) { - isBinary = RawText.isBinary(binbuf, binbufcnt); + isBinary = RawText.isBinary(binbuf, binbufcnt, complete); if (!isBinary) { - isBinary = RawText.isCrLfText(binbuf, binbufcnt); + isBinary = RawText.isCrLfText(binbuf, binbufcnt, complete); } detectBinary = false; } @@ -149,8 +151,9 @@ public class AutoCRLFOutputStream extends OutputStream { /** {@inheritDoc} */ @Override public void flush() throws IOException { - if (binbufcnt <= binbuf.length) - decideMode(); + if (binbufcnt <= binbuf.length) { + decideMode(true); + } buf = -1; out.flush(); } diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/util/io/AutoLFInputStream.java b/org.eclipse.jgit/src/org/eclipse/jgit/util/io/AutoLFInputStream.java index b6d1848b3a..7db882c074 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/util/io/AutoLFInputStream.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/util/io/AutoLFInputStream.java @@ -262,14 +262,14 @@ public class AutoLFInputStream extends InputStream { return false; } if (detectBinary) { - isBinary = RawText.isBinary(buf, cnt); + isBinary = RawText.isBinary(buf, cnt, cnt < buf.length); passAsIs = isBinary; detectBinary = false; if (isBinary && abortIfBinary) { throw new IsBinaryException(); } if (!passAsIs && forCheckout) { - passAsIs = RawText.isCrLfText(buf, cnt); + passAsIs = RawText.isCrLfText(buf, cnt, cnt < buf.length); } } ptr = 0; diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/util/io/AutoLFOutputStream.java b/org.eclipse.jgit/src/org/eclipse/jgit/util/io/AutoLFOutputStream.java index e08a53f502..a0e9fb68c5 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/util/io/AutoLFOutputStream.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/util/io/AutoLFOutputStream.java @@ -146,16 +146,16 @@ public class AutoLFOutputStream extends OutputStream { binbufcnt += copy; int remaining = len - copy; if (remaining > 0) { - decideMode(); + decideMode(false); } return remaining; } - private void decideMode() throws IOException { + private void decideMode(boolean complete) throws IOException { if (detectBinary) { - isBinary = RawText.isBinary(binbuf, binbufcnt); + isBinary = RawText.isBinary(binbuf, binbufcnt, complete); if (!isBinary) { - isBinary = RawText.isCrLfText(binbuf, binbufcnt); + isBinary = RawText.isCrLfText(binbuf, binbufcnt, complete); } detectBinary = false; } @@ -168,7 +168,7 @@ public class AutoLFOutputStream extends OutputStream { @Override public void flush() throws IOException { if (binbufcnt <= binbuf.length) { - decideMode(); + decideMode(true); } out.flush(); } |