Fix possible out-of-bounds array access
[git/git.git] / upload-pack.c
CommitLineData
def88e9a
LT
1#include "cache.h"
2#include "refs.h"
3#include "pkt-line.h"
f6b42a81
JH
4#include "tag.h"
5#include "object.h"
f0243f26 6#include "commit.h"
77cb17e9 7#include "exec_cmd.h"
def88e9a 8
960deccb 9static const char upload_pack_usage[] = "git-upload-pack [--strict] [--timeout=nn] <dir>";
def88e9a 10
f0243f26 11#define THEY_HAVE (1U << 0)
565ebbf7
JH
12#define OUR_REF (1U << 1)
13#define WANTED (1U << 2)
6b32884a
JH
14#define MAX_HAS 256
15#define MAX_NEEDS 256
1bd8c8f0 16static int nr_has = 0, nr_needs = 0, multi_ack = 0, nr_our_refs = 0;
b19696c2 17static int use_thin_pack = 0;
fb9040cc
LT
18static unsigned char has_sha1[MAX_HAS][20];
19static unsigned char needs_sha1[MAX_NEEDS][20];
960deccb
PA
20static unsigned int timeout = 0;
21
22static void reset_timeout(void)
23{
24 alarm(timeout);
25}
fb9040cc 26
75bfc6c2
LT
27static int strip(char *line, int len)
28{
29 if (len && line[len-1] == '\n')
30 line[--len] = 0;
31 return len;
32}
33
fb9040cc
LT
34static void create_pack_file(void)
35{
75bfc6c2
LT
36 int fd[2];
37 pid_t pid;
565ebbf7 38 int create_full_pack = (nr_our_refs == nr_needs && !nr_has);
75bfc6c2
LT
39
40 if (pipe(fd) < 0)
41 die("git-upload-pack: unable to create pipe");
42 pid = fork();
43 if (pid < 0)
44 die("git-upload-pack: unable to fork git-rev-list");
45
46 if (!pid) {
47 int i;
e091eb93 48 int args;
9201c707 49 const char **argv;
e091eb93
JH
50 char *buf;
51 char **p;
52
b19696c2 53 if (create_full_pack) {
565ebbf7 54 args = 10;
b19696c2
JH
55 use_thin_pack = 0; /* no point doing it */
56 }
e091eb93
JH
57 else
58 args = nr_has + nr_needs + 5;
9201c707
JH
59 p = xmalloc(args * sizeof(char *));
60 argv = (const char **) p;
e091eb93 61 buf = xmalloc(args * 45);
75bfc6c2
LT
62
63 dup2(fd[1], 1);
64 close(0);
65 close(fd[0]);
66 close(fd[1]);
77cb17e9 67 *p++ = "rev-list";
b19696c2 68 *p++ = use_thin_pack ? "--objects-edge" : "--objects";
b5c367f7 69 if (create_full_pack || MAX_NEEDS <= nr_needs)
e091eb93
JH
70 *p++ = "--all";
71 else {
72 for (i = 0; i < nr_needs; i++) {
73 *p++ = buf;
74 memcpy(buf, sha1_to_hex(needs_sha1[i]), 41);
75 buf += 41;
76 }
75bfc6c2 77 }
b5c367f7
JS
78 if (!create_full_pack)
79 for (i = 0; i < nr_has; i++) {
80 *p++ = buf;
81 *buf++ = '^';
82 memcpy(buf, sha1_to_hex(has_sha1[i]), 41);
83 buf += 41;
84 }
75bfc6c2 85 *p++ = NULL;
77cb17e9 86 execv_git_cmd(argv);
75bfc6c2
LT
87 die("git-upload-pack: unable to exec git-rev-list");
88 }
89 dup2(fd[0], 0);
90 close(fd[0]);
91 close(fd[1]);
77cb17e9 92 execl_git_cmd("pack-objects", "--stdout", NULL);
75bfc6c2 93 die("git-upload-pack: unable to exec git-pack-objects");
fb9040cc
LT
94}
95
def88e9a
LT
96static int got_sha1(char *hex, unsigned char *sha1)
97{
98 if (get_sha1_hex(hex, sha1))
99 die("git-upload-pack: expected SHA1 object, got '%s'", hex);
fb9040cc
LT
100 if (!has_sha1_file(sha1))
101 return 0;
f0243f26
JS
102 if (nr_has < MAX_HAS) {
103 struct object *o = lookup_object(sha1);
104 if (!(o && o->parsed))
105 o = parse_object(sha1);
106 if (!o)
107 die("oops (%s)", sha1_to_hex(sha1));
885a86ab 108 if (o->type == TYPE_COMMIT) {
f0243f26
JS
109 struct commit_list *parents;
110 if (o->flags & THEY_HAVE)
111 return 0;
112 o->flags |= THEY_HAVE;
113 for (parents = ((struct commit*)o)->parents;
114 parents;
115 parents = parents->next)
116 parents->item->object.flags |= THEY_HAVE;
117 }
118 memcpy(has_sha1[nr_has++], sha1, 20);
fb9040cc
LT
119 }
120 return 1;
def88e9a
LT
121}
122
123static int get_common_commits(void)
124{
125 static char line[1000];
1bd8c8f0 126 unsigned char sha1[20], last_sha1[20];
def88e9a
LT
127 int len;
128
f0243f26
JS
129 track_object_refs = 0;
130 save_commit_buffer = 0;
131
def88e9a
LT
132 for(;;) {
133 len = packet_read_line(0, line, sizeof(line));
960deccb 134 reset_timeout();
def88e9a
LT
135
136 if (!len) {
1bd8c8f0
JS
137 if (nr_has == 0 || multi_ack)
138 packet_write(1, "NAK\n");
def88e9a
LT
139 continue;
140 }
75bfc6c2 141 len = strip(line, len);
def88e9a 142 if (!strncmp(line, "have ", 5)) {
1bd8c8f0
JS
143 if (got_sha1(line+5, sha1) &&
144 (multi_ack || nr_has == 1)) {
145 if (nr_has >= MAX_HAS)
146 multi_ack = 0;
147 packet_write(1, "ACK %s%s\n",
148 sha1_to_hex(sha1),
149 multi_ack ? " continue" : "");
150 if (multi_ack)
151 memcpy(last_sha1, sha1, 20);
af2d3aa4 152 }
def88e9a
LT
153 continue;
154 }
155 if (!strcmp(line, "done")) {
1bd8c8f0
JS
156 if (nr_has > 0) {
157 if (multi_ack)
158 packet_write(1, "ACK %s\n",
159 sha1_to_hex(last_sha1));
160 return 0;
161 }
def88e9a
LT
162 packet_write(1, "NAK\n");
163 return -1;
164 }
165 die("git-upload-pack: expected SHA1 list, got '%s'", line);
166 }
def88e9a
LT
167}
168
fb9040cc
LT
169static int receive_needs(void)
170{
171 static char line[1000];
172 int len, needs;
173
174 needs = 0;
175 for (;;) {
565ebbf7 176 struct object *o;
e091eb93 177 unsigned char dummy[20], *sha1_buf;
fb9040cc 178 len = packet_read_line(0, line, sizeof(line));
960deccb 179 reset_timeout();
fb9040cc
LT
180 if (!len)
181 return needs;
182
e091eb93
JH
183 sha1_buf = dummy;
184 if (needs == MAX_NEEDS) {
185 fprintf(stderr,
186 "warning: supporting only a max of %d requests. "
187 "sending everything instead.\n",
188 MAX_NEEDS);
189 }
190 else if (needs < MAX_NEEDS)
191 sha1_buf = needs_sha1[needs];
192
193 if (strncmp("want ", line, 5) || get_sha1_hex(line+5, sha1_buf))
194 die("git-upload-pack: protocol error, "
195 "expected to get sha, not '%s'", line);
1bd8c8f0
JS
196 if (strstr(line+45, "multi_ack"))
197 multi_ack = 1;
b19696c2
JH
198 if (strstr(line+45, "thin-pack"))
199 use_thin_pack = 1;
565ebbf7
JH
200
201 /* We have sent all our refs already, and the other end
202 * should have chosen out of them; otherwise they are
203 * asking for nonsense.
204 *
205 * Hmph. We may later want to allow "want" line that
206 * asks for something like "master~10" (symbolic)...
207 * would it make sense? I don't know.
208 */
209 o = lookup_object(sha1_buf);
210 if (!o || !(o->flags & OUR_REF))
211 die("git-upload-pack: not our ref %s", line+5);
212 if (!(o->flags & WANTED)) {
213 o->flags |= WANTED;
214 needs++;
215 }
fb9040cc
LT
216 }
217}
218
def88e9a
LT
219static int send_ref(const char *refname, const unsigned char *sha1)
220{
b19696c2 221 static char *capabilities = "multi_ack thin-pack";
f6b42a81
JH
222 struct object *o = parse_object(sha1);
223
b5b16990
CW
224 if (!o)
225 die("git-upload-pack: cannot find object %s:", sha1_to_hex(sha1));
226
1f5881bb
JS
227 if (capabilities)
228 packet_write(1, "%s %s%c%s\n", sha1_to_hex(sha1), refname,
229 0, capabilities);
230 else
231 packet_write(1, "%s %s\n", sha1_to_hex(sha1), refname);
232 capabilities = NULL;
565ebbf7
JH
233 if (!(o->flags & OUR_REF)) {
234 o->flags |= OUR_REF;
235 nr_our_refs++;
236 }
885a86ab 237 if (o->type == TYPE_TAG) {
9534f40b 238 o = deref_tag(o, refname, 0);
f6b42a81
JH
239 packet_write(1, "%s %s^{}\n", sha1_to_hex(o->sha1), refname);
240 }
def88e9a
LT
241 return 0;
242}
243
244static int upload_pack(void)
245{
960deccb 246 reset_timeout();
723c31fe 247 head_ref(send_ref);
def88e9a
LT
248 for_each_ref(send_ref);
249 packet_flush(1);
fb9040cc
LT
250 nr_needs = receive_needs();
251 if (!nr_needs)
252 return 0;
def88e9a 253 get_common_commits();
fb9040cc 254 create_pack_file();
def88e9a
LT
255 return 0;
256}
257
258int main(int argc, char **argv)
259{
8d630132 260 char *dir;
960deccb
PA
261 int i;
262 int strict = 0;
263
264 for (i = 1; i < argc; i++) {
265 char *arg = argv[i];
266
267 if (arg[0] != '-')
268 break;
269 if (!strcmp(arg, "--strict")) {
270 strict = 1;
271 continue;
272 }
273 if (!strncmp(arg, "--timeout=", 10)) {
274 timeout = atoi(arg+10);
275 continue;
276 }
277 if (!strcmp(arg, "--")) {
278 i++;
279 break;
280 }
281 }
282
283 if (i != argc-1)
def88e9a 284 usage(upload_pack_usage);
960deccb 285 dir = argv[i];
113b9475 286
8d630132
AE
287 if (!enter_repo(dir, strict))
288 die("'%s': unable to chdir or not a git archive", dir);
960deccb 289
def88e9a
LT
290 upload_pack();
291 return 0;
292}