Commit 05816a98e95b0fb3897f1e8482494893c20673bb

Edward Thomson 2020-04-05T17:20:08

netops: use GIT_ASSERT

diff --git a/src/netops.c b/src/netops.c
index 1ef2302..a1ee292 100644
--- a/src/netops.c
+++ b/src/netops.c
@@ -61,18 +61,20 @@ void gitno_buffer_setup_fromstream(git_stream *st, gitno_buffer *buf, char *data
 }
 
 /* Consume up to ptr and move the rest of the buffer to the beginning */
-void gitno_consume(gitno_buffer *buf, const char *ptr)
+int gitno_consume(gitno_buffer *buf, const char *ptr)
 {
 	size_t consumed;
 
-	assert(ptr - buf->data >= 0);
-	assert(ptr - buf->data <= (int) buf->len);
+	GIT_ASSERT(ptr - buf->data >= 0);
+	GIT_ASSERT(ptr - buf->data <= (int) buf->len);
 
 	consumed = ptr - buf->data;
 
 	memmove(buf->data, ptr, buf->offset - consumed);
 	memset(buf->data + buf->offset, 0x0, buf->len - buf->offset);
 	buf->offset -= consumed;
+
+	return 0;
 }
 
 /* Consume const bytes and move the rest of the buffer to the beginning */
diff --git a/src/netops.h b/src/netops.h
index 52f1ccc..771c87b 100644
--- a/src/netops.h
+++ b/src/netops.h
@@ -62,7 +62,7 @@ void gitno_buffer_setup_fromstream(git_stream *st, gitno_buffer *buf, char *data
 void gitno_buffer_setup_callback(gitno_buffer *buf, char *data, size_t len, int (*recv)(gitno_buffer *buf), void *cb_data);
 int gitno_recv(gitno_buffer *buf);
 
-void gitno_consume(gitno_buffer *buf, const char *ptr);
+int gitno_consume(gitno_buffer *buf, const char *ptr);
 void gitno_consume_n(gitno_buffer *buf, size_t cons);
 
 #endif
diff --git a/src/transports/smart_protocol.c b/src/transports/smart_protocol.c
index c01656d..18dcaa8 100644
--- a/src/transports/smart_protocol.c
+++ b/src/transports/smart_protocol.c
@@ -64,7 +64,9 @@ int git_smart__store_refs(transport_smart *t, int flushes)
 			continue;
 		}
 
-		gitno_consume(buf, line_end);
+		if (gitno_consume(buf, line_end) < 0)
+			return -1;
+
 		if (pkt->type == GIT_PKT_ERR) {
 			git_error_set(GIT_ERROR_NET, "remote error: %s", ((git_pkt_err *)pkt)->error);
 			git__free(pkt);
@@ -236,7 +238,9 @@ static int recv_pkt(git_pkt **out_pkt, git_pkt_type *out_type, gitno_buffer *buf
 		}
 	} while (error);
 
-	gitno_consume(buf, line_end);
+	if (gitno_consume(buf, line_end) < 0)
+		return -1;
+
 	if (out_type != NULL)
 		*out_type = pkt->type;
 	if (out_pkt != NULL)
@@ -791,7 +795,8 @@ static int parse_report(transport_smart *transport, git_push *push)
 			continue;
 		}
 
-		gitno_consume(buf, line_end);
+		if (gitno_consume(buf, line_end) < 0)
+			return -1;
 
 		error = 0;