[JGIT PATCH 2/7] Disambiguate pkt-line "0000" from "0004"

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



The pkt-line length includes its own 4 bytes.  So "0000" is the
magic flush/end marker used as part of the protocols based upon
pkt-line, while "0004" indicates a packet of 0 bytes, but not a
flush/end marker.

Currently there is no need for this distinction in the code, as
the protocol never sends an empty packet, but it reduces the risk
that in the future a "0004" packet is misread as a "0000" flush.

Signed-off-by: Shawn O. Pearce <spearce@xxxxxxxxxxx>
---
 .../spearce/jgit/transport/BasePackConnection.java |    5 ++---
 .../jgit/transport/BasePackPushConnection.java     |    2 +-
 .../org/spearce/jgit/transport/DaemonClient.java   |    5 +----
 .../org/spearce/jgit/transport/PacketLineIn.java   |   18 +++++++++---------
 .../org/spearce/jgit/transport/ReceivePack.java    |    6 +++---
 .../src/org/spearce/jgit/transport/UploadPack.java |    4 ++--
 6 files changed, 18 insertions(+), 22 deletions(-)

diff --git a/org.spearce.jgit/src/org/spearce/jgit/transport/BasePackConnection.java b/org.spearce.jgit/src/org/spearce/jgit/transport/BasePackConnection.java
index c6440c7..0382d2b 100644
--- a/org.spearce.jgit/src/org/spearce/jgit/transport/BasePackConnection.java
+++ b/org.spearce.jgit/src/org/spearce/jgit/transport/BasePackConnection.java
@@ -140,6 +140,8 @@ private void readAdvertisedRefsImpl() throws IOException {
 					throw noRepository();
 				throw eof;
 			}
+			if (line == PacketLineIn.END)
+				break;
 
 			if (avail.isEmpty()) {
 				final int nul = line.indexOf('\0');
@@ -152,9 +154,6 @@ private void readAdvertisedRefsImpl() throws IOException {
 				}
 			}
 
-			if (line.length() == 0)
-				break;
-
 			String name = line.substring(41, line.length());
 			if (avail.isEmpty() && name.equals("capabilities^{}")) {
 				// special line from git-receive-pack to show
diff --git a/org.spearce.jgit/src/org/spearce/jgit/transport/BasePackPushConnection.java b/org.spearce.jgit/src/org/spearce/jgit/transport/BasePackPushConnection.java
index 1117109..712d3c0 100644
--- a/org.spearce.jgit/src/org/spearce/jgit/transport/BasePackPushConnection.java
+++ b/org.spearce.jgit/src/org/spearce/jgit/transport/BasePackPushConnection.java
@@ -225,7 +225,7 @@ private void readStatusReport(final Map<String, RemoteRefUpdate> refUpdates)
 							+ unpackStatus);
 
 		String refLine;
-		while ((refLine = pckIn.readString()).length() > 0) {
+		while ((refLine = pckIn.readString()) != PacketLineIn.END) {
 			boolean ok = false;
 			int refNameEnd = -1;
 			if (refLine.startsWith("ok ")) {
diff --git a/org.spearce.jgit/src/org/spearce/jgit/transport/DaemonClient.java b/org.spearce.jgit/src/org/spearce/jgit/transport/DaemonClient.java
index 636cf22..e80d86b 100644
--- a/org.spearce.jgit/src/org/spearce/jgit/transport/DaemonClient.java
+++ b/org.spearce.jgit/src/org/spearce/jgit/transport/DaemonClient.java
@@ -85,10 +85,7 @@ void execute(final InputStream in, final OutputStream out)
 		rawIn = in;
 		rawOut = out;
 
-		String cmd = new PacketLineIn(rawIn).readStringNoLF();
-		if (cmd == null || cmd.length() == 0)
-			return;
-
+		String cmd = new PacketLineIn(rawIn).readStringRaw();
 		final int nul = cmd.indexOf('\0');
 		if (nul >= 0) {
 			// Newer clients hide a "host" header behind this byte.
diff --git a/org.spearce.jgit/src/org/spearce/jgit/transport/PacketLineIn.java b/org.spearce.jgit/src/org/spearce/jgit/transport/PacketLineIn.java
index ef218be..92c7009 100644
--- a/org.spearce.jgit/src/org/spearce/jgit/transport/PacketLineIn.java
+++ b/org.spearce.jgit/src/org/spearce/jgit/transport/PacketLineIn.java
@@ -50,6 +50,7 @@
 import org.spearce.jgit.util.RawParseUtils;
 
 class PacketLineIn {
+	static final String END = new String("") /* must not string pool */;
 	private static final byte fromhex[];
 
 	static {
@@ -101,20 +102,23 @@ AckNackResult readACK(final MutableObjectId returnedId) throws IOException {
 	String readString() throws IOException {
 		int len = readLength();
 		if (len == 0)
-			return "";
+			return END;
 
-		len -= 5; // length header (4 bytes) and trailing LF.
+		len -= 4; // length header (4 bytes)
+		if (len == 0)
+			return "";
 
 		final byte[] raw = new byte[len];
 		NB.readFully(in, raw, 0, len);
-		readLF();
+		if (raw[len - 1] == '\n')
+			len--;
 		return RawParseUtils.decode(Constants.CHARSET, raw, 0, len);
 	}
 
-	String readStringNoLF() throws IOException {
+	String readStringRaw() throws IOException {
 		int len = readLength();
 		if (len == 0)
-			return "";
+			return END;
 
 		len -= 4; // length header (4 bytes)
 
@@ -123,10 +127,6 @@ String readStringNoLF() throws IOException {
 		return RawParseUtils.decode(Constants.CHARSET, raw, 0, len);
 	}
 
-	private void readLF() throws IOException {
-		if (in.read() != '\n')
-			throw new IOException("Protocol error: expected LF");
-	}
 
 	int readLength() throws IOException {
 		NB.readFully(in, lenbuffer, 0, 4);
diff --git a/org.spearce.jgit/src/org/spearce/jgit/transport/ReceivePack.java b/org.spearce.jgit/src/org/spearce/jgit/transport/ReceivePack.java
index eaa1016..abaf876 100644
--- a/org.spearce.jgit/src/org/spearce/jgit/transport/ReceivePack.java
+++ b/org.spearce.jgit/src/org/spearce/jgit/transport/ReceivePack.java
@@ -506,12 +506,14 @@ private void recvCommands() throws IOException {
 		for (;;) {
 			String line;
 			try {
-				line = pckIn.readStringNoLF();
+				line = pckIn.readStringRaw();
 			} catch (EOFException eof) {
 				if (commands.isEmpty())
 					return;
 				throw eof;
 			}
+			if (line == PacketLineIn.END)
+				break;
 
 			if (commands.isEmpty()) {
 				final int nul = line.indexOf('\0');
@@ -522,8 +524,6 @@ private void recvCommands() throws IOException {
 				}
 			}
 
-			if (line.length() == 0)
-				break;
 			if (line.length() < 83) {
 				final String m = "error: invalid protocol: wanted 'old new ref'";
 				sendError(m);
diff --git a/org.spearce.jgit/src/org/spearce/jgit/transport/UploadPack.java b/org.spearce.jgit/src/org/spearce/jgit/transport/UploadPack.java
index 45d57b3..53fbce0 100644
--- a/org.spearce.jgit/src/org/spearce/jgit/transport/UploadPack.java
+++ b/org.spearce.jgit/src/org/spearce/jgit/transport/UploadPack.java
@@ -293,7 +293,7 @@ private void recvWants() throws IOException {
 				throw eof;
 			}
 
-			if (line.length() == 0)
+			if (line == PacketLineIn.END)
 				break;
 			if (!line.startsWith("want ") || line.length() < 45)
 				throw new PackProtocolException("expected want; got " + line);
@@ -348,7 +348,7 @@ private void negotiate() throws IOException {
 				throw eof;
 			}
 
-			if (line.length() == 0) {
+			if (line == PacketLineIn.END) {
 				if (commonBase.isEmpty() || multiAck)
 					pckOut.writeString("NAK\n");
 				pckOut.flush();
-- 
1.6.3.1.333.g3ebba7

--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]