Unverified Commit efc4e7e5 by Edward Thomson Committed by GitHub

Merge pull request #5802 from lhchavez/git-warn-unused-result

Introduce GIT_WARN_UNUSED_RESULT
parents 0850b172 991ccdc5
...@@ -43,7 +43,15 @@ ...@@ -43,7 +43,15 @@
# define GIT_ALIGN(x,size) x # define GIT_ALIGN(x,size) x
#endif #endif
#define GIT_UNUSED(x) ((void)(x)) #if defined(__GNUC__)
# define GIT_UNUSED(x) \
do { \
typeof(x) _unused __attribute__((unused)); \
_unused = (x); \
} while (0)
#else
# define GIT_UNUSED(x) ((void)(x))
#endif
/* Define the printf format specifier to use for size_t output */ /* Define the printf format specifier to use for size_t output */
#if defined(_MSC_VER) || defined(__MINGW32__) #if defined(_MSC_VER) || defined(__MINGW32__)
......
...@@ -30,6 +30,24 @@ ...@@ -30,6 +30,24 @@
# define __has_builtin(x) 0 # define __has_builtin(x) 0
#endif #endif
/**
* Declare that a function's return value must be used.
*
* Used mostly to guard against potential silent bugs at runtime. This is
* recommended to be added to functions that:
*
* - Allocate / reallocate memory. This prevents memory leaks or errors where
* buffers are expected to have grown to a certain size, but could not be
* resized.
* - Acquire locks. When a lock cannot be acquired, that will almost certainly
* cause a data race / undefined behavior.
*/
#if defined(__GNUC__)
# define GIT_WARN_UNUSED_RESULT __attribute__((warn_unused_result))
#else
# define GIT_WARN_UNUSED_RESULT
#endif
#include <assert.h> #include <assert.h>
#include <errno.h> #include <errno.h>
#include <limits.h> #include <limits.h>
......
...@@ -573,7 +573,7 @@ int git_odb__add_default_backends( ...@@ -573,7 +573,7 @@ int git_odb__add_default_backends(
git_odb *db, const char *objects_dir, git_odb *db, const char *objects_dir,
bool as_alternates, int alternate_depth) bool as_alternates, int alternate_depth)
{ {
size_t i; size_t i = 0;
struct stat st; struct stat st;
ino_t inode; ino_t inode;
git_odb_backend *loose, *packed; git_odb_backend *loose, *packed;
...@@ -582,7 +582,7 @@ int git_odb__add_default_backends( ...@@ -582,7 +582,7 @@ int git_odb__add_default_backends(
* a cross-platform workaround for this */ * a cross-platform workaround for this */
#ifdef GIT_WIN32 #ifdef GIT_WIN32
GIT_UNUSED(i); GIT_UNUSED(i);
GIT_UNUSED(st); GIT_UNUSED(&st);
inode = 0; inode = 0;
#else #else
......
...@@ -1915,7 +1915,13 @@ GIT_INLINE(bool) should_validate_longpaths(git_repository *repo) ...@@ -1915,7 +1915,13 @@ GIT_INLINE(bool) should_validate_longpaths(git_repository *repo)
} }
#else #else
# define should_validate_longpaths(repo) (GIT_UNUSED(repo), false)
GIT_INLINE(bool) should_validate_longpaths(git_repository *repo)
{
GIT_UNUSED(repo);
return false;
}
#endif #endif
int git_path_validate_workdir(git_repository *repo, const char *path) int git_path_validate_workdir(git_repository *repo, const char *path)
......
...@@ -122,7 +122,7 @@ static int packed_reload(refdb_fs_backend *backend) ...@@ -122,7 +122,7 @@ static int packed_reload(refdb_fs_backend *backend)
*/ */
if (error <= 0) { if (error <= 0) {
if (error == GIT_ENOTFOUND) { if (error == GIT_ENOTFOUND) {
git_sortedcache_clear(backend->refcache, true); GIT_UNUSED(git_sortedcache_clear(backend->refcache, true));
git_error_clear(); git_error_clear();
error = 0; error = 0;
} }
...@@ -131,7 +131,7 @@ static int packed_reload(refdb_fs_backend *backend) ...@@ -131,7 +131,7 @@ static int packed_reload(refdb_fs_backend *backend)
/* At this point, refresh the packed refs from the loaded buffer. */ /* At this point, refresh the packed refs from the loaded buffer. */
git_sortedcache_clear(backend->refcache, false); GIT_UNUSED(git_sortedcache_clear(backend->refcache, false));
scan = (char *)packedrefs.ptr; scan = (char *)packedrefs.ptr;
eof = scan + packedrefs.size; eof = scan + packedrefs.size;
...@@ -219,7 +219,7 @@ static int packed_reload(refdb_fs_backend *backend) ...@@ -219,7 +219,7 @@ static int packed_reload(refdb_fs_backend *backend)
parse_failed: parse_failed:
git_error_set(GIT_ERROR_REFERENCE, "corrupted packed references file"); git_error_set(GIT_ERROR_REFERENCE, "corrupted packed references file");
git_sortedcache_clear(backend->refcache, false); GIT_UNUSED(git_sortedcache_clear(backend->refcache, false));
git_sortedcache_wunlock(backend->refcache); git_sortedcache_wunlock(backend->refcache);
git_buf_dispose(&packedrefs); git_buf_dispose(&packedrefs);
......
...@@ -58,7 +58,7 @@ typedef struct { ...@@ -58,7 +58,7 @@ typedef struct {
* may be NULL. The cache makes it easy to load this and check * may be NULL. The cache makes it easy to load this and check
* if it has been modified since the last load and/or write. * if it has been modified since the last load and/or write.
*/ */
int git_sortedcache_new( GIT_WARN_UNUSED_RESULT int git_sortedcache_new(
git_sortedcache **out, git_sortedcache **out,
size_t item_path_offset, /* use offsetof(struct, path-field) macro */ size_t item_path_offset, /* use offsetof(struct, path-field) macro */
git_sortedcache_free_item_fn free_item, git_sortedcache_free_item_fn free_item,
...@@ -71,7 +71,7 @@ int git_sortedcache_new( ...@@ -71,7 +71,7 @@ int git_sortedcache_new(
* - `copy_item` can be NULL to just use memcpy * - `copy_item` can be NULL to just use memcpy
* - if `lock`, grabs read lock on `src` during copy and releases after * - if `lock`, grabs read lock on `src` during copy and releases after
*/ */
int git_sortedcache_copy( GIT_WARN_UNUSED_RESULT int git_sortedcache_copy(
git_sortedcache **out, git_sortedcache **out,
git_sortedcache *src, git_sortedcache *src,
bool lock, bool lock,
...@@ -100,7 +100,7 @@ const char *git_sortedcache_path(git_sortedcache *sc); ...@@ -100,7 +100,7 @@ const char *git_sortedcache_path(git_sortedcache *sc);
*/ */
/* Lock sortedcache for write */ /* Lock sortedcache for write */
int git_sortedcache_wlock(git_sortedcache *sc); GIT_WARN_UNUSED_RESULT int git_sortedcache_wlock(git_sortedcache *sc);
/* Unlock sorted cache when done with write */ /* Unlock sorted cache when done with write */
void git_sortedcache_wunlock(git_sortedcache *sc); void git_sortedcache_wunlock(git_sortedcache *sc);
...@@ -120,7 +120,8 @@ void git_sortedcache_wunlock(git_sortedcache *sc); ...@@ -120,7 +120,8 @@ void git_sortedcache_wunlock(git_sortedcache *sc);
* *
* @return 0 if up-to-date, 1 if out-of-date, <0 on error * @return 0 if up-to-date, 1 if out-of-date, <0 on error
*/ */
int git_sortedcache_lockandload(git_sortedcache *sc, git_buf *buf); GIT_WARN_UNUSED_RESULT int git_sortedcache_lockandload(
git_sortedcache *sc, git_buf *buf);
/* Refresh file timestamp after write completes /* Refresh file timestamp after write completes
* You should already be holding the write lock when you call this. * You should already be holding the write lock when you call this.
...@@ -132,12 +133,13 @@ void git_sortedcache_updated(git_sortedcache *sc); ...@@ -132,12 +133,13 @@ void git_sortedcache_updated(git_sortedcache *sc);
* If `wlock` is true, grabs write lock and releases when done, otherwise * If `wlock` is true, grabs write lock and releases when done, otherwise
* you should already be holding a write lock when you call this. * you should already be holding a write lock when you call this.
*/ */
int git_sortedcache_clear(git_sortedcache *sc, bool wlock); GIT_WARN_UNUSED_RESULT int git_sortedcache_clear(
git_sortedcache *sc, bool wlock);
/* Find and/or insert item, returning pointer to item data. /* Find and/or insert item, returning pointer to item data.
* You should already be holding the write lock when you call this. * You should already be holding the write lock when you call this.
*/ */
int git_sortedcache_upsert( GIT_WARN_UNUSED_RESULT int git_sortedcache_upsert(
void **out, git_sortedcache *sc, const char *key); void **out, git_sortedcache *sc, const char *key);
/* Removes entry at pos from cache /* Removes entry at pos from cache
...@@ -155,7 +157,7 @@ int git_sortedcache_remove(git_sortedcache *sc, size_t pos); ...@@ -155,7 +157,7 @@ int git_sortedcache_remove(git_sortedcache *sc, size_t pos);
*/ */
/* Lock sortedcache for read */ /* Lock sortedcache for read */
int git_sortedcache_rlock(git_sortedcache *sc); GIT_WARN_UNUSED_RESULT int git_sortedcache_rlock(git_sortedcache *sc);
/* Unlock sorted cache when done with read */ /* Unlock sorted cache when done with read */
void git_sortedcache_runlock(git_sortedcache *sc); void git_sortedcache_runlock(git_sortedcache *sc);
......
...@@ -26,11 +26,13 @@ typedef struct git_vector { ...@@ -26,11 +26,13 @@ typedef struct git_vector {
#define GIT_VECTOR_INIT {0} #define GIT_VECTOR_INIT {0}
int git_vector_init(git_vector *v, size_t initial_size, git_vector_cmp cmp); GIT_WARN_UNUSED_RESULT int git_vector_init(
git_vector *v, size_t initial_size, git_vector_cmp cmp);
void git_vector_free(git_vector *v); void git_vector_free(git_vector *v);
void git_vector_free_deep(git_vector *v); /* free each entry and self */ void git_vector_free_deep(git_vector *v); /* free each entry and self */
void git_vector_clear(git_vector *v); void git_vector_clear(git_vector *v);
int git_vector_dup(git_vector *v, const git_vector *src, git_vector_cmp cmp); GIT_WARN_UNUSED_RESULT int git_vector_dup(
git_vector *v, const git_vector *src, git_vector_cmp cmp);
void git_vector_swap(git_vector *a, git_vector *b); void git_vector_swap(git_vector *a, git_vector *b);
int git_vector_size_hint(git_vector *v, size_t size_hint); int git_vector_size_hint(git_vector *v, size_t size_hint);
......
...@@ -8,7 +8,6 @@ ...@@ -8,7 +8,6 @@
#define INCLUDE_win32_path_w32_h__ #define INCLUDE_win32_path_w32_h__
#include "common.h" #include "common.h"
#include "vector.h"
/** /**
* Create a Win32 path (in UCS-2 format) from a UTF-8 string. If the given * Create a Win32 path (in UCS-2 format) from a UTF-8 string. If the given
......
...@@ -52,7 +52,7 @@ void test_core_sha1__detect_collision_attack(void) ...@@ -52,7 +52,7 @@ void test_core_sha1__detect_collision_attack(void)
git_oid oid, expected; git_oid oid, expected;
#ifdef GIT_SHA1_COLLISIONDETECT #ifdef GIT_SHA1_COLLISIONDETECT
GIT_UNUSED(expected); GIT_UNUSED(&expected);
cl_git_fail(sha1_file(&oid, FIXTURE_DIR "/shattered-1.pdf")); cl_git_fail(sha1_file(&oid, FIXTURE_DIR "/shattered-1.pdf"));
cl_assert_equal_s("SHA1 collision attack detected", git_error_last()->message); cl_assert_equal_s("SHA1 collision attack detected", git_error_last()->message);
#else #else
......
...@@ -54,7 +54,7 @@ void test_core_sortedcache__name_only(void) ...@@ -54,7 +54,7 @@ void test_core_sortedcache__name_only(void)
cl_assert_equal_i( cl_assert_equal_i(
GIT_ENOTFOUND, git_sortedcache_lookup_index(&pos, sc, "abc")); GIT_ENOTFOUND, git_sortedcache_lookup_index(&pos, sc, "abc"));
git_sortedcache_clear(sc, true); cl_git_pass(git_sortedcache_clear(sc, true));
cl_assert_equal_sz(0, git_sortedcache_entrycount(sc)); cl_assert_equal_sz(0, git_sortedcache_entrycount(sc));
cl_assert(git_sortedcache_entry(sc, 0) == NULL); cl_assert(git_sortedcache_entry(sc, 0) == NULL);
...@@ -154,7 +154,7 @@ void test_core_sortedcache__in_memory(void) ...@@ -154,7 +154,7 @@ void test_core_sortedcache__in_memory(void)
cl_assert_equal_i(0, free_count); cl_assert_equal_i(0, free_count);
git_sortedcache_clear(sc, true); cl_git_pass(git_sortedcache_clear(sc, true));
cl_assert_equal_i(5, free_count); cl_assert_equal_i(5, free_count);
...@@ -247,7 +247,7 @@ static void sortedcache_test_reload(git_sortedcache *sc) ...@@ -247,7 +247,7 @@ static void sortedcache_test_reload(git_sortedcache *sc)
cl_assert(git_sortedcache_lockandload(sc, &buf) > 0); cl_assert(git_sortedcache_lockandload(sc, &buf) > 0);
git_sortedcache_clear(sc, false); /* clear once we already have lock */ cl_git_pass(git_sortedcache_clear(sc, false)); /* clear once we already have lock */
for (scan = buf.ptr; *scan; scan = after + 1) { for (scan = buf.ptr; *scan; scan = after + 1) {
int val = strtol(scan, &after, 0); int val = strtol(scan, &after, 0);
......
...@@ -8,7 +8,7 @@ void test_core_vector__0(void) ...@@ -8,7 +8,7 @@ void test_core_vector__0(void)
{ {
git_vector x; git_vector x;
int i; int i;
git_vector_init(&x, 1, NULL); cl_git_pass(git_vector_init(&x, 1, NULL));
for (i = 0; i < 10; ++i) { for (i = 0; i < 10; ++i) {
git_vector_insert(&x, (void*) 0xabc); git_vector_insert(&x, (void*) 0xabc);
} }
...@@ -21,7 +21,7 @@ void test_core_vector__1(void) ...@@ -21,7 +21,7 @@ void test_core_vector__1(void)
{ {
git_vector x; git_vector x;
/* make initial capacity exact for our insertions. */ /* make initial capacity exact for our insertions. */
git_vector_init(&x, 3, NULL); cl_git_pass(git_vector_init(&x, 3, NULL));
git_vector_insert(&x, (void*) 0xabc); git_vector_insert(&x, (void*) 0xabc);
git_vector_insert(&x, (void*) 0xdef); git_vector_insert(&x, (void*) 0xdef);
git_vector_insert(&x, (void*) 0x123); git_vector_insert(&x, (void*) 0x123);
...@@ -76,7 +76,7 @@ void test_core_vector__3(void) ...@@ -76,7 +76,7 @@ void test_core_vector__3(void)
{ {
git_vector x; git_vector x;
intptr_t i; intptr_t i;
git_vector_init(&x, 1, &compare_them); cl_git_pass(git_vector_init(&x, 1, &compare_them));
for (i = 0; i < 10; i += 2) { for (i = 0; i < 10; i += 2) {
git_vector_insert_sorted(&x, (void*)(i + 1), NULL); git_vector_insert_sorted(&x, (void*)(i + 1), NULL);
...@@ -99,7 +99,7 @@ void test_core_vector__4(void) ...@@ -99,7 +99,7 @@ void test_core_vector__4(void)
{ {
git_vector x; git_vector x;
intptr_t i; intptr_t i;
git_vector_init(&x, 1, &compare_them); cl_git_pass(git_vector_init(&x, 1, &compare_them));
for (i = 0; i < 10; i += 2) { for (i = 0; i < 10; i += 2) {
git_vector_insert_sorted(&x, (void*)(i + 1), NULL); git_vector_insert_sorted(&x, (void*)(i + 1), NULL);
...@@ -163,7 +163,7 @@ void test_core_vector__5(void) ...@@ -163,7 +163,7 @@ void test_core_vector__5(void)
git_vector x; git_vector x;
int i; int i;
git_vector_init(&x, 1, &compare_structs); cl_git_pass(git_vector_init(&x, 1, &compare_structs));
for (i = 0; i < 10; i += 2) for (i = 0; i < 10; i += 2)
git_vector_insert_sorted(&x, alloc_struct(i), &merge_structs); git_vector_insert_sorted(&x, alloc_struct(i), &merge_structs);
...@@ -205,7 +205,7 @@ void test_core_vector__remove_matching(void) ...@@ -205,7 +205,7 @@ void test_core_vector__remove_matching(void)
size_t i; size_t i;
void *compare; void *compare;
git_vector_init(&x, 1, NULL); cl_git_pass(git_vector_init(&x, 1, NULL));
git_vector_insert(&x, (void*) 0x001); git_vector_insert(&x, (void*) 0x001);
cl_assert(x.length == 1); cl_assert(x.length == 1);
......
...@@ -82,7 +82,7 @@ void test_fetchhead_nonetwork__write(void) ...@@ -82,7 +82,7 @@ void test_fetchhead_nonetwork__write(void)
int equals = 0; int equals = 0;
size_t i; size_t i;
git_vector_init(&fetchhead_vector, 6, NULL); cl_git_pass(git_vector_init(&fetchhead_vector, 6, NULL));
cl_set_cleanup(&cleanup_repository, "./test1"); cl_set_cleanup(&cleanup_repository, "./test1");
cl_git_pass(git_repository_init(&g_repo, "./test1", 0)); cl_git_pass(git_repository_init(&g_repo, "./test1", 0));
......
...@@ -318,11 +318,9 @@ void test_index_addall__files_in_folders(void) ...@@ -318,11 +318,9 @@ void test_index_addall__files_in_folders(void)
void test_index_addall__hidden_files(void) void test_index_addall__hidden_files(void)
{ {
#ifdef GIT_WIN32
git_index *index; git_index *index;
GIT_UNUSED(index);
#ifdef GIT_WIN32
addall_create_test_repo(true); addall_create_test_repo(true);
cl_git_pass(git_repository_index(&index, g_repo)); cl_git_pass(git_repository_index(&index, g_repo));
......
...@@ -49,13 +49,10 @@ void test_index_bypath__add_submodule_unregistered(void) ...@@ -49,13 +49,10 @@ void test_index_bypath__add_submodule_unregistered(void)
void test_index_bypath__add_hidden(void) void test_index_bypath__add_hidden(void)
{ {
#ifdef GIT_WIN32
const git_index_entry *entry; const git_index_entry *entry;
bool hidden; bool hidden;
GIT_UNUSED(entry);
GIT_UNUSED(hidden);
#ifdef GIT_WIN32
cl_git_mkfile("submod2/hidden_file", "you can't see me"); cl_git_mkfile("submod2/hidden_file", "you can't see me");
cl_git_pass(git_win32__hidden(&hidden, "submod2/hidden_file")); cl_git_pass(git_win32__hidden(&hidden, "submod2/hidden_file"));
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment