libgit2 / libgit2/libgit2

Potential bug when handling relative paths.

Open
#6,392 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C
Stars
10.6k
Forks
2.7k
PR merge metrics
No merged PRs in 30d

Description

Reproduction steps

Compile the following code (gcc example.c -lgit2), and run it against an object directory.
If we supply an absolute path, the example works fine.
./a.out /home/elliott/repo
If a relative path is provided, the example failed. The git_odb_write_multi_pack_index complains path issues.
./a.out [repo]

A sample repo can be downloaded here. repo.zip

// This simple example just opens a repo odb and writes out a multi-index pack.

#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <git2.h>



struct Context {
	git_odb *db;
	git_odb_backend *backend;
	git_indexer *idxer;
};

int git_odb_foreach_cb_1(const git_oid *id, void *payload) {
	printf("%s\n", git_oid_tostr_s(id));
	return 0;
}

int main(int argc, char **argv) {

	struct Context c;

	if (argc != 2) {
		fprintf(stderr, "Usage: bug_example object_dir\n");
		return -1;
	}

	const char* obj_dir = argv[1];

	git_libgit2_init();

	if (git_odb_new(&c.db)) {
		fprintf(stderr, "Unable to open db\n");
		return -1;
	}

	if (git_odb_backend_pack(&c.backend, obj_dir)) {
		fprintf(stderr, "Unable to open backend\n");
		return -1;
	}

	if (git_odb_add_backend(c.db, c.backend, 1)) {
		fprintf(stderr, "Unable to add backend\n");
		return -1;
	}

	if (git_odb_foreach(c.db, git_odb_foreach_cb_1, &c)) {
		fprintf(stderr, "Unable to walk repo\n");
		return -1;
	}


	if (git_odb_write_multi_pack_index(c.db)) {
		fprintf(stderr, "Could not open repository: %s\n", git_error_last()->message);
		return -1;
	}

	return 0;
	
}
Expected behavior

Works on relative path and absolute path.

Actual behavior

If we supply an absolute path, the example works fine.
If a relative path is provided, the example failed. The git_odb_write_multi_pack_index complains path issues.

Version of libgit2 (release number or SHA1)

Tested on 1.5 and 1.4

Operating system(s) tested

Ubuntu 22.04

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by compiling the supplied C example against libgit2 1.5 or 1.4 and run it with the linked sample repository, comparing absolute and relative object-directory paths. Trace the git_odb_backend_pack and git_odb_write_multi_pack_index calls to identify where the relative path fails; done means both invocation forms work without the path error.

Written by the indexing model from the issue text.

Assessment

Tech stack
c, git
Domain
database
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.