Commit 59ece79d29ce555391953bc053f934b5b1ec15ff

Stefan Sperling 2018-02-12T22:13:27

make struct got_object_id opaque

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
diff --git a/include/got_error.h b/include/got_error.h
index 5543f39..63ff8ec 100644
--- a/include/got_error.h
+++ b/include/got_error.h
@@ -38,6 +38,7 @@
 #define GOT_ERR_BAD_DELTA_CHAIN	20
 #define GOT_ERR_BAD_DELTA	21
 #define GOT_ERR_COMPRESSION	22
+#define GOT_ERR_BAD_OBJ_ID_STR	23
 
 static const struct got_error {
 	int code;
@@ -64,8 +65,9 @@ static const struct got_error {
 	{ GOT_ERR_NOT_IMPL,	"feature not implemented" },
 	{ GOT_ERR_OBJ_NOT_PACKED,"object is not packed" },
 	{ GOT_ERR_BAD_DELTA_CHAIN,"bad delta chain" },
-	{ GOT_ERR_BAD_DELTA	,"bad delta" },
-	{ GOT_ERR_COMPRESSION,"compression failed" },
+	{ GOT_ERR_BAD_DELTA,	"bad delta" },
+	{ GOT_ERR_COMPRESSION,	"compression failed" },
+	{ GOT_ERR_BAD_OBJ_ID_STR,"bad object id string" },
 };
 
 const struct got_error * got_error(int code);
diff --git a/include/got_object.h b/include/got_object.h
index 12c26ba..1e490d0 100644
--- a/include/got_object.h
+++ b/include/got_object.h
@@ -14,9 +14,7 @@
  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  */
 
-struct got_object_id {
-	u_int8_t sha1[SHA1_DIGEST_LENGTH];
-};
+struct got_object_id;
 
 struct got_blob_object;
 
@@ -24,7 +22,7 @@ struct got_tree_entry {
 	SIMPLEQ_ENTRY(got_tree_entry) entry;
 	mode_t mode;
 	char *name;
-	struct got_object_id id;
+	struct got_object_id *id;
 };
 
 struct got_tree_object {
@@ -34,13 +32,13 @@ struct got_tree_object {
 
 struct got_parent_id {
 	SIMPLEQ_ENTRY(got_parent_id) entry;
-	struct got_object_id id;
+	struct got_object_id *id;
 };
 
 SIMPLEQ_HEAD(got_parent_id_list, got_parent_id);
 
 struct got_commit_object {
-	struct got_object_id tree_id;
+	struct got_object_id *tree_id;
 	unsigned int nparents;
 	SIMPLEQ_HEAD(, got_parent_id) parent_ids;
 	char *author;
@@ -60,6 +58,7 @@ struct got_object;
 struct got_repository;
 
 char *got_object_id_str(struct got_object_id *, char *, size_t);
+const struct got_error *got_parse_object_id(struct got_object_id **, const char *);
 int got_object_id_cmp(struct got_object_id *, struct got_object_id *);
 int got_object_get_type(struct got_object *);
 const struct got_error *got_object_open(struct got_object **,
diff --git a/lib/diff.c b/lib/diff.c
index 0d41719..a2cb581 100644
--- a/lib/diff.c
+++ b/lib/diff.c
@@ -366,21 +366,21 @@ diff_entry_old_new(struct got_tree_entry *te1, struct got_tree_object *tree2,
 	te2 = match_entry_by_name(te1, tree2);
 	if (te2 == NULL) {
 		if (S_ISDIR(te1->mode))
-			return diff_deleted_tree(&te1->id, repo, outfile);
-		return diff_deleted_blob(&te1->id, repo, outfile);
+			return diff_deleted_tree(te1->id, repo, outfile);
+		return diff_deleted_blob(te1->id, repo, outfile);
 	}
 
 	if (S_ISDIR(te1->mode) && S_ISDIR(te2->mode)) {
-		if (got_object_id_cmp(&te1->id, &te2->id) != 0)
-			return diff_modified_tree(&te1->id, &te2->id, repo,
+		if (got_object_id_cmp(te1->id, te2->id) != 0)
+			return diff_modified_tree(te1->id, te2->id, repo,
 			    outfile);
 	} else if (S_ISREG(te1->mode) && S_ISREG(te2->mode)) {
-		if (got_object_id_cmp(&te1->id, &te2->id) != 0)
-			return diff_modified_blob(&te1->id, &te2->id, repo,
+		if (got_object_id_cmp(te1->id, te2->id) != 0)
+			return diff_modified_blob(te1->id, te2->id, repo,
 			    outfile);
 	}
 
-	return diff_kind_mismatch(&te1->id, &te2->id, outfile);
+	return diff_kind_mismatch(te1->id, te2->id, outfile);
 }
 
 static const struct got_error *
@@ -395,8 +395,8 @@ diff_entry_new_old(struct got_tree_entry *te2, struct got_tree_object *tree1,
 		return NULL;
 
 	if (S_ISDIR(te2->mode))
-		return diff_added_tree(&te2->id, repo, outfile);
-	return diff_added_blob(&te2->id, repo, outfile);
+		return diff_added_tree(te2->id, repo, outfile);
+	return diff_added_blob(te2->id, repo, outfile);
 }
 
 const struct got_error *
diff --git a/lib/object.c b/lib/object.c
index 9e483cc..e9db3c6 100644
--- a/lib/object.c
+++ b/lib/object.c
@@ -30,8 +30,8 @@
 #include "got_object.h"
 #include "got_repository.h"
 #include "got_sha1.h"
-#include "pack.h"
 #include "delta.h"
+#include "pack.h"
 #include "zb.h"
 #include "object.h"
 
@@ -58,6 +58,21 @@ got_object_id_str(struct got_object_id *id, char *buf, size_t size)
 	return got_sha1_digest_to_str(id->sha1, buf, size);
 }
 
+const struct got_error *
+got_parse_object_id(struct got_object_id **id, const char *buf)
+{
+	*id = calloc(1, sizeof(**id));
+	if (*id == NULL)
+		return got_error(GOT_ERR_NO_MEM);
+	if (!got_parse_sha1_digest((*id)->sha1, buf)) {
+		free(*id);
+		*id = NULL;
+		return got_error(GOT_ERR_BAD_OBJ_ID_STR);
+	}
+	return NULL;
+}
+
+
 int
 got_object_id_cmp(struct got_object_id *id1, struct got_object_id *id2)
 {
@@ -281,7 +296,7 @@ commit_object_valid(struct got_commit_object *commit)
 
 	n = 0;
 	for (i = 0; i < SHA1_DIGEST_LENGTH; i++) {
-		if (commit->tree_id.sha1[i] == 0)
+		if (commit->tree_id->sha1[i] == 0)
 			n++;
 	}
 	if (n == SHA1_DIGEST_LENGTH)
@@ -301,6 +316,12 @@ parse_commit_object(struct got_commit_object **commit, char *buf, size_t len)
 	*commit = calloc(1, sizeof(**commit));
 	if (*commit == NULL)
 		return got_error(GOT_ERR_NO_MEM);
+	(*commit)->tree_id = calloc(1, sizeof(*(*commit)->tree_id));
+	if ((*commit)->tree_id == NULL) {
+		free(*commit);
+		*commit = NULL;
+		return got_error(GOT_ERR_NO_MEM);
+	}
 
 	SIMPLEQ_INIT(&(*commit)->parent_ids);
 
@@ -312,7 +333,7 @@ parse_commit_object(struct got_commit_object **commit, char *buf, size_t len)
 			goto done;
 		}
 		s += tlen;
-		if (!got_parse_sha1_digest((*commit)->tree_id.sha1, s)) {
+		if (!got_parse_sha1_digest((*commit)->tree_id->sha1, s)) {
 			err = got_error(GOT_ERR_BAD_OBJ_DATA);
 			goto done;
 		}
@@ -338,9 +359,17 @@ parse_commit_object(struct got_commit_object **commit, char *buf, size_t len)
 			err = got_error(GOT_ERR_NO_MEM);
 			goto done;
 		}
+		pid->id = calloc(1, sizeof(*pid->id));
+		if (pid->id == NULL) {
+			free(pid);
+			err = got_error(GOT_ERR_NO_MEM);
+			goto done;
+		}
 		s += tlen;
-		if (!got_parse_sha1_digest(pid->id.sha1, s)) {
+		if (!got_parse_sha1_digest(pid->id->sha1, s)) {
 			err = got_error(GOT_ERR_BAD_OBJ_DATA);
+			free(pid->id);
+			free(pid);
 			goto done;
 		}
 		SIMPLEQ_INSERT_TAIL(&(*commit)->parent_ids, pid, entry);
@@ -406,14 +435,17 @@ parse_commit_object(struct got_commit_object **commit, char *buf, size_t len)
 		goto done;
 	}
 done:
-	if (err)
+	if (err) {
 		got_object_commit_close(*commit);
+		*commit = NULL;
+	}
 	return err;
 }
 
 static void
 tree_entry_close(struct got_tree_entry *te)
 {
+	free(te->id);
 	free(te->name);
 	free(te);
 }
@@ -430,15 +462,24 @@ parse_tree_entry(struct got_tree_entry **te, size_t *elen, char *buf,
 	if (*te == NULL)
 		return got_error(GOT_ERR_NO_MEM);
 
+	(*te)->id = calloc(1, sizeof(*(*te)->id));
+	if ((*te)->id == NULL) {
+		free(*te);
+		*te = NULL;
+		return got_error(GOT_ERR_NO_MEM);
+	}
+
 	*elen = strlen(buf) + 1;
 	if (*elen > maxlen) {
 		free(*te);
+		*te = NULL;
 		return got_error(GOT_ERR_BAD_OBJ_DATA);
 	}
 
 	space = strchr(buf, ' ');
 	if (space == NULL) {
 		free(*te);
+		*te = NULL;
 		return got_error(GOT_ERR_BAD_OBJ_DATA);
 	}
 	while (*p != ' ') {
@@ -457,11 +498,13 @@ parse_tree_entry(struct got_tree_entry **te, size_t *elen, char *buf,
 		goto done;
 	}
 	buf += strlen(buf) + 1;
-	memcpy((*te)->id.sha1, buf, SHA1_DIGEST_LENGTH);
+	memcpy((*te)->id->sha1, buf, SHA1_DIGEST_LENGTH);
 	*elen += SHA1_DIGEST_LENGTH;
 done:
-	if (err)
+	if (err) {
 		tree_entry_close(*te);
+		*te = NULL;
+	}
 	return err;
 }
 
@@ -608,9 +651,11 @@ got_object_commit_close(struct got_commit_object *commit)
 	while (!SIMPLEQ_EMPTY(&commit->parent_ids)) {
 		pid = SIMPLEQ_FIRST(&commit->parent_ids);
 		SIMPLEQ_REMOVE_HEAD(&commit->parent_ids, entry);
+		free(pid->id);
 		free(pid);
 	}
 
+	free(commit->tree_id);
 	free(commit->author);
 	free(commit->committer);
 	free(commit->logmsg);
diff --git a/lib/object.h b/lib/object.h
index 9b27d1b..156901f 100644
--- a/lib/object.h
+++ b/lib/object.h
@@ -14,6 +14,10 @@
  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  */
 
+struct got_object_id {
+	u_int8_t sha1[SHA1_DIGEST_LENGTH];
+};
+
 struct got_object {
 	int type;
 	int flags;
diff --git a/lib/pack.h b/lib/pack.h
index 189b564..348ae1a 100644
--- a/lib/pack.h
+++ b/lib/pack.h
@@ -88,7 +88,7 @@ struct got_packfile_object_data {
 
 /* If object is of type	GOT_OBJ_TYPE_REF_DELTA. */
 struct got_packfile_object_data_ref_delta {
-	struct got_object_id id;
+	uint8_t sha1[SHA1_DIGEST_LENGTH];
 	uint8_t *delta_data;		/* compressed */
 };
 
diff --git a/lib/refs.c b/lib/refs.c
index 9392f66..67a327f 100644
--- a/lib/refs.c
+++ b/lib/refs.c
@@ -31,7 +31,9 @@
 #include "got_sha1.h"
 
 #include "path.h"
-
+#include "delta.h"
+#include "zb.h"
+#include "object.h"
 
 static const struct got_error *
 parse_symref(struct got_reference **ref, const char *name, const char *line)
diff --git a/regress/repository/repository_test.c b/regress/repository/repository_test.c
index 0b04bd6..0dac4a0 100644
--- a/regress/repository/repository_test.c
+++ b/regress/repository/repository_test.c
@@ -23,6 +23,7 @@
 #include <string.h>
 #include <sha1.h>
 #include <zlib.h>
+#include <unistd.h>
 
 #include "got_error.h"
 #include "got_object.h"
@@ -30,7 +31,6 @@
 #include "got_repository.h"
 #include "got_sha1.h"
 #include "got_diff.h"
-#include "unistd.h"
 
 #define GOT_REPO_PATH "../../../"
 
@@ -61,7 +61,7 @@ print_parent_commits(struct got_commit_object *commit,
 	struct got_object *obj;
 
 	SIMPLEQ_FOREACH(pid, &commit->parent_ids, entry) {
-		err = got_object_open(&obj, repo, &pid->id);
+		err = got_object_open(&obj, repo, pid->id);
 		if (err != NULL)
 			return err;
 		if (got_object_get_type(obj) != GOT_OBJ_TYPE_COMMIT)
@@ -92,15 +92,15 @@ print_tree_object(struct got_object *obj, char *parent,
 
 		if (!S_ISDIR(te->mode)) {
 			test_printf("%s %s/%s\n",
-			    got_object_id_str(&te->id, hex, sizeof(hex)),
+			    got_object_id_str(te->id, hex, sizeof(hex)),
 			    parent, te->name);
 			continue;
 		}
 		test_printf("%s %s/%s\n",
-		    got_object_id_str(&te->id, hex, sizeof(hex)),
+		    got_object_id_str(te->id, hex, sizeof(hex)),
 		    parent, te->name);
 
-		err = got_object_open(&treeobj, repo, &te->id);
+		err = got_object_open(&treeobj, repo, te->id);
 		if (err != NULL)
 			break;
 
@@ -144,17 +144,17 @@ print_commit_object(struct got_object *obj, struct got_repository *repo)
 		return err;
 
 	test_printf("tree: %s\n",
-	    got_object_id_str(&commit->tree_id, buf, sizeof(buf)));
+	    got_object_id_str(commit->tree_id, buf, sizeof(buf)));
 	test_printf("parent%s: ", (commit->nparents == 1) ? "" : "s");
 	SIMPLEQ_FOREACH(pid, &commit->parent_ids, entry) {
 		test_printf("%s\n",
-		    got_object_id_str(&pid->id, buf, sizeof(buf)));
+		    got_object_id_str(pid->id, buf, sizeof(buf)));
 	}
 	test_printf("author: %s\n", commit->author);
 	test_printf("committer: %s\n", commit->committer);
 	test_printf("log: %s\n", commit->logmsg);
 
-	err = got_object_open(&treeobj, repo, &commit->tree_id);
+	err = got_object_open(&treeobj, repo, commit->tree_id);
 	if (err != NULL)
 		return err;
 	if (got_object_get_type(treeobj) == GOT_OBJ_TYPE_TREE) {
@@ -212,19 +212,21 @@ repo_read_tree(const char *repo_path)
 	const char *tree_sha1 = "6cc96e0e093fb30630ba7f199d0a008b24c6a690";
 	const struct got_error *err;
 	struct got_repository *repo;
-	struct got_object_id id;
+	struct got_object_id *id;
 	struct got_object *obj;
 	char hex[SHA1_DIGEST_STRING_LENGTH];
 	int i;
 	size_t len;
 
-	if (!got_parse_sha1_digest(id.sha1, tree_sha1))
+	err = got_parse_object_id(&id, tree_sha1);
+	if (err != NULL)
 		return 0;
 
 	err = got_repo_open(&repo, repo_path);
 	if (err != NULL || repo == NULL)
 		return 0;
-	err = got_object_open(&obj, repo, &id);
+	err = got_object_open(&obj, repo, id);
+	free(id);
 	if (err != NULL || obj == NULL)
 		return 0;
 	if (got_object_get_type(obj) != GOT_OBJ_TYPE_TREE)
@@ -243,20 +245,22 @@ repo_read_blob(const char *repo_path)
 	const char *blob_sha1 = "141f5fdc96126c1f4195558560a3c915e3d9b4c3";
 	const struct got_error *err;
 	struct got_repository *repo;
-	struct got_object_id id;
+	struct got_object_id *id;
 	struct got_object *obj;
 	struct got_blob_object *blob;
 	char hex[SHA1_DIGEST_STRING_LENGTH];
 	int i;
 	size_t len;
 
-	if (!got_parse_sha1_digest(id.sha1, blob_sha1))
+	err = got_parse_object_id(&id, blob_sha1);
+	if (err != NULL)
 		return 0;
 
 	err = got_repo_open(&repo, repo_path);
 	if (err != NULL || repo == NULL)
 		return 0;
-	err = got_object_open(&obj, repo, &id);
+	err = got_object_open(&obj, repo, id);
+	free(id);
 	if (err != NULL || obj == NULL)
 		return 0;
 	if (got_object_get_type(obj) != GOT_OBJ_TYPE_BLOB)
@@ -290,8 +294,8 @@ repo_diff_blob(const char *repo_path)
 	const char *blob2_sha1 = "de7eb21b21c7823a753261aadf7cba35c9580fbf";
 	const struct got_error *err;
 	struct got_repository *repo;
-	struct got_object_id id1;
-	struct got_object_id id2;
+	struct got_object_id *id1;
+	struct got_object_id *id2;
 	struct got_object *obj1;
 	struct got_object *obj2;
 	struct got_blob_object *blob1;
@@ -301,21 +305,26 @@ repo_diff_blob(const char *repo_path)
 	size_t len;
 	FILE *outfile;
 
-	if (!got_parse_sha1_digest(id1.sha1, blob1_sha1))
+	err = got_parse_object_id(&id1, blob1_sha1);
+	if (err != NULL)
 		return 0;
-	if (!got_parse_sha1_digest(id2.sha1, blob2_sha1))
+
+	err = got_parse_object_id(&id2, blob2_sha1);
+	if (err != NULL)
 		return 0;
 
 	err = got_repo_open(&repo, repo_path);
 	if (err != NULL || repo == NULL)
 		return 0;
 
-	err = got_object_open(&obj1, repo, &id1);
+	err = got_object_open(&obj1, repo, id1);
+	free(id1);
 	if (err != NULL || obj1 == NULL)
 		return 0;
 	if (got_object_get_type(obj1) != GOT_OBJ_TYPE_BLOB)
 		return 0;
-	err = got_object_open(&obj2, repo, &id2);
+	err = got_object_open(&obj2, repo, id2);
+	free(id2);
 	if (err != NULL || obj2 == NULL)
 		return 0;
 	if (got_object_get_type(obj2) != GOT_OBJ_TYPE_BLOB)
@@ -354,8 +363,8 @@ repo_diff_tree(const char *repo_path)
 	const char *tree2_sha1 = "4aa8f2933839ff8a8fb3f905a4c232d22c6ff5f3";
 	const struct got_error *err;
 	struct got_repository *repo;
-	struct got_object_id id1;
-	struct got_object_id id2;
+	struct got_object_id *id1;
+	struct got_object_id *id2;
 	struct got_object *obj1;
 	struct got_object *obj2;
 	struct got_tree_object *tree1;
@@ -365,21 +374,26 @@ repo_diff_tree(const char *repo_path)
 	size_t len;
 	FILE *outfile;
 
-	if (!got_parse_sha1_digest(id1.sha1, tree1_sha1))
+	err = got_parse_object_id(&id1, tree1_sha1);
+	if (err != NULL)
 		return 0;
-	if (!got_parse_sha1_digest(id2.sha1, tree2_sha1))
+
+	err = got_parse_object_id(&id2, tree2_sha1);
+	if (err != NULL)
 		return 0;
 
 	err = got_repo_open(&repo, repo_path);
 	if (err != NULL || repo == NULL)
 		return 0;
 
-	err = got_object_open(&obj1, repo, &id1);
+	err = got_object_open(&obj1, repo, id1);
+	free(id1);
 	if (err != NULL || obj1 == NULL)
 		return 0;
 	if (got_object_get_type(obj1) != GOT_OBJ_TYPE_TREE)
 		return 0;
-	err = got_object_open(&obj2, repo, &id2);
+	err = got_object_open(&obj2, repo, id2);
+	free(id2);
 	if (err != NULL || obj2 == NULL)
 		return 0;
 	if (got_object_get_type(obj2) != GOT_OBJ_TYPE_TREE)