Commit 0a585a0d6446ac47834148d4071c8374ffc15bc2

Stefan Sperling 2018-03-17T18:13:19

remove GOT_ERR_NO_MEM, we can just use errno in those cases

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
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
diff --git a/got/got.c b/got/got.c
index 176c609..40c422e 100644
--- a/got/got.c
+++ b/got/got.c
@@ -190,15 +190,16 @@ cmd_checkout(int argc, char *argv[])
 		if (dotgit)
 			*dotgit = '\0';
 		if (asprintf(&worktree_path, "%s/%s", cwd, base) == -1) {
+			error = got_error_from_errno();
 			free(cwd);
-			return got_error(GOT_ERR_NO_MEM);
+			return error;
 		}
 		free(cwd);
 	} else if (argc == 2) {
 		repo_path = argv[0];
 		worktree_path = strdup(argv[1]);
 		if (worktree_path == NULL)
-			return got_error(GOT_ERR_NO_MEM);
+			return got_error_from_errno();
 	} else
 		usage_checkout();
 
@@ -272,11 +273,12 @@ print_commits(struct got_object *root_obj, struct got_object_id *root_id,
 
 	entry = calloc(1, sizeof(*entry));
 	if (entry == NULL)
-		return got_error(GOT_ERR_NO_MEM);
+		return got_error_from_errno();
 	entry->id = got_object_id_dup(root_id);
 	if (entry->id == NULL) {
+		err = got_error_from_errno();
 		free(entry);
-		return got_error(GOT_ERR_NO_MEM);
+		return err;
 	}
 	entry->commit = root_commit;
 	TAILQ_INSERT_HEAD(&commits, entry, entry);
@@ -309,13 +311,13 @@ print_commits(struct got_object *root_obj, struct got_object_id *root_id,
 
 			pentry = calloc(1, sizeof(*pentry));
 			if (pentry == NULL) {
-				err = got_error(GOT_ERR_NO_MEM);
+				err = got_error_from_errno();
 				got_object_commit_close(pcommit);
 				break;
 			}
 			pentry->id = got_object_id_dup(pid->id);
 			if (pentry->id == NULL) {
-				err = got_error(GOT_ERR_NO_MEM);
+				err = got_error_from_errno();
 				got_object_commit_close(pcommit);
 				break;
 			}
diff --git a/include/got_error.h b/include/got_error.h
index 0d03e12..ad4669b 100644
--- a/include/got_error.h
+++ b/include/got_error.h
@@ -15,8 +15,8 @@
  */
 
 /* Error codes */
-#define GOT_ERR_ERRNO		0
-#define GOT_ERR_NO_MEM		1
+#define GOT_ERR_OK		0
+#define GOT_ERR_ERRNO		1
 #define GOT_ERR_NOT_GIT_REPO	2
 #define GOT_ERR_NOT_ABSPATH	3
 #define GOT_ERR_BAD_PATH	4
@@ -50,8 +50,8 @@ static const struct got_error {
 	int code;
 	const char *msg;
 } got_errors[] = {
+	{ GOT_ERR_OK,		"no error occured?!?" },
 	{ GOT_ERR_ERRNO,	"see errno" },
-	{ GOT_ERR_NO_MEM,	"out of memory" },
 	{ GOT_ERR_NOT_GIT_REPO, "no git repository found" },
 	{ GOT_ERR_NOT_ABSPATH,	"absolute path expected" },
 	{ GOT_ERR_BAD_PATH,	"bad path" },
diff --git a/lib/diffreg.c b/lib/diffreg.c
index 2fdd381..c20c1b3 100644
--- a/lib/diffreg.c
+++ b/lib/diffreg.c
@@ -331,11 +331,11 @@ got_diffreg(int *rval, FILE *f1, FILE *f2, int flags,
 		goto closem;
 	}
 	if (prepare(ds, 0, f1, ds->stb1.st_size, flags)) {
-		err = got_error(GOT_ERR_NO_MEM);
+		err = got_error_from_errno();
 		goto closem;
 	}
 	if (prepare(ds, 1, f2, ds->stb2.st_size, flags)) {
-		err = got_error(GOT_ERR_NO_MEM);
+		err = got_error_from_errno();
 		goto closem;
 	}
 
@@ -347,44 +347,46 @@ got_diffreg(int *rval, FILE *f1, FILE *f2, int flags,
 	equiv(ds->sfile[0], ds->slen[0], ds->sfile[1], ds->slen[1], ds->member);
 	ds->member = reallocarray(ds->member, ds->slen[1] + 2, sizeof(*ds->member));
 	if (ds->member == NULL) {
-		err = got_error(GOT_ERR_NO_MEM);
+		err = got_error_from_errno();
 		goto closem;
 	}
 
 	ds->class = (int *)ds->file[0];
 	if (unsort(ds->sfile[0], ds->slen[0], ds->class)) {
-		err = got_error(GOT_ERR_NO_MEM);
+		err = got_error_from_errno();
 		goto closem;
 	}
 	ds->class = reallocarray(ds->class, ds->slen[0] + 2, sizeof(*ds->class));
 	if (ds->class == NULL) {
-		err = got_error(GOT_ERR_NO_MEM);
+		err = got_error_from_errno();
 		goto closem;
 	}
 
 	ds->klist = calloc(ds->slen[0] + 2, sizeof(*ds->klist));
 	if (ds->klist == NULL) {
-		err = got_error(GOT_ERR_NO_MEM);
+		err = got_error_from_errno();
 		goto closem;
 	}
 	ds->clen = 0;
 	ds->clistlen = 100;
 	ds->clist = calloc(ds->clistlen, sizeof(*ds->clist));
 	if (ds->clist == NULL) {
-		err = got_error(GOT_ERR_NO_MEM);
+		err = got_error_from_errno();
 		goto closem;
 	}
 	i = stone(ds, ds->class, ds->slen[0], ds->member, ds->klist, flags);
-	free(ds->member);
-	free(ds->class);
 	if (i < 0) {
-		err = got_error(GOT_ERR_NO_MEM);
+		err = got_error_from_errno();
+		free(ds->member);
+		free(ds->class);
 		goto closem;
 	}
+	free(ds->member);
+	free(ds->class);
 
 	ds->J = reallocarray(ds->J, ds->len[0] + 2, sizeof(*ds->J));
 	if (ds->J == NULL) {
-		err = got_error(GOT_ERR_NO_MEM);
+		err = got_error_from_errno();
 		goto closem;
 	}
 	unravel(ds, ds->klist[i]);
@@ -395,18 +397,18 @@ got_diffreg(int *rval, FILE *f1, FILE *f2, int flags,
 
 	ds->ixold = reallocarray(ds->ixold, ds->len[0] + 2, sizeof(*ds->ixold));
 	if (ds->ixold == NULL) {
-		err = got_error(GOT_ERR_NO_MEM);
+		err = got_error_from_errno();
 		goto closem;
 	}
 	ds->ixnew = reallocarray(ds->ixnew, ds->len[1] + 2, sizeof(*ds->ixnew));
 	if (ds->ixnew == NULL) {
-		err = got_error(GOT_ERR_NO_MEM);
+		err = got_error_from_errno();
 		goto closem;
 	}
 	check(ds, f1, f2, flags);
 	if (output(outfile, ds, args, args->label[0], f1, args->label[1], f2,
 	    flags))
-		err = got_error(GOT_ERR_NO_MEM);
+		err = got_error_from_errno();
 closem:
 	if (ds->anychange) {
 		args->status |= 1;
diff --git a/lib/fileindex.c b/lib/fileindex.c
index 6c2d120..6511dec 100644
--- a/lib/fileindex.c
+++ b/lib/fileindex.c
@@ -39,13 +39,14 @@ got_fileindex_entry_open(struct got_fileindex_entry **entry,
 
 	*entry = calloc(1, sizeof(**entry));
 	if (*entry == NULL)
-		return got_error(GOT_ERR_NO_MEM);
+		return got_error_from_errno();
 
 	(*entry)->path = strdup(relpath);
 	if ((*entry)->path == NULL) {
+		const struct got_error *err = got_error_from_errno();
 		free(*entry);
 		*entry = NULL;
-		return got_error(GOT_ERR_NO_MEM);
+		return err;
 	}
 	
 	(*entry)->ctime_sec = sb.st_ctime;
diff --git a/lib/object.c b/lib/object.c
index 7037325..7f38f5a 100644
--- a/lib/object.c
+++ b/lib/object.c
@@ -60,7 +60,7 @@ got_object_id_str(char **outbuf, struct got_object_id *id)
 
 	*outbuf = calloc(1, len);
 	if (*outbuf == NULL)
-		return got_error(GOT_ERR_NO_MEM);
+		return got_error_from_errno();
 
 	if (got_sha1_digest_to_str(id->sha1, *outbuf, len) == NULL) {
 		free(*outbuf);
@@ -152,7 +152,7 @@ parse_object_header(struct got_object **obj, char *buf, size_t len)
 
 	*obj = calloc(1, sizeof(**obj));
 	if (*obj == NULL)
-		return got_error(GOT_ERR_NO_MEM);
+		return got_error_from_errno();
 	(*obj)->type = type;
 	(*obj)->hdrlen = hdrlen;
 	(*obj)->size = size;
@@ -172,7 +172,7 @@ read_object_header(struct got_object **obj, struct got_repository *repo,
 
 	buf = calloc(zbsize, sizeof(char));
 	if (buf == NULL)
-		return got_error(GOT_ERR_NO_MEM);
+		return got_error_from_errno();
 
 	err = got_inflate_init(&zb, NULL, zbsize);
 	if (err)
@@ -187,7 +187,7 @@ read_object_header(struct got_object **obj, struct got_repository *repo,
 		if (strchr(zb.outbuf, '\0') == NULL) {
 			buf = recallocarray(buf, 1 + i, 2 + i, zbsize);
 			if (buf == NULL) {
-				err = got_error(GOT_ERR_NO_MEM);
+				err = got_error_from_errno();
 				goto done;
 			}
 		}
@@ -210,7 +210,7 @@ object_path(char **path, struct got_object_id *id, struct got_repository *repo)
 	char *path_objects = got_repo_get_path_objects(repo);
 
 	if (path_objects == NULL)
-		return got_error(GOT_ERR_NO_MEM);
+		return got_error_from_errno();
 
 	err = got_object_id_str(&hex, id);
 	if (err)
@@ -218,7 +218,7 @@ object_path(char **path, struct got_object_id *id, struct got_repository *repo)
 
 	if (asprintf(path, "%s/%.2x/%s", path_objects,
 	    id->sha1[0], hex + 2) == -1)
-		err = got_error(GOT_ERR_NO_MEM);
+		err = got_error_from_errno();
 
 	free(hex);
 	free(path_objects);
@@ -319,12 +319,13 @@ 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);
+		return got_error_from_errno();
 	(*commit)->tree_id = calloc(1, sizeof(*(*commit)->tree_id));
 	if ((*commit)->tree_id == NULL) {
+		err = got_error_from_errno();
 		free(*commit);
 		*commit = NULL;
-		return got_error(GOT_ERR_NO_MEM);
+		return err;
 	}
 
 	SIMPLEQ_INIT(&(*commit)->parent_ids);
@@ -360,13 +361,13 @@ parse_commit_object(struct got_commit_object **commit, char *buf, size_t len)
 
 		pid = calloc(1, sizeof(*pid));
 		if (pid == NULL) {
-			err = got_error(GOT_ERR_NO_MEM);
+			err = got_error_from_errno();
 			goto done;
 		}
 		pid->id = calloc(1, sizeof(*pid->id));
 		if (pid->id == NULL) {
+			err = got_error_from_errno();
 			free(pid);
-			err = got_error(GOT_ERR_NO_MEM);
 			goto done;
 		}
 		s += tlen;
@@ -401,7 +402,7 @@ parse_commit_object(struct got_commit_object **commit, char *buf, size_t len)
 		*p = '\0';
 		(*commit)->author = strdup(s);
 		if ((*commit)->author == NULL) {
-			err = got_error(GOT_ERR_NO_MEM);
+			err = got_error_from_errno();
 			goto done;
 		}
 		s += strlen((*commit)->author) + 1;
@@ -426,7 +427,7 @@ parse_commit_object(struct got_commit_object **commit, char *buf, size_t len)
 		*p = '\0';
 		(*commit)->committer = strdup(s);
 		if ((*commit)->committer == NULL) {
-			err = got_error(GOT_ERR_NO_MEM);
+			err = got_error_from_errno();
 			goto done;
 		}
 		s += strlen((*commit)->committer) + 1;
@@ -435,7 +436,7 @@ parse_commit_object(struct got_commit_object **commit, char *buf, size_t len)
 
 	(*commit)->logmsg = strndup(s, remain);
 	if ((*commit)->logmsg == NULL) {
-		err = got_error(GOT_ERR_NO_MEM);
+		err = got_error_from_errno();
 		goto done;
 	}
 done:
@@ -463,13 +464,14 @@ parse_tree_entry(struct got_tree_entry **te, size_t *elen, char *buf,
 
 	*te = calloc(1, sizeof(**te));
 	if (*te == NULL)
-		return got_error(GOT_ERR_NO_MEM);
+		return got_error_from_errno();
 
 	(*te)->id = calloc(1, sizeof(*(*te)->id));
 	if ((*te)->id == NULL) {
+		err = got_error_from_errno();
 		free(*te);
 		*te = NULL;
-		return got_error(GOT_ERR_NO_MEM);
+		return err;
 	}
 
 	*elen = strlen(buf) + 1;
@@ -481,9 +483,10 @@ parse_tree_entry(struct got_tree_entry **te, size_t *elen, char *buf,
 
 	space = strchr(buf, ' ');
 	if (space == NULL) {
+		err = got_error(GOT_ERR_BAD_OBJ_DATA);
 		free(*te);
 		*te = NULL;
-		return got_error(GOT_ERR_BAD_OBJ_DATA);
+		return err;
 	}
 	while (*p != ' ') {
 		if (*p < '0' && *p > '7') {
@@ -520,7 +523,7 @@ parse_tree_object(struct got_tree_object **tree, struct got_repository *repo,
 
 	*tree = calloc(1, sizeof(**tree));
 	if (*tree == NULL)
-		return got_error(GOT_ERR_NO_MEM);
+		return got_error_from_errno();
 
 	SIMPLEQ_INIT(&(*tree)->entries);
 
@@ -558,7 +561,7 @@ read_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
 
 	buf = calloc(1, blocksize);
 	if (buf == NULL)
-		return got_error(GOT_ERR_NO_MEM);
+		return got_error_from_errno();
 
 	remain = blocksize;
 	total = 0;
@@ -567,7 +570,7 @@ read_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
 			uint8_t *newbuf;
 			newbuf = reallocarray(buf, 1, total + blocksize);
 			if (newbuf == NULL) {
-				err = got_error(GOT_ERR_NO_MEM);
+				err = got_error_from_errno();
 				goto done;
 			}
 			buf = newbuf;
@@ -755,14 +758,15 @@ got_object_blob_open(struct got_blob_object **blob,
 
 	*blob = calloc(1, sizeof(**blob));
 	if (*blob == NULL)
-		return got_error(GOT_ERR_NO_MEM);
+		return got_error_from_errno();
 
 	if (obj->flags & GOT_OBJ_FLAG_PACKED) {
 		(*blob)->read_buf = calloc(1, blocksize);
 		if ((*blob)->read_buf == NULL) {
+			err = got_error_from_errno();
 			free(*blob);
 			*blob = NULL;
-			return got_error(GOT_ERR_NO_MEM);
+			return err;
 		}
 		err = got_packfile_extract_object(&((*blob)->f), obj, repo);
 		if (err) {
diff --git a/lib/pack.c b/lib/pack.c
index 13400cd..f9c3ce7 100644
--- a/lib/pack.c
+++ b/lib/pack.c
@@ -89,7 +89,7 @@ get_packfile_size(size_t *size, const char *path_idx)
 		return got_error(GOT_ERR_BAD_PATH);
 	*dot = '\0';
 	if (asprintf(&path_pack, "%s.pack", base_path) == -1)
-		return got_error(GOT_ERR_NO_MEM);
+		return got_error_from_errno();
 
 	if (stat(path_pack, &sb) != 0) {
 		free(path_pack);
@@ -123,7 +123,7 @@ got_packidx_open(struct got_packidx_v2_hdr **packidx, const char *path)
 
 	p = calloc(1, sizeof(*p));
 	if (p == NULL) {
-		err = got_error(GOT_ERR_NO_MEM);
+		err = got_error_from_errno();
 		goto done;
 	}
 
@@ -169,7 +169,7 @@ got_packidx_open(struct got_packidx_v2_hdr **packidx, const char *path)
 
 	p->sorted_ids = calloc(nobj, sizeof(*p->sorted_ids));
 	if (p->sorted_ids == NULL) {
-		err = got_error(GOT_ERR_NO_MEM);
+		err = got_error_from_errno();
 		goto done;
 	}
 
@@ -184,7 +184,7 @@ got_packidx_open(struct got_packidx_v2_hdr **packidx, const char *path)
 
 	p->crc32 = calloc(nobj, sizeof(*p->crc32));
 	if (p->crc32 == NULL) {
-		err = got_error(GOT_ERR_NO_MEM);
+		err = got_error_from_errno();
 		goto done;
 	}
 
@@ -198,7 +198,7 @@ got_packidx_open(struct got_packidx_v2_hdr **packidx, const char *path)
 
 	p->offsets = calloc(nobj, sizeof(*p->offsets));
 	if (p->offsets == NULL) {
-		err = got_error(GOT_ERR_NO_MEM);
+		err = got_error_from_errno();
 		goto done;
 	}
 
@@ -216,7 +216,7 @@ got_packidx_open(struct got_packidx_v2_hdr **packidx, const char *path)
 
 	p->large_offsets = calloc(nobj, sizeof(*p->large_offsets));
 	if (p->large_offsets == NULL) {
-		err = got_error(GOT_ERR_NO_MEM);
+		err = got_error_from_errno();
 		goto done;
 	}
 
@@ -404,8 +404,6 @@ search_packidx(struct got_packidx_v2_hdr **packidx, int *idx,
 		*idx = get_object_idx(repo->packidx_cache[i], id);
 		if (*idx != -1) {
 			*packidx = repo->packidx_cache[i];
-			if (*packidx == NULL)
-				return got_error(GOT_ERR_NO_MEM);
 			return NULL;
 		}
 	}
@@ -413,7 +411,7 @@ search_packidx(struct got_packidx_v2_hdr **packidx, int *idx,
 
 	path_packdir = got_repo_get_path_objects_pack(repo);
 	if (path_packdir == NULL)
-		return got_error(GOT_ERR_NO_MEM);
+		return got_error_from_errno();
 
 	packdir = opendir(path_packdir);
 	if (packdir == NULL) {
@@ -427,7 +425,7 @@ search_packidx(struct got_packidx_v2_hdr **packidx, int *idx,
 
 		if (asprintf(&path_packidx, "%s/%s", path_packdir,
 		    dent->d_name) == -1) {
-			err = got_error(GOT_ERR_NO_MEM);
+			err = got_error_from_errno();
 			goto done;
 		}
 
@@ -465,7 +463,7 @@ get_packfile_path(char **path_packfile, struct got_repository *repo,
 
 	path_packdir = got_repo_get_path_objects_pack(repo);
 	if (path_packdir == NULL)
-		return got_error(GOT_ERR_NO_MEM);
+		return got_error_from_errno();
 
 	sha1str = got_sha1_digest_to_str(packidx->trailer.packfile_sha1,
 	    hex, sizeof(hex));
@@ -475,7 +473,7 @@ get_packfile_path(char **path_packfile, struct got_repository *repo,
 	if (asprintf(path_packfile, "%s/%s%s%s", path_packdir,
 	    GOT_PACK_PREFIX, sha1str, GOT_PACKFILE_SUFFIX) == -1) {
 		*path_packfile = NULL;
-		return got_error(GOT_ERR_NO_MEM);
+		return got_error_from_errno();
 	}
 
 	return NULL;
@@ -565,7 +563,7 @@ cache_pack(struct got_pack **packp, const char *path_packfile,
 
 	pack->path_packfile = strdup(path_packfile);
 	if (pack->path_packfile == NULL) {
-		err = got_error(GOT_ERR_NO_MEM);
+		err = got_error_from_errno();
 		goto done;
 	}
 
@@ -643,13 +641,14 @@ open_plain_object(struct got_object **obj, const char *path_packfile,
 {
 	*obj = calloc(1, sizeof(**obj));
 	if (*obj == NULL)
-		return got_error(GOT_ERR_NO_MEM);
+		return got_error_from_errno();
 
 	(*obj)->path_packfile = strdup(path_packfile);
 	if ((*obj)->path_packfile == NULL) {
+		const struct got_error *err = got_error_from_errno();
 		free(*obj);
 		*obj = NULL;
-		return got_error(GOT_ERR_NO_MEM);
+		return err;
 	}
 
 	(*obj)->type = type;
@@ -728,7 +727,7 @@ add_delta(struct got_delta_chain *deltas, const char *path_packfile,
 	    delta_type, delta_size, delta_data_offset, delta_buf,
 	    delta_len);
 	if (delta == NULL)
-		return got_error(GOT_ERR_NO_MEM);
+		return got_error_from_errno();
 	/* delta is freed in got_object_close() */
 	deltas->nentries++;
 	SIMPLEQ_INSERT_HEAD(&deltas->entries, delta, entry);
@@ -899,7 +898,7 @@ open_delta_object(struct got_object **obj, struct got_repository *repo,
 
 	*obj = calloc(1, sizeof(**obj));
 	if (*obj == NULL)
-		return got_error(GOT_ERR_NO_MEM);
+		return got_error_from_errno();
 
 	(*obj)->flags = 0;
 	(*obj)->hdrlen = 0;
@@ -909,7 +908,7 @@ open_delta_object(struct got_object **obj, struct got_repository *repo,
 
 	(*obj)->path_packfile = strdup(path_packfile);
 	if ((*obj)->path_packfile == NULL) {
-		err = got_error(GOT_ERR_NO_MEM);
+		err = got_error_from_errno();
 		goto done;
 	}
 	(*obj)->flags |= GOT_OBJ_FLAG_PACKED;
@@ -1070,7 +1069,7 @@ dump_delta_chain_to_file(size_t *result_size, struct got_delta_chain *deltas,
 	if (max_size < GOT_DELTA_RESULT_SIZE_CACHED_MAX) {
 		accum_buf = malloc(max_size);
 		if (accum_buf == NULL)
-			return got_error(GOT_ERR_NO_MEM);
+			return got_error_from_errno();
 	} else {
 		base_file = got_opentemp();
 		if (base_file == NULL)
@@ -1113,7 +1112,7 @@ dump_delta_chain_to_file(size_t *result_size, struct got_delta_chain *deltas,
 					uint8_t *p;
 					p = reallocarray(base_buf, 1, max_size);
 					if (p == NULL) {
-						err = got_error(GOT_ERR_NO_MEM);
+						err = got_error_from_errno();
 						goto done;
 					}
 					base_buf = p;
@@ -1198,7 +1197,7 @@ dump_delta_chain_to_mem(uint8_t **outbuf, size_t *outlen,
 		return err;
 	accum_buf = malloc(max_size);
 	if (accum_buf == NULL)
-		return got_error(GOT_ERR_NO_MEM);
+		return got_error_from_errno();
 
 	/* Deltas are ordered in ascending order. */
 	SIMPLEQ_FOREACH(delta, &deltas->entries, entry) {
@@ -1225,7 +1224,7 @@ dump_delta_chain_to_mem(uint8_t **outbuf, size_t *outlen,
 				uint8_t *p;
 				p = reallocarray(base_buf, 1, max_size);
 				if (p == NULL) {
-					err = got_error(GOT_ERR_NO_MEM);
+					err = got_error_from_errno();
 					goto done;
 				}
 				base_buf = p;
diff --git a/lib/path.c b/lib/path.c
index ddd0a1e..8f96a5a 100644
--- a/lib/path.c
+++ b/lib/path.c
@@ -71,7 +71,7 @@ got_path_segment_count(int *count, const char *path)
 	*count = 0;
 
 	if (s == NULL)
-		return got_error(GOT_ERR_NO_MEM);
+		return got_error_from_errno();
 
 	do {
 		p = strsep(&s, "/");
@@ -114,7 +114,7 @@ got_opentemp_named(char **path, FILE **outfile, const char *basepath)
 
 	if (asprintf(path, "%s-XXXXXX", basepath) == -1) {
 		*path = NULL;
-		return got_error(GOT_ERR_NO_MEM);
+		return got_error_from_errno();
 	}
 
 	fd = mkstemp(*path);
diff --git a/lib/refs.c b/lib/refs.c
index 07d6b87..fd17969 100644
--- a/lib/refs.c
+++ b/lib/refs.c
@@ -70,16 +70,17 @@ parse_symref(struct got_reference **ref, const char *name, const char *line)
 
 	symref_name = strdup(name);
 	if (symref_name == NULL)
-		return got_error(GOT_ERR_NO_MEM);
+		return got_error_from_errno();
 	symref_ref = strdup(line);
 	if (symref_ref == NULL) {
+		const struct got_error *err = got_error_from_errno();
 		free(symref_name);
-		return got_error(GOT_ERR_NO_MEM);
+		return err;
 	}
 
 	*ref = calloc(1, sizeof(**ref));
 	if (*ref == NULL)
-		return got_error(GOT_ERR_NO_MEM);
+		return got_error_from_errno();
 	(*ref)->flags |= GOT_REF_IS_SYMBOLIC;
 	symref = &((*ref)->ref.symref);
 	symref->name = symref_name;
@@ -100,14 +101,14 @@ parse_ref_line(struct got_reference **ref, const char *name, const char *line)
 
 	ref_name = strdup(name);
 	if (ref_name == NULL)
-		return got_error(GOT_ERR_NO_MEM);
+		return got_error_from_errno();
 
 	if (!got_parse_sha1_digest(digest, line))
 		return got_error(GOT_ERR_NOT_REF);
 
 	*ref = calloc(1, sizeof(**ref));
 	if (*ref == NULL)
-		return got_error(GOT_ERR_NO_MEM);
+		return got_error_from_errno();
 	(*ref)->ref.ref.name = ref_name;
 	memcpy(&(*ref)->ref.ref.sha1, digest, SHA1_DIGEST_LENGTH);
 	return NULL;
@@ -162,14 +163,14 @@ got_ref_open(struct got_reference **ref, struct got_repository *repo,
 	char *path_refs = get_refs_dir_path(repo, refname);
 
 	if (path_refs == NULL) {
-		err = got_error(GOT_ERR_NO_MEM);
+		err = got_error_from_errno();
 		goto done;
 	}
 	
 	/* XXX For now, this assumes that refs exist in the filesystem. */
 
 	if (asprintf(&path_ref, "%s/%s", path_refs, refname) == -1) {
-		err = got_error(GOT_ERR_NO_MEM);
+		err = got_error_from_errno();
 		goto done;
 	}
 
@@ -269,7 +270,7 @@ got_ref_resolve(struct got_object_id **id, struct got_repository *repo,
 
 	*id = calloc(1, sizeof(**id));
 	if (*id == NULL)
-		return got_error(GOT_ERR_NO_MEM);
+		return got_error_from_errno();
 	memcpy((*id)->sha1, ref->ref.ref.sha1, SHA1_DIGEST_LENGTH);
 	return NULL;
 }
diff --git a/lib/repository.c b/lib/repository.c
index 9dea7fe..086900f 100644
--- a/lib/repository.c
+++ b/lib/repository.c
@@ -161,7 +161,7 @@ got_repo_open(struct got_repository **ret, const char *path)
 
 	repo = calloc(1, sizeof(*repo));
 	if (repo == NULL) {
-		err = got_error(GOT_ERR_NO_MEM);
+		err = got_error_from_errno();
 		goto done;
 	}
 
@@ -173,14 +173,14 @@ got_repo_open(struct got_repository **ret, const char *path)
 
 	repo->path_git_dir = strdup(repo->path);
 	if (repo->path_git_dir == NULL) {
-		err = got_error(GOT_ERR_NO_MEM);
+		err = got_error_from_errno();
 		goto done;
 	}
 	if (!is_git_repo(repo)) {
 		free(repo->path_git_dir);
 		if (asprintf(&repo->path_git_dir, "%s/%s", repo->path,
 		    GOT_GIT_DIR) == -1) {
-			err = got_error(GOT_ERR_NO_MEM);
+			err = got_error_from_errno();
 			goto done;
 		}
 		if (!is_git_repo(repo)) {
diff --git a/lib/worktree.c b/lib/worktree.c
index 63c5da8..80b9f97 100644
--- a/lib/worktree.c
+++ b/lib/worktree.c
@@ -56,7 +56,7 @@ create_meta_file(const char *path_got, const char *name, const char *content)
 	ssize_t n;
 
 	if (asprintf(&path, "%s/%s", path_got, name) == -1) {
-		err = got_error(GOT_ERR_NO_MEM);
+		err = got_error_from_errno();
 		path = NULL;
 		goto done;
 	}
@@ -103,7 +103,7 @@ read_meta_file(char **content, const char *path_got, const char *name)
 	*content = NULL;
 
 	if (asprintf(&path, "%s/%s", path_got, name) == -1) {
-		err = got_error(GOT_ERR_NO_MEM);
+		err = got_error_from_errno();
 		path = NULL;
 		goto done;
 	}
@@ -122,7 +122,7 @@ read_meta_file(char **content, const char *path_got, const char *name)
 	stat(path, &sb);
 	*content = calloc(1, sb.st_size);
 	if (*content == NULL) {
-		err = got_error(GOT_ERR_NO_MEM);
+		err = got_error_from_errno();
 		goto done;
 	}
 
@@ -162,7 +162,7 @@ got_worktree_init(const char *path, struct got_reference *head_ref,
 
 	if (!got_path_is_absolute(prefix)) {
 		if (asprintf(&absprefix, "/%s", prefix) == -1)
-			return got_error(GOT_ERR_NO_MEM);
+			return got_error_from_errno();
 	}
 
 	/* Create top-level directory (may already exist). */
@@ -173,7 +173,7 @@ got_worktree_init(const char *path, struct got_reference *head_ref,
 
 	/* Create .got directory (may already exist). */
 	if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
-		err = got_error(GOT_ERR_NO_MEM);
+		err = got_error_from_errno();
 		goto done;
 	}
 	if (mkdir(path_got, GOT_DEFAULT_DIR_MODE) == -1 && errno != EEXIST) {
@@ -194,7 +194,7 @@ got_worktree_init(const char *path, struct got_reference *head_ref,
 	/* Write the HEAD reference. */
 	refstr = got_ref_to_str(head_ref);
 	if (refstr == NULL) {
-		err = got_error(GOT_ERR_NO_MEM);
+		err = got_error_from_errno();
 		goto done;
 	}
 	err = create_meta_file(path_got, GOT_WORKTREE_HEAD, refstr);
@@ -204,7 +204,7 @@ got_worktree_init(const char *path, struct got_reference *head_ref,
 	/* Store path to repository. */
 	repo_path = got_repo_get_path(repo);
 	if (repo_path == NULL) {
-		err = got_error(GOT_ERR_NO_MEM);
+		err = got_error_from_errno();
 		goto done;
 	}
 	err = create_meta_file(path_got, GOT_WORKTREE_REPOSITORY, repo_path);
@@ -219,7 +219,7 @@ got_worktree_init(const char *path, struct got_reference *head_ref,
 
 	/* Stamp work tree with format file. */
 	if (asprintf(&formatstr, "%d", GOT_WORKTREE_FORMAT_VERSION) == -1) {
-		err = got_error(GOT_ERR_NO_MEM);
+		err = got_error_from_errno();
 		goto done;
 	}
 	err = create_meta_file(path_got, GOT_WORKTREE_FORMAT, formatstr);
@@ -248,13 +248,13 @@ got_worktree_open(struct got_worktree **worktree, const char *path)
 	*worktree = NULL;
 
 	if (asprintf(&path_got, "%s/%s", path, GOT_WORKTREE_GOT_DIR) == -1) {
-		err = got_error(GOT_ERR_NO_MEM);
+		err = got_error_from_errno();
 		path_got = NULL;
 		goto done;
 	}
 
 	if (asprintf(&path_lock, "%s/%s", path_got, GOT_WORKTREE_LOCK) == -1) {
-		err = got_error(GOT_ERR_NO_MEM);
+		err = got_error_from_errno();
 		path_lock = NULL;
 		goto done;
 	}
@@ -282,14 +282,14 @@ got_worktree_open(struct got_worktree **worktree, const char *path)
 
 	*worktree = calloc(1, sizeof(**worktree));
 	if (*worktree == NULL) {
-		err = got_error(GOT_ERR_NO_MEM);
+		err = got_error_from_errno();
 		goto done;
 	}
 	(*worktree)->lockfd = -1;
 
 	(*worktree)->root_path = strdup(path);
 	if ((*worktree)->root_path == NULL) {
-		err = got_error(GOT_ERR_NO_MEM);
+		err = got_error_from_errno();
 		goto done;
 	}
 	err = read_meta_file(&(*worktree)->repo_path, path_got,
@@ -377,7 +377,7 @@ add_file_on_disk(struct got_worktree *worktree, struct got_fileindex *fileindex,
 
 	if (asprintf(&ondisk_path, "%s/%s", worktree->root_path,
 	    apply_path_prefix(worktree, path)) == -1)
-		return got_error(GOT_ERR_NO_MEM);
+		return got_error_from_errno();
 
 	fd = open(ondisk_path, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW,
 	    GOT_DEFAULT_FILE_MODE);
@@ -439,7 +439,7 @@ add_dir_on_disk(struct got_worktree *worktree, const char *path)
 
 	if (asprintf(&abspath, "%s/%s", worktree->root_path,
 	    apply_path_prefix(worktree, path)) == -1)
-		return got_error(GOT_ERR_NO_MEM);
+		return got_error_from_errno();
 
 	/* XXX queue work rather than editing disk directly? */
 	if (mkdir(abspath, GOT_DEFAULT_DIR_MODE) == -1) {
@@ -487,7 +487,7 @@ tree_checkout_entry(struct got_worktree *worktree,
 	if (parent[0] == '/' && parent[1] == '\0')
 		parent = "";
 	if (asprintf(&path, "%s/%s", parent, te->name) == -1)
-		return got_error(GOT_ERR_NO_MEM);
+		return got_error_from_errno();
 
 	/* Skip this entry if it is outside of our path prefix. */
 	len = MIN(strlen(worktree->path_prefix), strlen(path));
@@ -583,13 +583,13 @@ got_worktree_checkout_files(struct got_worktree *worktree,
 
 	fileindex = got_fileindex_open();
 	if (fileindex == NULL) {
-		err = got_error(GOT_ERR_NO_MEM);
+		err = got_error_from_errno();
 		goto done;
 	}
 
 	if (asprintf(&fileindex_path, "%s/%s/%s", worktree->root_path,
 	    GOT_WORKTREE_GOT_DIR, GOT_WORKTREE_FILE_INDEX) == -1) {
-		err = got_error(GOT_ERR_NO_MEM);
+		err = got_error_from_errno();
 		fileindex_path = NULL;
 		goto done;
 	}
diff --git a/lib/zbuf.c b/lib/zbuf.c
index 9024264..93279f0 100644
--- a/lib/zbuf.c
+++ b/lib/zbuf.c
@@ -46,14 +46,14 @@ got_inflate_init(struct got_zstream_buf *zb, uint8_t *outbuf, size_t bufsize)
 
 	zb->inbuf = calloc(1, zb->inlen);
 	if (zb->inbuf == NULL) {
-		err = got_error(GOT_ERR_NO_MEM);
+		err = got_error_from_errno();
 		goto done;
 	}
 
 	if (outbuf == NULL) {
 		zb->outbuf = calloc(1, zb->outlen);
 		if (zb->outbuf == NULL) {
-			err = got_error(GOT_ERR_NO_MEM);
+			err = got_error_from_errno();
 			goto done;
 		}
 		zb->flags |= GOT_ZSTREAM_F_OWN_OUTBUF;
@@ -122,7 +122,7 @@ got_inflate_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
 
 	*outbuf = calloc(1, GOT_ZSTREAM_BUFSIZE);
 	if (*outbuf == NULL)
-		return got_error(GOT_ERR_NO_MEM);
+		return got_error_from_errno();
 	err = got_inflate_init(&zb, *outbuf, GOT_ZSTREAM_BUFSIZE);
 	if (err)
 		return err;
@@ -138,10 +138,10 @@ got_inflate_to_mem(uint8_t **outbuf, size_t *outlen, FILE *f)
 			newbuf = reallocarray(*outbuf, 1,
 			    *outlen + GOT_ZSTREAM_BUFSIZE);
 			if (newbuf == NULL) {
+				err = got_error_from_errno();
 				free(*outbuf);
 				*outbuf = NULL;
 				*outlen = 0;
-				err = got_error(GOT_ERR_NO_MEM);
 				goto done;
 			}
 			*outbuf = newbuf;
diff --git a/regress/repository/repository_test.c b/regress/repository/repository_test.c
index 6f6b30c..d822baa 100644
--- a/regress/repository/repository_test.c
+++ b/regress/repository/repository_test.c
@@ -112,7 +112,7 @@ print_tree_object(struct got_object *obj, char *parent,
 		}
 
 		if (asprintf(&next_parent, "%s/%s", parent, te->name) == -1) {
-			err = got_error(GOT_ERR_NO_MEM);
+			err = got_error_from_errno();
 			got_object_close(treeobj);
 			break;
 		}