Commit Diff


commit - 15d3ea5655eca208cfecf4e42d2f3dbda1842eff
commit + d5003b790484caa6509175907d44dedd373b59d3
blob - 23a4cf3f57651c3cf747e03f36514dcb571733e6
blob + 425c26d14098f9f28e48521cb347a23089a2264d
--- lib/object.c
+++ lib/object.c
@@ -309,7 +309,7 @@ object_path(char **path, struct got_object_id *id, str
 }
 
 static const struct got_error *
-open_loose_object(FILE **f, struct got_object *obj, struct got_repository *repo)
+open_loose_object(int *fd, struct got_object *obj, struct got_repository *repo)
 {
 	const struct got_error *err = NULL;
 	char *path;
@@ -317,8 +317,8 @@ open_loose_object(FILE **f, struct got_object *obj, st
 	err = object_path(&path, &obj->id, repo);
 	if (err)
 		return err;
-	*f = fopen(path, "rb");
-	if (*f == NULL) {
+	*fd = open(path, O_RDONLY | O_NOFOLLOW, GOT_DEFAULT_FILE_MODE);
+	if (*fd == -1) {
 		err = got_error_from_errno();
 		goto done;
 	}
@@ -728,9 +728,16 @@ got_object_commit_open(struct got_commit_object **comm
 		free(buf);
 	} else {
 		FILE *f;
-		err = open_loose_object(&f, obj, repo);
+		int fd;
+		err = open_loose_object(&fd, obj, repo);
 		if (err)
 			return err;
+		f = fdopen(fd, "rb");
+		if (f == NULL) {
+			err = got_error_from_errno();
+			close(fd);
+			return err;
+		}
 		err = read_commit_object(commit, repo, obj, f);
 		fclose(f);
 	}
@@ -804,11 +811,18 @@ got_object_tree_open(struct got_tree_object **tree,
 		free(buf);
 	} else {
 		FILE *f;
-		err = open_loose_object(&f, obj, repo);
+		int fd;
+		err = open_loose_object(&fd, obj, repo);
 		if (err)
 			return err;
+		f = fdopen(fd, "rb");
+		if (f == NULL) {
+			close(fd);
+			return got_error_from_errno();
+		}
 		err = read_tree_object(tree, repo, obj, f);
 		fclose(f);
+		close(fd);
 	}
 	return err;
 }
@@ -859,12 +873,20 @@ got_object_blob_open(struct got_blob_object **blob,
 			return err;
 		}
 	} else {
-		err = open_loose_object(&((*blob)->f), obj, repo);
+		int fd;
+		err = open_loose_object(&fd, obj, repo);
 		if (err) {
 			free(*blob);
 			*blob = NULL;
 			return err;
 		}
+		(*blob)->f = fdopen(fd, "rb");
+		if ((*blob)->f == NULL) {
+			free(*blob);
+			*blob = NULL;
+			close(fd);
+			return err;
+		}
 
 		err = got_inflate_init(&(*blob)->zb, NULL, blocksize);
 		if (err != NULL) {