Most of the callers of packet_read_line just read into a
static 1000-byte buffer (callers which handle arbitrary
binary data already use LARGE_PACKET_MAX). This works fine
in practice, because:
1. The only variable-sized data in these lines is a ref
name, and refs tend to be a lot shorter than 1000
characters.
2. When sending ref lines, git-core always limits itself
to 1000 byte packets.
However, the only limit given in the protocol specification
in Documentation/technical/protocol-common.txt is
LARGE_PACKET_MAX; the 1000 byte limit is mentioned only in
pack-protocol.txt, and then only describing what we write,
not as a specific limit for readers.
This patch lets us bump the 1000-byte limit to
LARGE_PACKET_MAX. Even though git-core will never write a
packet where this makes a difference, there are two good
reasons to do this:
1. Other git implementations may have followed
protocol-common.txt and used a larger maximum size. We
don't bump into it in practice because it would involve
very long ref names.
2. We may want to increase the 1000-byte limit one day.
Since packets are transferred before any capabilities,
it's difficult to do this in a backwards-compatible
way. But if we bump the size of buffer the readers can
handle, eventually older versions of git will be
obsolete enough that we can justify bumping the
writers, as well. We don't have plans to do this
anytime soon, but there is no reason not to start the
clock ticking now.
Just bumping all of the reading bufs to LARGE_PACKET_MAX
would waste memory. Instead, since most readers just read
into a temporary buffer anyway, let's provide a single
static buffer that all callers can use. We can further wrap
this detail away by having the packet_read_line wrapper just
use the buffer transparently and return a pointer to the
static storage. That covers most of the cases, and the
remaining ones already read into their own LARGE_PACKET_MAX
buffers.
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
raw | patch | inline | side by side (from parent 1: 047ec60 )
const char *remote, const char *exec,
const char *name_hint)
{
const char *remote, const char *exec,
const char *name_hint)
{
- char buf[LARGE_PACKET_MAX] ;
- int fd[2], i, len, rv;
+ char *buf ;
+ int fd[2], i, rv;
struct transport *transport;
struct remote *_remote;
struct transport *transport;
struct remote *_remote;
packet_write(fd[1], "argument %s\n", argv[i]);
packet_flush(fd[1]);
packet_write(fd[1], "argument %s\n", argv[i]);
packet_flush(fd[1]);
- len = packet_read_line(fd[0], buf, sizeof(buf) );
- if (!len )
+ buf = packet_read_line(fd[0], NULL );
+ if (!buf )
die(_("git archive: expected ACK/NAK, got EOF"));
if (strcmp(buf, "ACK")) {
die(_("git archive: expected ACK/NAK, got EOF"));
if (strcmp(buf, "ACK")) {
- if (len > 5 && !prefixcmp(buf, "NACK "))
+ if (!prefixcmp(buf, "NACK "))
die(_("git archive: NACK %s"), buf + 5);
die(_("git archive: NACK %s"), buf + 5);
- if (len > 4 && !prefixcmp(buf, "ERR "))
+ if (!prefixcmp(buf, "ERR "))
die(_("remote error: %s"), buf + 4);
die(_("git archive: protocol error"));
}
die(_("remote error: %s"), buf + 4);
die(_("git archive: protocol error"));
}
- len = packet_read_line(fd[0], buf, sizeof(buf));
- if (len)
+ if (packet_read_line(fd[0], NULL))
die(_("git archive: expected a flush"));
/* Now, start reading from fd[0] and spit it out to stdout */
die(_("git archive: expected a flush"));
/* Now, start reading from fd[0] and spit it out to stdout */
/* in stateless RPC mode we use pkt-line to read
* from stdin, until we get a flush packet
*/
/* in stateless RPC mode we use pkt-line to read
* from stdin, until we get a flush packet
*/
- static char line[1000];
- int n = packet_read_line(0, line, sizeof(line) );
- if (!n )
+ char *line = packet_read_line(0, NULL );
+ if (!line )
- string_list_append(&sought, xmemdupz(line, n ));
+ string_list_append(&sought, xstrdup(line ));
struct command *commands = NULL;
struct command **p = &commands;
for (;;) {
struct command *commands = NULL;
struct command **p = &commands;
for (;;) {
- static char line[1000] ;
unsigned char old_sha1[20], new_sha1[20];
struct command *cmd;
char *refname;
int len, reflen;
unsigned char old_sha1[20], new_sha1[20];
struct command *cmd;
char *refname;
int len, reflen;
- len = packet_read_line(0, line, sizeof(line) );
- if (!len )
+ line = packet_read_line(0, &len );
+ if (!line )
break;
if (len < 83 ||
line[40] != ' ' ||
break;
if (len < 83 ||
line[40] != ' ' ||
{
struct argv_array sent_argv = ARGV_ARRAY_INIT;
const char *arg_cmd = "argument ";
{
struct argv_array sent_argv = ARGV_ARRAY_INIT;
const char *arg_cmd = "argument ";
- char buf[4096];
- int len;
if (argc != 2)
usage(upload_archive_usage);
if (argc != 2)
usage(upload_archive_usage);
/* put received options in sent_argv[] */
argv_array_push(&sent_argv, "git-upload-archive");
for (;;) {
/* put received options in sent_argv[] */
argv_array_push(&sent_argv, "git-upload-archive");
for (;;) {
- /* This will die if not enough free space in buf */
- len = packet_read_line(0, buf, sizeof(buf));
- if (len == 0)
+ char *buf = packet_read_line(0, NULL);
+ if (!buf)
break; /* got a flush */
if (sent_argv.argc > MAX_ARGS)
die("Too many options (>%d)", MAX_ARGS - 1);
break; /* got a flush */
if (sent_argv.argc > MAX_ARGS)
die("Too many options (>%d)", MAX_ARGS - 1);
for (;;) {
struct ref *ref;
unsigned char old_sha1[20];
for (;;) {
struct ref *ref;
unsigned char old_sha1[20];
- static char buffer[1000];
char *name;
int len, name_len;
char *name;
int len, name_len;
+ char *buffer = packet_buffer;
- len = packet_read(in, buffer, sizeof( buffer),
+ len = packet_read(in, packet_buffer, sizeof(packet_ buffer),
PACKET_READ_GENTLE_ON_EOF |
PACKET_READ_CHOMP_NEWLINE);
if (len < 0)
PACKET_READ_GENTLE_ON_EOF |
PACKET_READ_CHOMP_NEWLINE);
if (len < 0)
static int execute(void)
{
static int execute(void)
{
- static char line[1000] ;
+ char *line = packet_buffer ;
int pktlen, len, i;
char *addr = getenv("REMOTE_ADDR"), *port = getenv("REMOTE_PORT");
int pktlen, len, i;
char *addr = getenv("REMOTE_ADDR"), *port = getenv("REMOTE_PORT");
loginfo("Connection from %s:%s", addr, port);
alarm(init_timeout ? init_timeout : timeout);
loginfo("Connection from %s:%s", addr, port);
alarm(init_timeout ? init_timeout : timeout);
- pktlen = packet_read(0, line, sizeof(line ), 0);
+ pktlen = packet_read(0, packet_buffer, sizeof(packet_buffer ), 0);
alarm(0);
len = strlen(line);
alarm(0);
len = strlen(line);
* shallow and unshallow commands every time there
* is a block of have lines exchanged.
*/
* shallow and unshallow commands every time there
* is a block of have lines exchanged.
*/
- char line[1000] ;
- while (packet_read_line(fd, line, sizeof(line ))) {
+ char *line ;
+ while ((line = packet_read_line(fd, NULL ))) {
if (!prefixcmp(line, "shallow "))
continue;
if (!prefixcmp(line, "unshallow "))
if (!prefixcmp(line, "shallow "))
continue;
if (!prefixcmp(line, "unshallow "))
static enum ack_type get_ack(int fd, unsigned char *result_sha1)
{
static enum ack_type get_ack(int fd, unsigned char *result_sha1)
{
- static char line[1000] ;
- int len = packet_read_line(fd, line, sizeof(line) );
+ int len ;
+ char *line = packet_read_line(fd, &len );
if (!len)
die("git fetch-pack: expected ACK/NAK, got EOF");
if (!len)
die("git fetch-pack: expected ACK/NAK, got EOF");
state_len = req_buf.len;
if (args->depth > 0) {
state_len = req_buf.len;
if (args->depth > 0) {
unsigned char sha1[20];
send_request(args, fd[1], &req_buf);
unsigned char sha1[20];
send_request(args, fd[1], &req_buf);
- while (packet_read_line(fd[0], line, sizeof(line ))) {
+ while ((line = packet_read_line(fd[0], NULL ))) {
if (!prefixcmp(line, "shallow ")) {
if (get_sha1_hex(line + 8, sha1))
die("invalid shallow line: %s", line);
if (!prefixcmp(line, "shallow ")) {
if (get_sha1_hex(line + 8, sha1))
die("invalid shallow line: %s", line);
#include "cache.h"
#include "pkt-line.h"
#include "cache.h"
#include "pkt-line.h"
+char packet_buffer[LARGE_PACKET_MAX];
static const char *packet_trace_prefix = "git";
static const char trace_key[] = "GIT_TRACE_PACKET";
static const char *packet_trace_prefix = "git";
static const char trace_key[] = "GIT_TRACE_PACKET";
-int packet_read_line(int fd, char *buffer, unsigned size )
+char *packet_read_line(int fd, int *len_p )
- return packet_read(fd, buffer, size, PACKET_READ_CHOMP_NEWLINE);
+ int len = packet_read(fd, packet_buffer, sizeof(packet_buffer),
+ PACKET_READ_CHOMP_NEWLINE);
+ if (len_p)
+ *len_p = len;
+ return len ? packet_buffer : NULL;
}
int packet_get_line(struct strbuf *out,
}
int packet_get_line(struct strbuf *out,
/*
* Convenience wrapper for packet_read that is not gentle, and sets the
/*
* Convenience wrapper for packet_read that is not gentle, and sets the
- * CHOMP_NEWLINE option.
+ * CHOMP_NEWLINE option. The return value is NULL for a flush packet,
+ * and otherwise points to a static buffer (that may be overwritten by
+ * subsequent calls). If the size parameter is not NULL, the length of the
+ * packet is written to it.
-int packet_read_line(int fd, char *buffer, unsigned size);
+char *packet_read_line(int fd, int *size);
+
#define DEFAULT_PACKET_MAX 1000
#define LARGE_PACKET_MAX 65520
#define DEFAULT_PACKET_MAX 1000
#define LARGE_PACKET_MAX 65520
+extern char packet_buffer[LARGE_PACKET_MAX];
int packet_get_line(struct strbuf *out, char **src_buf, size_t *src_len);
int packet_get_line(struct strbuf *out, char **src_buf, size_t *src_len);
static int receive_status(int in, struct ref *refs)
{
struct ref *hint;
static int receive_status(int in, struct ref *refs)
{
struct ref *hint;
- int len = packet_read_line(in, line, sizeof(line) );
+ char *line = packet_read_line(in, NULL );
if (prefixcmp(line, "unpack "))
return error("did not receive remote status");
if (strcmp(line, "unpack ok")) {
if (prefixcmp(line, "unpack "))
return error("did not receive remote status");
if (strcmp(line, "unpack ok")) {
while (1) {
char *refname;
char *msg;
while (1) {
char *refname;
char *msg;
- len = packet_read_line(in, line, sizeof(line) );
- if (!len )
+ line = packet_read_line(in, NULL );
+ if (!line )
break;
if (prefixcmp(line, "ok ") && prefixcmp(line, "ng ")) {
error("invalid ref status from remote: %s", line);
break;
if (prefixcmp(line, "ok ") && prefixcmp(line, "ng ")) {
error("invalid ref status from remote: %s", line);
static int get_common_commits(void)
{
static int get_common_commits(void)
{
- static char line[1000];
unsigned char sha1[20];
char last_hex[41];
int got_common = 0;
unsigned char sha1[20];
char last_hex[41];
int got_common = 0;
save_commit_buffer = 0;
for (;;) {
save_commit_buffer = 0;
for (;;) {
- int len = packet_read_line(0, line, sizeof(line) );
+ char *line = packet_read_line(0, NULL );
if (multi_ack == 2 && got_common
&& !got_other && ok_to_give_up()) {
sent_ready = 1;
if (multi_ack == 2 && got_common
&& !got_other && ok_to_give_up()) {
sent_ready = 1;
static void receive_needs(void)
{
struct object_array shallows = OBJECT_ARRAY_INIT;
static void receive_needs(void)
{
struct object_array shallows = OBJECT_ARRAY_INIT;
- static char line[1000];
- int len, depth = 0;
int has_non_tip = 0;
shallow_nr = 0;
int has_non_tip = 0;
shallow_nr = 0;
struct object *o;
const char *features;
unsigned char sha1_buf[20];
struct object *o;
const char *features;
unsigned char sha1_buf[20];
- len = packet_read_line(0, line, sizeof(line) );
+ char *line = packet_read_line(0, NULL );
break;
if (!prefixcmp(line, "shallow ")) {
break;
if (!prefixcmp(line, "shallow ")) {