From 2f4dbf1848f3ec7683d44022b8da06462a288ff6 Mon Sep 17 00:00:00 2001 From: Berthold Stoeger Date: Thu, 29 Feb 2024 13:57:26 +0100 Subject: [PATCH] core: make get_sha() return std::string This was crazy: it returned a local static buffer, i.e. was inherently non-reentrant. Signed-off-by: Berthold Stoeger --- core/file.cpp | 4 ++-- core/git-access.h | 2 +- core/load-git.cpp | 8 ++++---- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/core/file.cpp b/core/file.cpp index f1cdc5215..901e465e8 100644 --- a/core/file.cpp +++ b/core/file.cpp @@ -269,9 +269,9 @@ extern "C" bool remote_repo_uptodate(const char *filename, struct git_info *info std::string current_sha = saved_git_id; if (is_git_repository(filename, info) && open_git_repository(info)) { - const char *sha = get_sha(info->repo, info->branch); + std::string sha = get_sha(info->repo, info->branch); if (!sha.empty() && current_sha == sha) { - fprintf(stderr, "already have loaded SHA %s - don't load again\n", sha); + fprintf(stderr, "already have loaded SHA %s - don't load again\n", sha.c_str()); return true; } } diff --git a/core/git-access.h b/core/git-access.h index 0aec388cd..38bac45c2 100644 --- a/core/git-access.h +++ b/core/git-access.h @@ -42,7 +42,6 @@ extern bool remote_repo_uptodate(const char *filename, struct git_info *info); extern int sync_with_remote(struct git_info *); extern int git_save_dives(struct git_info *, bool select_only); extern int git_load_dives(struct git_info *, struct divelog *log); -extern const char *get_sha(git_repository *repo, const char *branch); extern int do_git_save(struct git_info *, bool select_only, bool create_empty); extern void cleanup_git_info(struct git_info *); extern bool git_local_only; @@ -60,6 +59,7 @@ int get_authorship(git_repository *repo, git_signature **authorp); #include extern std::string saved_git_id; +extern std::string get_sha(git_repository *repo, const char *branch); #endif #endif // GITACCESS_H diff --git a/core/load-git.cpp b/core/load-git.cpp index 41c166aeb..092111b3d 100644 --- a/core/load-git.cpp +++ b/core/load-git.cpp @@ -1906,14 +1906,14 @@ static int do_git_load(git_repository *repo, const char *branch, struct git_pars return ret; } -extern "C" const char *get_sha(git_repository *repo, const char *branch) +std::string get_sha(git_repository *repo, const char *branch) { - static char git_id_buffer[GIT_OID_HEXSZ + 1]; + char git_id_buffer[GIT_OID_HEXSZ + 1]; git_commit *commit; if (find_commit(repo, branch, &commit)) - return NULL; + return std::string(); git_oid_tostr(git_id_buffer, sizeof(git_id_buffer), (const git_oid *)commit); - return git_id_buffer; + return std::string(git_id_buffer); } /*